c++ - 调用 qExec "no known conversion for argument 1 to QObject"时出错

标签 c++ qt testing qobject qttest

我正在尝试使用 QtTest 为 C++ 应用程序创建测试。我拥有的三个相关文件是:包含我的主要功能的 GuiTests.cpp,包含我的测试的 testsuite1.cpptestsuite1.h其中包含我的测试的定义。我在不同指南的帮助下创建了这些文件,例如 this one .

当我尝试构建时出现此错误:

no matching function for call to 'qExec(TestSuite1 (*)(), int&, char**&)'

no known conversion for argument 1 from 'TestSuite1 (*)()' to 'QObject*'

我不明白为什么,正如你在TestSuite1下面的testsuite.h中看到的,是一个QObject。有趣的是这个确切的代码(我很确定)之前工作但后来我摆弄着将 argcargv 传递给 guiTest()有一段时间,在我删除 argcargv 并回到我之前拥有的东西(我现在拥有的东西,请参阅下面的文件)之后,我得到了这个错误。

我已经尝试解决这个问题很长时间了,但我在网上找不到任何答案,所以请帮助我,我们将不胜感激。谢谢!

GuiTests.cpp

#include "testsuite1.h"
#include <QtTest>
#include <QCoreApplication>

int main(int argc, char** argv) {
    TestSuite1 testSuite1();
    return QTest::qExec(&testSuite1, argc, argv);
}

测试套件1.h

#ifndef TESTSUIT1_H
#define TESTSUIT1_H

#include <QMainWindow>
#include <QObject>
#include <QWidget>
#include <QtTest>

class TestSuite1 : public QObject {
Q_OBJECT
public:
    TestSuite1();
    ~TestSuite1();

private slots:
    // functions executed by QtTest before and after test suite
    void initTestCase();
    void cleanupTestCase();

    // functions executed by QtTest before and after each test
    //void init();
    //void cleanup();

    // test functions
    void testSomething();
    void guiTest();
};

#endif // TESTSUIT1_H

测试套件1.cpp

#include "testsuite1.h"
#include <QtWidgets>
#include <QtCore>
#include <QtTest>

TestSuite1::TestSuite1()
{

}

TestSuite1::~TestSuite1()
{

}

void TestSuite1::initTestCase()
{

}

void TestSuite1::cleanupTestCase()
{

}

void TestSuite1::guiTest()
{
    QVERIFY(1+1 == 2);
}

void TestSuite1::testSomething()
{
    QLineEdit lineEdit;

    QTest::keyClicks(&lineEdit, "hello world");

    QCOMPARE(lineEdit.text(), QString("hello world"));

    //QVERIFY(1+1 == 2);
}

//QTEST_MAIN(TestSuite1)
//#include "TestSuite1.moc"

最佳答案

TestSuite1 testSuite1();

声明一个名为 testSuite1 的函数返回 TestSuite1。获取它的地址会为您提供 TestSuite1 (*)()(函数指针),而不是将转换为 QObject*TestSuite1*

使用以下之一:

TestSuite1 testSuite1;
TestSuite1 testSuite1{};
auto testSuite1 = TestSuite();
auto testSuite1 = TestSuite{};

声明一个变量。

关于c++ - 调用 qExec "no known conversion for argument 1 to QObject"时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38092843/

相关文章:

c++ - 如何在 QT 中创建具有多个属性的按钮?

c++ - Qt Creator session 管理器错误?

c++ - 如何使 QT 小部件从其父小部件更新修改后的属性?

c++ - 动态多态内存容器 - 返回值不正确

c++ - 在使用Builder Pattern的构造函数中将指针类型的成员数据初始化为NULL

android - 创建 Android APK 测试应用程序

testing - 有没有办法 'test run' 一个 Ant 构建?

c++ - 将存储在int中的内存地址分配给int

python - 在PyQT中给QPlainTextEdit添加文本(结果是一个状态日志)

java - 如何对数据库访问/ORM 实体进行单元测试?