QtQuick StackView加载问题

时间:2018-08-04 10:33:04

标签: qt qml c++14 qtquick2

我正在研究Nicholas Sherriff的书“ Learn Qt 5”。 现在,我在使QtStackView组件正常工作时遇到一些问题。

我编写了以下代码:

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"
}

但是执行时,不会显示SplashView.qml中的矩形。 我没有任何错误。如果我将矩形块放在窗口块内,它将起作用。但是,即使我将带有id的矩形放入StackView块中,也行不通。

我使用的QtQuick版本比书中建议的版本晚,因为不是使用最新的Debian发行版。 我想念什么吗?

谢谢

1 个答案:

答案 0 :(得分:0)

问题很简单,SplashView中的Rectangle取父对象的大小:

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

父级是StackView,但是StackView没有大小,因此解决方案是设置大小,为此,我们可以使用锚点:

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