@KelvinS。这是我遵循@VGambit 方法的代码片段,它尝试测试将日志添加到itemview。
#include <QApplication>
#include <QWidget>
#include <QtTest/QtTest>
#include "guimain.h"`
#include "xlogview.h"`
class TestLogView:public QObject
{
Q_OBJECT
public:
void set_mainwindow(QWidget * qw);
public slots:
void startTest();
private:
QWidget * m_qw ;
private slots:
void addItem();
};
void TestLogView::startTest()
{
QTest::qExec(this);
}
void TestLogView::set_mainwindow(QWidget * qw)
{
m_qw = qw;
}
void TestLogView::addItem()
{
XLogView * test_logview= m_qw->findChild<XLogView*>();
bool ret = test_logview->addLog("new log");
QVERIFY (ret == true);
}
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GUIMain window;
window.show();
app.processEvents();
TestLogView test;
test.set_mainwindow(&window);
QTimer::singleShot(1000, &test, SLOT(startTest()));
return app.exec();
}