QTest:测试带有子对话框的对话框

QTest : testing dialogs with sub dialogs

我正在尝试为这样的用例创建测试:

我能够编写完成所有这些的测试并组织对话框以使此类测试成为可能。但是,会出现一个奇怪的问题:如果这些步骤重复多次,那么最后的动作就会重复多次。 为简化起见,我们假设该操作正在输出输入数据。这是重复模式:

似乎不​​知何故重复了最后的动作信号,但我一直没能找到代码有什么问题。这是重现问题的简化代码示例:

测试用例

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();
}

如果需要(并且可能在 Whosebug 中),我可以 post 整个 QtCreator 项目或那些 类.

的头文件

我终于找到问题所在了。 disconnect() 语句不起作用。 从

更改它
disconnect(subDialog);

subDialog->disconnect();

已解决问题。