liupailife 2020-05-03 21:40 采纳率: 0%
浏览 842

Qt Quick工程中,添加ui.qml文件无法被其他qml文件找到

我的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的,但是报错又说无法找到该文件,不知道是啥情况

  • 写回答

2条回答 默认 最新

  • qtchen_1988 2020-05-04 10:36
    关注

    你的项目是否在改名称之前运行过,如果是,你关闭项目,把编译文件全部删掉再打开从新编译运行试试

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥35 跳过我的世界插件ip验证
  • ¥15 抖音看过的视频,缓存在哪个文件
  • ¥15 自定义损失函数报输入参数的数目不足
  • ¥15 如果我想学习C大家有是的的资料吗
  • ¥15 根据文件名称对文件进行排序
  • ¥15 deploylinux的ubuntu系统无法成功安装使用MySQL❓
  • ¥15 有人会用py或者r画这种图吗