1
votes

I'm working through the book "Learn Qt 5" from Nicholas Sherriff. Now I have some problems getting the QtStackView component to work properly.

I've written following code:

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

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

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/views/MasterView.qml")));

    return app.exec();
}

MasterView.qml

import QtQuick 2.5
import QtQuick.Window 2.0
import QtQuick.Controls 1.4

Window {
    visible: true
    width: 1024
    height: 768
    title: qsTr("Client Management")

    StackView {
        id: contentFrame
        initialItem: Qt.resolvedUrl("qrc:/views/SplashView.qml")
    }
}

SplashView.qml

import QtQuick 2.5

Rectangle {
    anchors.fill: parent
    color: "#f04c42"
}

But when executing, the rectangle from SplashView.qml does not appear. I do not get any errors. If I put the rectangle block inside the window block it works. But even if i put the rectangle with an id in the StackView block it doesn't work.

I'm using an older version of QtQuick than the book recommends, becouse im using not the newest debian distribution. Am I missing something?

Thanks

1

1 Answers

0
votes

The problem is simple, the Rectangle in the SplashView takes the size of the parent:

Rectangle {
    anchors.fill: parent //<---
    color: "#f04c42"
}

And the parent is the StackView, but the StackView has no size, so the solution is to set a size, for this we can use the anchors:

StackView {
    id: contentFrame
    anchors.fill: parent // <--- possible solution
    initialItem: Qt.resolvedUrl("qrc:/views/SplashView.qml")
}