gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:43:16 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com