簡體   English   中英

如何獲取指向Qt中彈出對話框的指針,這會阻塞QTest中的UI線程

[英]How to grab pointer to popup dialog in Qt which blocks the UI thread in QTest

我正在使用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 = &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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM