我正在尝试为这样的用例创建测试:
我能够编写一个测试来完成所有这些并组织对话框以使这样的测试成为可能。但是,会出现一个奇怪的问题:如果这些步骤重复多次,那么最终操作将重复多次。 为简化起见,我们假设操作正在输出输入数据。这是重复模式:
似乎不知何故最后的动作信号被重复,但我无法找到代码的错误。以下是重现问题的简化代码示例:
测试用例
void QtBugIsolationTest::testCase1()
{
auto dialog = new MyDialog();
QTest::mouseClick(dialog->showSubDialogButton, Qt::LeftButton);
QTest::mouseClick(dialog->subDialog->button, Qt::LeftButton);
QTest::mouseClick(dialog->showSubDialogButton, Qt::LeftButton);
QTest::mouseClick(dialog->subDialog->button, Qt::LeftButton);
QTest::mouseClick(dialog->showSubDialogButton, Qt::LeftButton);
QTest::mouseClick(dialog->subDialog->button, Qt::LeftButton);
QVERIFY(true);
}
主要对话框
MyDialog::MyDialog()
{
setGeometry(0, 0, 100, 100);
subDialog = new MySubDialog(this);
showSubDialogButton = new QPushButton(this);
connect(showSubDialogButton, SIGNAL(clicked()), this, SLOT(OnShowSub()));
}
MyDialog::~MyDialog()
{
delete showSubDialogButton;
}
void MyDialog::OnShowSub()
{
connect(subDialog, SIGNAL(accepted()), this, SLOT(OnCloseSub()));
subDialog->show();
}
void MyDialog::OnCloseSub()
{
std::cout << "Closed sub dialog" << std::endl;
subDialog->hide();
disconnect(subDialog);//, SIGNAL(accepted()));
}
子对话
MySubDialog::MySubDialog(QWidget* parent) : QDialog(parent)
{
setGeometry(0, 0, 100, 100);
button = new QPushButton(this);
connect(button, SIGNAL(clicked()), this, SLOT(OnPush()));
auto layout = new QHBoxLayout();
layout->addWidget(button);
setLayout(layout);
}
MySubDialog::~MySubDialog()
{
delete button;
}
void MySubDialog::OnPush()
{
std::cout << "Button Pushed" << std::endl;
accepted();
}
如果请求(并且可能在StackOverflow中),我可以发布整个QtCreator项目或这些类的头文件。
答案 0 :(得分:0)
我终于找到了问题。这是因为disconnect()语句无法正常工作。 从
改变它disconnect(subDialog);
到
subDialog->disconnect();
解决了这个问题。