c++ - 如何在 Qt 中获取指向弹出对话框的指针,这会阻止 QTest 中的 UI 线程
问题描述
我正在使用 QTest 编写一个集成测试,其中只要我单击小部件的视口,就会出现一个多行弹出 QInputDialog,它会阻止代码的进一步执行并需要手动取消对话框。这是代码:
void PartTest::testTypewriterAnnotTool()
{
Okular::Part part(nullptr, nullptr, QVariantList()); // It is the main widget of PDF reader comprising of viewport where PDF page is shown
part.openUrl(QUrl::fromLocalFile(QStringLiteral(KDESRCDIR "data/file1.pdf"))); // open file1.pdf
part.widget()->show();
QVERIFY(QTest::qWaitForWindowExposed(part.widget()));
// Width and height of pageView widget, the child of Part widget which shows the PDF page
const int width = part.m_pageView->horizontalScrollBar()->maximum() +
part.m_pageView->viewport()->width();
const int height = part.m_pageView->verticalScrollBar()->maximum() +
part.m_pageView->viewport()->height();
part.m_document->setViewportPage(0); // sets viewport page 0, i.e. page number 1
QMetaObject::invokeMethod(part.m_pageView, "slotToggleAnnotator", Q_ARG( bool, true )); // toggles and shows the annotation toolbar with all tools avaialable
QList<QToolButton *> toolbuttonList = part.m_pageView->findChildren<QToolButton *>(); // find a list of annotation toolbutton
// Check if the tooltip of 10th button is "Typewriter"
QToolButton* typewriterButton = toolbuttonList.at(9);
QCOMPARE( typewriterButton->toolTip(), QStringLiteral("Typewriter") );
typewriterButton->click(); // clicking and selecting typewriter annotation tool
QTest::mouseMove(part.m_pageView->viewport(), QPoint(width * 0.5, height * 0.2)); // leading mouse pointer to a specific point in the viewport
QTest::mouseClick(part.m_pageView->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(width * 0.5, height * 0.2)); // mouse left button click on the viewport and a popup dialog with 'add a new note' appears
}
我必须编写一个测试用例,我必须抓住那个弹出对话框并以编程方式关闭它。我试图通过使用 QTimer 来实现这一点,我将在其中执行一个函数以在单击视口 1 秒后抓取对话框,然后尝试像这样关闭它:
class PartTest
{
...
private:
Okular::part *m_part;
}
void PartTest::testTypewriterAnnotTool()
{
...
m_part = ∂
QTimer* mTimer = new QTimer(this);
mTimer->setSingleShot(true);
connect(mTimer, SIGNAL(timeout()), SLOT(testDialogClosed()));
mTimer->start(1000);
QTest::mouseClick(part.m_pageView->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(width * 0.5, height * 0.2));
}
void PartTest::testDialogClosed()
{
bool m_clicked = false;
QDialog *dialog = m_part->widget()->findChild<QDialog*>();
if (dialog)
{
QDialogButtonBox *buttonBox = dialog->findChild<QDialogButtonBox*>();
buttonBox->button(QDialogButtonBox::Cancel)->click();
m_clicked = true;
}
QVERIFY(m_clicked);
}
在这里,QVERIFY(m_clicked)
测试用例总是“失败”,这意味着弹出窗口QInputDialog is not the child of Okular::Part
,我没有办法抓住指向这个对话框窗口的指针并关闭它。有什么帮助吗?
解决方案
您可以枚举顶级小部件并以这种方式找到对话框:
template <class P> QList<P> topLevelWidgets() {
QList<P> widgets
for (auto *w : QApplication::topLevelWidgets())
if (auto *t = qobject_cast<P>(w))
widgets << t;
return widgets;
}
template <class P> P topLevelWidget() {
auto w = topLevelWidgets<P>();
return (w.size() == 1) ? w.first() : nullptr;
}
void PartTest::testDialogClosed()
{
bool m_clicked = false;
if (auto *dialog = topLevelWidget<QDialog*>())
if (auto *buttonBox = dialog->findChild<QDialogButtonBox*>()) {
buttonBox->button(QDialogButtonBox::Cancel)->click();
m_clicked = true;
}
QVERIFY(m_clicked);
}
推荐阅读
- python - 如何从python字典中删除一些值
- c - MSP432 LaunchPad 按钮初始化
- excel - 将验证列表复制到新书时如何保留另一张纸上的验证列表
- reactjs - 尝试使用 UseEffect 在 react.js 中创建倒计时(日:小时:分钟:秒)
- python - Pandas:如何在 Dataframe 中添加表示 2 个属性交集的列
- api - 在 DocuSign 中,信封状态报告(在控制台 -> 报告部分)我们得到一个字段总页数;如何通过 DocuSign API 获得相同的?
- node.js - 如何从前端保存和发送 JWT 令牌
- oauth-2.0 - 用于身份验证的 Onedrive/Azure API 代码流将我发送到我的重定向 url,但没有给我附加到 url 的代码
- php - Docker 上的 Nginx 仅提供欢迎页面
- r - 如何在绘图中添加线条和阴影