Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rectangle as a root element in QML

Tags:

qt

qml

qtquick2

I use Qt 5.5.0 MSVC 2013, 32bit.
I want to create minimal QtQuick application. When I choose New Project - Qt Quick Application I got project with 2 QML files: main.qml and MainForm.ui.qml. Since I do not need them, I delete second one and paste following to main.qml:

Import QtQuick 2.4

Rectangle{
    id: root
    visible: true
    color: "gray"
    width: 400
    height: 800
}

But when I run project I got nothing. I see application in Task Manager but there is no application window.
Question: Is it possible to create .qml file with Rectangle as a root element?

like image 436
fat Avatar asked Sep 04 '15 07:09

fat


People also ask

What is Z in QML?

Now QML gives you chance to change the stacking order through z property of the item. in the above example if I assign z property of the red rectangle to have a value of anything above 0, I would see it on top of blue rectangle. So z property has changed the stacking order for the sibling item.

What is parent in QML?

The concept of the visual parent in Qt Quick is separate from, but related to, the concept of the object parent within the QObject parent hierarchy. All QML objects have an object parent, which is determined by the object hierarchy in which the object is declared.

What is property alias in QML?

Unlike an ordinary property definition, which allocates a new, unique storage space for the property, a property alias connects the newly declared property (called the aliasing property) as a direct reference to an existing property (the aliased property).


1 Answers

Solution was found at official Qt Forum.

The template for creating Qt Quick Application adds QQmlApplicationEngine to launch the QML. But QQmlApplicationEngine dont work directly with Rectangle or Item as root element but requires any window like Window or ApplicationWindow. So to make it work for Rectangle use QQuickView instead of QQmlApplicationEngine.

I changed content of main.cpp to

#include <QGuiApplication>
#include <QQuickView>

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

    QQuickView *view = new QQuickView;
    view->setSource(QUrl("qrc:/main.qml"));

    view->show();

    return app.exec();
}

and it solved my problem.

like image 135
fat Avatar answered Oct 15 '22 09:10

fat