如何获得C++中QML ApplicationWindow的大小?

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

if (engine.rootObjects().isEmpty())
    return -1;

QObject *application_object = engine.rootObjects().first();

// Throws ApplicationWindow_QMLTYPE_11::height(int), no such signal
QObject::connect(application_object, SIGNAL(height(int)), &my_obj_here, SLOT(set_game_height(int)));
QObject::connect(application_object, SIGNAL(width(int)), &my_obj_here, SLOT(set_game_width(int)));

return app.exec();

我意识到我也没有获得ApplicationWindow内容的大小(减去工具栏,菜单栏等),但是如何访问它呢?

尝试使用window方法访问window_object上的property属性会返回空指针。

最佳答案

一种可能的解决方案是使用QQmlProperty获取QQuickItem,然后与信号heightChangedwidthChanged连接,这些信号仅通知属性已更改,但不指示值,因此必须使用height()width()方法。

QObject *topLevel = engine.rootObjects().first();
QQuickItem *contentItem =qvariant_cast<QQuickItem *>(QQmlProperty::read(topLevel, "contentItem"));
if(contentItem){
    QObject::connect(contentItem, &QQuickItem::heightChanged,
                     [&my_obj_here, contentItem](){
        my_obj_here.set_game_height(contentItem->height());
    });
    QObject::connect(contentItem, &QQuickItem::widthChanged,
                     [&my_obj_here, contentItem](){
        my_obj_here.set_game_width(contentItem->width());
    });
}

另一种解决方案是在QML端建立连接,为此,您必须创建q-property:
class GameObject: public QObject{
    Q_OBJECT
    Q_PROPERTY(int game_width READ game_width WRITE set_game_width NOTIFY game_widthChanged)
    Q_PROPERTY(int game_height READ game_height WRITE set_game_height NOTIFY game_heightChanged)
public:
    using QObject::QObject;
    int game_width() const{
        return m_width;
    }
    void set_game_width(int width){

        if(width == m_width)
            return;
        m_width = width;
        emit game_widthChanged();
    }
    int game_height() const{
        return m_height;
    }
    void set_game_height(int height){
        if(height == m_height)
            return;
        m_height = height;
        emit game_heightChanged();
    }
signals:
    void game_widthChanged();
    void game_heightChanged();
private:
    int m_width;
    int m_height;
};

main.cpp
...
GameObject my_obj_here;

QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("my_obj_here", &my_obj_here);
...

main.qml
ApplicationWindow{


    Connections{
        target: contentItem
        onHeightChanged:
            my_obj_here.game_height = height
        onWidthChanged:
            my_obj_here.game_width = width
    }
    ...

关于c++ - 在C++中获取ApplicationWindow的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51223199/

10-10 04:32