我的Qt Creator版本为4.6.2 ,QT版本5.11.
新建空的Qt Quick工程,在其中添加Qt Quick UI File
Xxx.qml代码如下
import QtQuick 2.7
import QtQuick.Window 2.2
Window{
visible: true
width: 640
height: 480
title: qsTr("Hello World")
XxxForm{
anchors.fill:parent
MouseArea.onClicked: {
console.log(qsTr('Clicked on background.Text:"'+textEdit.text+'"'))
}
}
}
XxxForm.ui.qml代码如下
import QtQuick 2.7
Rectangle {
property alias MouseArea: mouseArea
property alias TextEdit: textEdit
width: 360
height: 360
MouseArea {
id: mouseArea
anchors.fill: parent
}
TextEdit {
id: textEdit
Text: qsTr("Enter some text...")
verticalCenter: Text.AlignHCenter
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 20
Rectangle {
anchors.fill: parent
anchors.margins: -10
color: "transparent"
border.width: 1
}
}
}
main.cpp如下
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/Xxx.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
运行后报错如下
QML debugging is enabled. Only use this in a safe environment.
QQmlApplicationEngine failed to load component
qrc:/Xxx.qml:9 Type XxxForm unavailable
qrc:/XxxForm.ui.qml:4 Alias names cannot begin with an upper case letter
qrc:/XxxForm.ui.qml:5 Alias names cannot begin with an upper case letter
但是我在Xxx.qml中点击Xxxform是能进入XxxForm.ui.qml的,但是报错又说无法找到该文件,不知道是啥情况