我最近安装了适用于 Mac OS X 的 Qt5 RC2 并开始开发一些 QML 应用程序。看了新元素后,特别想试试 Window and Screen Element。 (http://qt-project.org/doc/qt-5.0/qtquick/qmlmodule-qtquick-window2-qtquick-window-2.html)
所以我将导入设置在文件的顶部,如下所示:
导入QtQuick 2.0
导入 QtQuick.Window 2.0
已找到导入,但我既不能使用 Window 也不能使用 Screen。每次我键入 Screen 或 Window 时都会出现错误,提示“未知组件 (M300)”
有人知道问题出在哪里吗?
最佳答案
有时 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 项嵌入其中。
关于qt - 如何在 Qt5 和 QML 中使用 QtQuick.Window 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13968370/