首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >qmltestcase找不到c++类方法

qmltestcase找不到c++类方法
EN

Stack Overflow用户
提问于 2018-12-11 02:33:13
回答 1查看 400关注 0票数 1

我有一个成功运行的简单应用程序;但是,我在尝试使用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测试用例找不到我的类成员函数mouse.clear()。我的设置正确吗?

main.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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 *********
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-11 04:23:44

您有两个错误:

第一个问题似乎是因为文档的例子并不完全正确,因为它省略了宏Q_OBJECT的使用,如果你想实现插槽,你应该添加

  • ,因为显然你不理解QScopedPointer是做什么的,它的主要任务是在对象也被删除的时候消除指针,在这种情况下,当qmlEngineAvailable完成执行时,QScopedPointer将被删除,从而消除qmlEngineAvailable指针。一种可能的解决方案是使类的QScopedPointer成员扩展其生命周期,从而扩展MouseMemory的生命周期。

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

main.cpp

代码语言:javascript
复制
#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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53711633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档