-1

在我们的项目中,我们对 QML 源代码进行了 C++ 单元测试。它使用以下代码动态加载组件以进行进一步处理

class MyTest {
    ...
    QScopedPointer<QQuickWidget> quickWidget;
    QQuickItem* root = nullptr;

    void setQmlSource(const QString& source)
    {
        quickWidget.reset(new QQuickWidget);
        quickWidget->rootContext()->engine()->addImportPath("qrc:/qml");
        quickWidget->setSource(QUrl::fromUserInput(source));
        root = quickWidget->rootObject();
    }
}

对于像这样的 qml 组件工作正常:

我的.qml:

Rectangle {
   ...
}

但是,当我将组件包装到Dialog

Dialog {
    ...
    Rectangle {
       ...
    }
}

它停止工作:

错误: QQuickWidget仅支持加载从QQuickItem.

这是预期DialogQQuickWindow。但是尝试像这样加载QQuickItemhttps://doc.qt.io/qt-5/qquickview.html#details QQuickView

void MyTest::setQmlWindow(const QString& source)
{
    QQuickView *view = new QQuickView;
    view->rootContext()->engine()->addImportPath("qrc:/qml");
    view->setSource(QUrl::fromUserInput(source));
    root = view->rootObject();
}

也因上述错误而失败。QQmlApplicationEngine并通过这里加载https://stackoverflow.com/a/23936741/630169

void MyTest::setQmlWindow(const QString& source)
{
    engine = new QQmlApplicationEngine;
    //engine->addImportPath("qrc:/qml");
    engine->load(QUrl::fromUserInput(source));
    QObject *myObject = engine->rootObjects().first();;
    QQuickWindow *window = qobject_cast<QQuickWindow*>(myObject);
    root = window->contentItem();
}

失败并出现另一个错误:

QQmlApplicationEngine未能加载组件
QWARN:未安装模块“” QWARN:未MyTest::myMethodTest()安装 模块“ ” ...mynamespace.mymodule
MyTest::myMethodTest()mynamespace.mymodule

为什么为itemview->setSource()正确加载此模块而无法为同一 item qml 源执行但包装到?RectangleQQmlApplicationEngineDialog

注意:这些模块是 C++ 并且可以很好地加载view->setSource().

如果我尝试通过QQmlComponent文档中提到的方式使用和加载:https ://doc.qt.io/qt-5/qqmlcomponent.html#details :

void MyTest::setQmlWindow(const QString& source)
{
    engine = new QQmlApplicationEngine;
    //engine->addImportPath("qrc:/qml");
    QQmlComponent *component = new QQmlComponent(engine, QUrl::fromUserInput(source));
    component->loadUrl(QUrl::fromUserInput(source));
    QQuickWindow *window = qobject_cast<QQuickWindow*>(component->create());
    root = window->contentItem();
}
  • 然后有另一个错误:

    QQmlComponent: 组件没有准备好

ifengine->addImportPath()没有被调用,并崩溃

位置:[未知文件(0)]

engine->addImportPath()调用时出错。

如何正确加载Dialog( QQuickWindow) 并在 C++ 中获取 rootQQuickItem以进行测试?有任何想法吗?谢谢!

4

1 回答 1

1

也许你的问题是关于QQuickWindow/ QQuickView?怎么QQuickWidget相关的问题?那只是QQuickWindow. 无论如何,我会改用一些 QML 动态加载包装器。这个想法是按顺序加载每个组件。下面的例子只是说明了这个想法。如果您不需要功能测试,只需删除计时器和连接并Loader.Ready改为使用。

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Test window")
    property var objects: ["Item1.qml", "Item2.qml", "Item3.qml"]
    property int currentObject: 0
    property var testResults: [0, 0]
    property bool testRunning: false

    onTestRunningChanged: {
        if(window.testRunning)
            console.log("start testing");
        else
            console.log("all the objects has tested. passed: " + window.testResults[0] + ", failed: " + window.testResults[1]);
    }

    function loadObject() {
        try
        {
            loader.source = window.objects[window.currentObject];
        }
        catch(ex) {}
    }

    function checkNext(prevStatus) {
        if(!window.testRunning)
            return;

        window.testResults[prevStatus ? 0 : 1]++;
        timer.running  = false;
        console.log(window.objects[window.currentObject] + ":" + ((prevStatus) ? "passed" : "failed"))
        if(window.currentObject === window.objects.length - 1) {
            window.testRunning = false;
        } else {
            window.currentObject ++;
            loadObject();
            timer.start();
        }
    }

    Loader {
        id: loader
        anchors.centerIn: parent
        onStatusChanged: {
            if (loader.status == Loader.Error) {
                loader.source = "";
                checkNext(false);
            }
        }
    }

    Connections {
        target: loader.item
        onTestPassed: checkNext(true);
    }

    Timer {
        id: timer
        interval: 5000
        repeat: false
        onTriggered: checkNext(false);
    }

    Component.onCompleted: {
        window.testRunning = true;
        loadObject();
    }
}

项目1.qml

import QtQuick 2.0
import QtQuick.Controls 2.5

Dialog {
    id: item
    signal testPassed(bool value);
    title: "Dialog test"
    modal: true
    standardButtons: Dialog.Ok
    visible: true
    onAccepted: item.testPassed(true);
}

Item2.qml

import QtQuick 2.5
import QtQuick.Controls 2.5

Rectangle {
    id: item
    signal testPassed(bool value);

    width: 200
    height: 200
    color: "orange"

    Button {
        anchors.centerIn: parent
        text: "Test"
        onClicked: item.testPassed(true);
    }
}

Item3.qml

import QtQuick 2.5

Item {
    Itemssss {

    }
}
于 2020-01-25T12:22:48.517 回答