qmltestcase找不到C ++类方法

时间:2018-12-10 18:33:13

标签: c++ qt qml qt5 qtestlib

我有一个可以成功运行的简单应用程序;但是,在尝试使用qmltest运行测试用例时遇到了困难。从documentation开始,显示了使用QUICK_TEST_MAIN_WITH_SETUP调用来设置main.cpp文件以初始化QML引擎上下文。有一个运行时消息:QMetaObject::invokeMethod: No such method QObject::qmlEngineAvailable(QQmlEngine*)和一个qwarn消息:QWARN : example::case_1::test_case1() file: MouseQml.qml:44: ReferenceError: mouse is not defined。 QML测试用例找不到我的Class成员函数mouse.clear()。我可以正确设置吗?

main.cpp

#include <QtQuickTest/quicktest.h>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlComponent>
#include "mousememory.h"

class Setup : public QObject
{

public:
    Setup() {}

public slots:
    void qmlEngineAvailable(QQmlEngine *engine)
    {
        QScopedPointer<MouseMemory> mouse(new MouseMemory);
        engine->rootContext()->setContextProperty("mouse", mouse.data());
    }
};

QUICK_TEST_MAIN_WITH_SETUP(example, Setup)

简化的MouseQML.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    id: root
    visible: true
    width: 500
    height: 500

    Row {
        id: tools

        Button {
            id: clear
            objectName: "clear"
            text: "Clear"
            onClicked: {
                canvas.clear()
            }
        }
    }

    Canvas {
        id: canvas
        anchors.top: tools.bottom
        width: 500
        height: 500
        property int lastX: 0
        property int lastY: 0

        function clear() {
            var ctx = getContext("2d")
            ctx.reset()
            canvas.requestPaint()
            mouse.clear()
        }

    }
}

tst_case_1.qml

import QtQuick 2.0
import QtTest 1.0
import "../app"

TestCase {
    name: "case_1"

    MouseQml {
        id: mainApp
    }

    function initTestCase() {
        var qmlClear = findChild(mainApp, "clear")
        tryCompare(qmlClear, "text", "Clear")
    }

    function cleanupTestCase() {
    }

    function test_case1() {
        var qmlClear = findChild(mainApp, "clear")
        mouseClick(qmlClear)
        //tryCompare(qmlClear, "text", "Clear")
    }

    function test_case() {

    }
}

console output

11:45:54: Starting MouseMonitor\build\ui_test\debug\ui_test...
QMetaObject::invokeMethod: No such method QObject::qmlEngineAvailable(QQmlEngine*)
********* Start testing of example *********
Config: Using QtTest library 5.11.2, Qt 5.11.2 (x86_64-little_endian-llp64 shared (dynamic) debug build; by MSVC 2015)
PASS   : example::case_1::initTestCase()
PASS   : example::case_1::test_case()
QWARN  : example::case_1::test_case1() file:///MouseQml.qml:44: ReferenceError: mouse is not defined
PASS   : example::case_1::test_case1()
PASS   : example::case_1::cleanupTestCase()
Totals: 4 passed, 0 failed, 0 skipped, 0 blacklisted, 16ms
********* Finished testing of example *********

1 个答案:

答案 0 :(得分:1)

您有2个错误:

  • 第一个似乎是由于文档的示例不完全正确而引起的,因为它忽略了如果要实现插槽应使用的宏Q_OBJECT的使用,添加#include "main.moc"

  • 另一个是因为您显然不了解QScopedPointer的用途,它的主要任务是在删除对象时消除指针,在这种情况下,当qmlEngineAvailable完成执行时,QScopedPointer将被删除,从而消除了MouseMemory指针。一种可能的解决方案是使该类的QScopedPointer成员延长其生命周期,从而延长其MouseMemory的生命周期。

考虑到上述情况,解决方案如下:

main.cpp

#include "mousememory.h"

#include <QtQuickTest>
#include <QQmlEngine>
#include <QQmlContext>

class Setup : public QObject
{
    Q_OBJECT
public:
    using QObject::QObject;
public slots:

#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
    void applicationAvailable(){
    }
#endif
    void qmlEngineAvailable(QQmlEngine *engine)
    {
        mouse.reset(new MouseMemory);
        engine->rootContext()->setContextProperty("mouse", mouse.data());
    }
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
    void cleanupTestCase(){
    }
#endif
private:
    QScopedPointer<MouseMemory> mouse;
};
QUICK_TEST_MAIN_WITH_SETUP(example, Setup)

#include "main.moc"

找到完整的示例here


注意:在Qt 5.12中添加了两个插槽:

  • void applicationAvailable()

  • void cleanupTestCase()