How to create a Window using QML ?



  • I know now to create a QML window in C++, but my qml code will frequently operate the window, so I want to create the window using QML.
    I wan't to write code like this:

    @
    Window {
    id: test_win
    source: "Component/xxx.qml"
    }
    @

    I wrote a class MyWindow derived from QDeclarativeView, and called qmlRegisterType.
    the MyWindow always use a global QDeclarativeView as father.

    but when I setSource to the global QDeclarativeView, error occurs:

    ASSERT: "!d->isWidget" in file kernel\qobject.cpp, line 1894

    Can anyone help?

    Thank you!

    I notice http://qt-project.org/doc/qt-5.0/qtquick/qmlmodule-qtquick-window2-qtquick-window-2.html in Qt5's document, but I'm currently still using Qt4, and I didn't found a QtQuick.Window 1.0 in Qt4..


  • Moderators

    QtQuick.Window is part of QtQuick2 and is not available in Qt4 (and is not going to be - Qt4 is considered feature complete, it will only receive bug fixes in the future).

    Interesting approach on your side, I don't see why it is not working. Maybe other declarative children interfere (a Window inside would be parented to it's QML parent, but you force it to reparent to global QDeclarativeView).

    Have you looked up the failing ASSERT code in QObject? I had some situations where a simple commenting out of an assert solved problems (that was back when Qt5 was rough, long before the alpha). If you do that, though, remember about GPL/ LGPL requirements.



  • Well, according to the ASSERT, I know that a QWidget cannot be directly put into QML code, but QDeclarativeView is inherited from QWidget, and MyWindow is inherited from QDeclarativeView, so the ASSERT failed.

    Another way is make MyWindow inherited from QObject and make a QDeclarativeView as a member of MyWindow, but this also lead to many code(rewritten of many properties, such as size, pos)

    So I finally give up this try. Thank you:)



  • In desktop components we also expose a Window just like this. (which is no longer needed in Qt 5) But you can see if it works better for you.

    https://qt.gitorious.org/qtplayground/qtdesktopcomponents (qt4 branch)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.