如何在Qt5和QML中使用QtQuick.Window元素?

3
我最近在Mac OS X上安装了Qt5 RC2并开始开发一些QML应用程序。看完新元素后,我特别想尝试Window和Screen Element。(http://qt-project.org/doc/qt-5.0/qtquick/qmlmodule-qtquick-window2-qtquick-window-2.html)
所以我像这样在文件顶部设置导入:
import QtQuick 2.0 import QtQuick.Window 2.0
虽然找到了导入,但是我既不能使用Window也不能使用Screen。每次我输入Screen或Window时都会出现一个错误,提示"Unknown component (M300)"。
有人知道问题出在哪里吗?

Qt 5.0已经发布了。你试过使用它吗? - sebasgo
是的,我尝试过了。但它仍然不起作用。 - Dominik
1个回答

5
有时,QtCreator不能识别一些类型/属性,主要是那些在Qt4.x中不存在的类型/属性,但这并不意味着你不能使用它们。是的,'Window'和属性 'antialiasing'、'fontSizeMode'或'active'一样,都是未知的,但是你仍然可以使用它们。以下是使用 QtQuick.Window 2.0 的示例:
import QtQuick        2.0
import QtQuick.Window 2.0

Window {
    id: win1;
    width: 320;
    height: 240;
    visible: true;
    color: "yellow";
    title: "First Window";

    Text {
        anchors.centerIn: parent;
        text: "First Window";

        Text {
            id: statusText;
            text: "second window is " + (win2.visible ? "visible" : "invisible");
            anchors.top: parent.bottom;
            anchors.horizontalCenter: parent.horizontalCenter;
        }
    }
    MouseArea {
        anchors.fill: parent;
        onClicked: { win2.visible = !win2.visible; }
    }

    Window {
        id: win2;
        width: win1.width;
        height: win1.height;
        x: win1.x + win1.width;
        y: win1.y;
        color: "green";
        title: "Second Window";

        Rectangle {
            anchors.fill: parent
            anchors.margins: 10

            Text {
                anchors.centerIn: parent
                text: "Second Window"
            }
        }
    }
}

你只需要将 Window 作为根对象,就可以将其他 Window 项嵌入其中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接