qml - How to show a window?



  • There is this book about qml http://qmlbook.github.io/en/ch02/index.html
    and when I tried to run this example in qt creator or as ./project_name where project_name is executable file created by qt creator there is no visible window:

    
    Rectangle {
        width: 360
        height: 360
        Text {
            anchors.centerIn: parent
            text: "Hello World"
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                Qt.quit();
            }
        }
    }
    
    why is that?
    Am I missing soething or maybe I have chosen wrong project type (Qt Quick Application - Empty)?
    The only way to see the window is to run:
    **qmlscene main.qml**
    

  • Moderators

    @twar said in qml - How to show a window?:

    when I tried to run this example in qt creator or as ./project_name where project_name is executable file created by qt creator there is no visible window:
    ...
    why is that?
    ...
    The only way to see the window is to run:
    qmlscene main.qml

    You used a Rectangle as your application's top-level object. However, a Rectangle needs to be put in an actual window to be seen. To create a window for this file, on the C++ side you must to load your QML file in a QQuickView or QQuickWidget. This is the old way of displaying a QML-based GUI.

    Note: qmlscene contains a QQuickView that dynamically loads the QML file that you specify.

    The new way does not require QQuickView or QQuickWidget; instead, it lets you define a window directly in QML code. To see it in action, just create a new Qt Quick Application (it doesn't matter which type) and look at the auto-generated code. You'll see that it has a Window at the top-level, not a Rectangle.

    EDIT: The auto-generated code is complete. You can run it.

    There is this book about qml http://qmlbook.github.io/en/ch02/index.html
    ...
    Am I missing soething or maybe I have chosen wrong project type (Qt Quick Application - Empty)?

    That tutorial was written before it was possible to create a Window in QML code.


Log in to reply
 

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