Qt application disappears

456 views Asked by At

hey guys I have a very simple QML file and I'm loading it with the following code:

   QQuickView view;
   view.setSource(QUrl(QStringLiteral("qrc:/Login.qml")));
   view.show();
   return app.exec();

The app GUI stays open for about 1 second and then disappears, the app still runs but no GUI, no error messages display or anything.

Here's the QML:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1

Rectangle {
    anchors.centerIn: parent
    Layout.minimumWidth: 360
    Layout.minimumHeight: 360
    Layout.preferredWidth: 480
    Layout.preferredHeight: 640
    Column {
        anchors.centerIn: parent
        spacing: 16
        Row {
            spacing: 4
            MediumText { text: "Username:" }
            TextField { placeholderText: "username"; Layout.fillWidth: true }
        }
        Row {
            spacing: 4
            MediumText { text: "Password:" }
            TextField { placeholderText: "password"; echoMode: TextInput.Password; Layout.fillWidth: true }
        }
        Row {
            spacing: 16
            anchors.horizontalCenter: parent.horizontalCenter
            Button { text: "Login"; onClicked: console.log("login") }
            Button { text: "Exit"; onClicked: console.log("guest") }
        }
    }
}

interestingly enough if I remove the "anchors.centerIn: parent" on the root Rectangle, it works fine, but doesn't center the content.

Anyone know what the issue is?

1

There are 1 answers

0
alessandro ferrucci On

I added the following line:

view.setResizeMode(QQuickView::SizeRootObjectToView);

and it fixed the issue.