Search code examples
qtqcomboboxmouseclick-eventqlistviewqtest

Qt's QTest doesn't select an item in a drop-down list with a click


There are two drop-downs. I'm trying to click on an item in a drop-down from a QComboBox.

For this purpose I created a function, launched for both drop-downs, here's an excerpt:

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
}

Now the flow looks like this:

  • First drop-down list is shown,
  • The item is highlighted (mouseClick) as chosen, but not really selected, not chosen
  • UI test goes to the second column, the first column shows the first element in the list (i.e., the upper-most), despite what was highlighted;
  • An item in the second drop-down is selected/chosen

So, the second list seems to be working with this function, the first one doesn't. I need these items to be selected, not just highlighted.

I run on Ubuntu 21.04. Seems to work well on Windows 10. Seems to fail on Mac. Any suggestions how to make it work?

What works: two clicks + Enter, but this will crash on MacOs. So that's not really a solution. Also, it destroys the dropDownList object, and if make with an interval, will lead to a crash.

What else doesn't work: key down N times.

Any suggestions?


Solution

  • I found the answer by myself.

    That's Qt's bug (as of Fall 2021), described here: https://bugreports.qt.io/browse/QTBUG-77772. The mouse clicked not the button, but some point nearby. Why it was selected and why it failed to react otherwise is still a mystery.

    Decision: I left this test to run on Windows and Linux only, closing it for Mac.