[Interest] QML ApplicationWindow fails to open a window

Juan Navarro oneorjuan at gmail.com
Mon May 26 16:04:34 CEST 2014


Hi there;
i'm struggling with the behavior of ApplicationWindow and I found that
it only opens a window when used directly from main() but not from a
custom class. Let's explain with an example:

----
main.qml:
    import QtQuick 2.2
    import QtQuick.Controls 1.1

    ApplicationWindow {
        visible: true
        width: 640
        height: 480

        Text {
            text: "Hello World"
            anchors.centerIn: parent
        }
    }
----
main.cpp:
    #include <QApplication>
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QObject>
    #include <QWidget>
    #include <QUrl>

    class MainWindow1
    {
    public:
        MainWindow1()
        { QQmlApplicationEngine engine("main.qml"); }
    };

    class MainWindow2 : public QObject
    {
    public:
        MainWindow2(QObject* parent = 0)
            : QObject(parent)
        { QQmlApplicationEngine engine("main.qml"); }
    };

    class MainWindow3 : public QWidget
    {
    public:
        MainWindow3(QWidget* parent = 0, Qt::WindowFlags flags = 0)
            : QWidget(parent, flags)
        { QQmlApplicationEngine engine("main.qml"); }
    };

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

    //    MainWindow1 window;
    //    MainWindow2 window;
    //    MainWindow3 window;
        QQmlApplicationEngine engine("main.qml");

        return app.exec();
    }
----

Check the main() function: only with the uncommented code I get to see
a new Window which shows the "Hello World" text. If we comment that
and uncomment any of the other 3 options, which use a custom
"MainWindow" class, then nothing will come out. I derived MainWindow
from QObject and QWidget in hopes to find some solution, but I
actually would think that a standalone MainWindow class should suffice
for this basic usage scenario.

Is it a bug or I am misusing this class?

Also, extracting the inner QQuickWindow and forcing a call to show()
doesn't work; ie:
    MainWindow1()
    {
        QQmlApplicationEngine engine("main.qml");
        Q_ASSERT(engine.rootObjects().size() > 0);
        QQuickWindow* window =
qobject_cast<QQuickWindow*>(engine.rootObjects().at(0));
        window->show();
    }
doesn't work.



More information about the Interest mailing list