Qt 在资源文件don’中使用qml编程;t打开非主qml文件

Qt 在资源文件don’中使用qml编程;t打开非主qml文件,qt,qt5,qt-quick,qtquick2,Qt,Qt5,Qt Quick,Qtquick2,我安装了适用于Windows 32位(MinGW 4.7823 MB)的Qt5.0.1 然后我创建了简单的Quick2应用程序。 我有两个简单的qml文件: main.qml import QtQuick 2.0 Rectangle { width: 360 height: 360 Text { text: qsTr("Hello World") anchors.centerIn: parent } MouseArea {

我安装了适用于Windows 32位(MinGW 4.7823 MB)的Qt5.0.1

然后我创建了简单的Quick2应用程序。 我有两个简单的qml文件:

main.qml

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360
    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
    HarrixMainButton{

    }
}
import QtQuick 2.0

Item {
    width: 93
    height: 93

    Rectangle {
        width: 50
        height: 62
        color: "red"
    }

}
HarrixMainButton.qml

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360
    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
    HarrixMainButton{

    }
}
import QtQuick 2.0

Item {
    width: 93
    height: 93

    Rectangle {
        width: 50
        height: 62
        color: "red"
    }

}
该计划运作良好。然后,我将qml文件放入前缀为qml的resource res.qrc中,并更改main.cpp:

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QUrl>
#include <QDebug>
#include <QQmlContext>
#include <QQuickItem>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    //viewer.setMainQmlFile(QStringLiteral("qml/HarrixAI/main.qml"));
    viewer.setSource(QUrl("qrc:qml/qml/HarrixAI/main.qml"));
    viewer.showExpanded();

    return app.exec();
}
#包括
#包括“qtquick2applicationviewer.h”
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
QGUI应用程序应用程序(argc、argv);
QTQuick2应用程序查看器;
//setMainQmlFile(QStringLiteral(“qml/HarrixAI/main.qml”);
setSource(qrl(“qrc:qml/qml/HarrixAI/main.qml”);
viewer.showExpanded();
返回app.exec();
}
而且这个程序不起作用。未加载第二个文件HarrixMainButton.qml。仅加载第一个主文件main.qml

qrc:qml/qml/HarrixAI/main.qml:16:5:HarrixMainButton不是类型 尝试渲染QtQuick2ApplicationViewer(0x28fe08)(QRect(8,30 116x0))时,找不到可渲染主窗口QtQuick2ApplicationViewer(0x28fe08)。


如何解决这个问题?在使用Qt Quick 1.1的Qt 4.7中,同样的方法也适用。

加载main.qml时,不要在URL中使用相对路径,它应该可以工作:


qrc:/qml/qml/HarrixAI/main.qml?是。这是一条正确的道路。