检查一个QMessageBox并关闭它 [英] Check for a QMessageBox and close it

查看:2368
本文介绍了检查一个QMessageBox并关闭它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在c ++ qt中使用qtest(在eclipse中)自动化gui测试
我已经给一个函数添加了一些选项卡到tabwidget(最多9个),如果你尝试打开第10个选项卡,一个QMessageBox出现:

I need to automate gui tests in c++ qt using qtest (in eclipse) I have given a function that adds some tabs to a tabwidget (up to max 9) and if you try to open a 10th tab, a QMessageBox appears:

QMessageBox::critical(this, "MAX9",
tr("Only a maximum of 9 tabs can be opened.\n"));

因为整个菜单带有添加标签功能和一切都是私有的,我不得不访问方法使用槽和信号从我的测试类。

Because the whole menu with the "add tab" function and everything is private, I had to access the method using slots and signals from my testclass.

现在我的问题是,有一种方法,我可以检查是否有任何QMessageBoxes打开,如果是,自动关闭它们?

Now my question is, is there a way I can check whether there are ANY QMessageBoxes open and if yes, automatically close them?

编辑:解决
我把vahancho的解决方案放入一个方法(CloseMessageBoxes),我已经创建了一个计时器在我的测试方法调用CloseMessageBoxes()

SOLVED I put vahancho's solution into a method (CloseMessageBoxes) and I've created a timer in my testmethod that calls CloseMessageBoxes() method then:

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(CloseMessageBoxes()));
timer->start(5000);

有多个消息框出现,但都在5秒后自动关闭。

There are multiple messageboxes appearing but all are closing themselves after 5 seconds.

推荐答案

我认为,你可以找到所有的消息框,因为他们是顶级小部件,并关闭它们一个一个:

I think, you can find all message boxes as they are top level widgets, and close them one by one:

QWidgetList topWidgets = QApplication::topLevelWidgets();
foreach (QWidget *w, topWidgets) {
    if (QMessageBox *mb = qobject_cast<QMessageBox *>(w)) {            
        QTest::keyClick(mb, Qt::Key_Enter);
    }
}

但问题是消息框是一个模态对话框并阻塞主事件循环。在消息框出现后,您需要找到一种执行上述代码的方法。

However the problem is that message box is a modal dialog and it blocks the main event loop. You need to find a way to execute the code above after a message box appeared.

这篇关于检查一个QMessageBox并关闭它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆