我有一些班级可以与我的班级发出一些信号。例子:
class CMyClass : public QBytaArray
{
void SomeAction();
}
class CMainClass : public QObject
{
signals:
void testSignal(const CMyClass &myClass);
public:
void test() {
CMyClass data;
emit testSignal(data);
}
}
我为 CMainClass 编写了 unix 测试:
标题:
#ifndef TESTMAINCLASS_H
#define TESTMAINCLASS_H
#include <QObject>
#include <QSignalSpy>
#include "MainClass.h"
Q_DECLARE_METATYPE(CMyClass)
class CTestMainClass : public QObject
{
Q_OBJECT
public:
explicit CTestMainClass(QObject *parent = 0);
private slots:
void testS();
};
#endif // TESTMAINCLASS_H
资源:
#include <QDebug>
#include "TestMainClass.h"
CTestMainClass::CTestMainClass(QObject *parent) : QObject(parent)
{
qRegisterMetaType<CMyClass>();
}
void CTestMainClass::testS()
{
qDebug() << "Test signal emitor";
CMainClass prc;
QSignalSpy spy(&prc, SIGNAL(testSignal(const CMyClass&)));
prc.test();
CMyClass buf = qvariant_cast<CMyClass>(spy.at(0).at(0));
}
但是,在日志文件中我有:
PASS : CTestCommandProcessor::initTestCase() QDEBUG : CTestCommandProcessor::testS() 测试信号发射器 QDEBUG : CTestCommandProcessor::testS() Count: 1 QDEBUG : CTestCommandProcessor::testS() Vals: QVariant(CMyClass, ) C 中发生崩溃:\UnitTest\build-UnitTest-Qt_5_7_1_5_7_1_Static-Release\release\UnitTest.exe。功能时间:0ms 总时间:0ms
我试图改变字符串:
CMyClass buf = qvariant_cast< CMyClass >(spy.at(0).at(0));
至
CMyClass buf = qvariant_cast< const CMyClass& >(spy.at(0).at(0));
但是,我遇到了同样的错误。怎么了?我可以在我自己的类中使用 Qt 的 unittest 信号吗?