首页 > 解决方案 > Qt的QTest不会通过单击选择下拉列表中的项目

问题描述

有两个下拉菜单。我正在尝试单击 QComboBox 下拉列表中的项目。

为此,我创建了一个为两个下拉菜单启动的函数,以下是摘录:

constexpr int DELAY{ 1000 };

void clickDropDown(int row, QComboBox *comboBox)
    {
    QListView *dropDownList = comboBox->findChild<QListView *>();    
    QModelIndex foundIndex{ dropDownList->model()->index(row, 0) };

    QRect foundDropDownItem = dropDownList->visualRect(foundIndex);
    QPoint foundDropDownItemPosition = foundDropDownItem.center();

    QWidget *activeWidget = dropDownList->viewport();   
    QTest::mouseClick(activeWidget, Qt::LeftButton, Qt::NoModifier, foundDropDownItemPosition);
    QTest::qWait(DELAY); // waits 1 second
}

现在流程看起来像这样:

所以,第二个列表似乎可以使用这个函数,第一个没有。我需要选择这些项目,而不仅仅是突出显示。

我在Ubuntu 21.04上运行。似乎在Windows 10上运行良好。在 Mac 上似乎失败了。任何建议如何使它工作?

什么有效: two clicks + Enter,但这会在 MacOs 上崩溃。所以这不是一个真正的解决方案。此外,它会破坏dropDownList对象,如果 make 有间隔,将导致崩溃。

还有什么不起作用: key down N次。

有什么建议么?

标签: qtqcomboboxmouseclick-eventqlistviewqtest

解决方案


我自己找到了答案。

这是 Qt 的错误(截至 2021 年秋季),如下所述: https ://bugreports.qt.io/browse/QTBUG-77772 。鼠标点击的不是按钮,而是附近的某个点。为什么选择它以及为什么它没有做出其他反应仍然是一个谜。

决定:我让这个测试只在 Windows 和 Linux 上运行,为 Mac 关闭它。


推荐阅读