camino
camino

Reputation: 10594

mouseclick/keyclick not work for delegate widget inside treeview in QTest framework

I use itemdelegate to create combobox for one column inside a treeview.

so when editing the cell, pressing UP/Down can change the value.

enter image description here

But it seems I cannot simulate the operations below using qtest framework

QTest::keyClick(treeview_->focusWidget(), Qt::Key_Down);
QTest::keyClick(treeview_->focusWidget(), Qt::Key_Down);
QTest::keyClick(treeview_->focusWidget(), Qt::Key_Enter);

//after these code. the value of the cell should be 3.03.

These codes above works well for a normal qt application.

But it doesn't work in qtest framework

if I try to test it using qtest framework, the value of the cell always is 1.01

class MyTest: public QObject
{
    Q_OBJECT
public:
    ...
    void tst_combo_column();
    ...
private:
};  

void MyTest::tst_combo_column()
{
    ...
    treeview_->setCurrentIndex(idx_combo);
    treeview_->edit(idx_combo);
    QTest::keyClick(treeview_->focusWidget(), Qt::Key_Down);
    QTest::keyClick(treeview_->focusWidget(), Qt::Key_Down);
    QTest::keyClick(treeview_->focusWidget(), Qt::Key_Enter);
    QVERIFY(GET_COL_VALUE("options") == "3.03"); //can not pass
} 

I also tried mouseclick, but got the same issue.

My environment Qt5.5, ubuntu14.10

Upvotes: 4

Views: 791

Answers (1)

maxschlepzig
maxschlepzig

Reputation: 39205

You can use QApplication::focusWindow() as target for the QTest::keyClick() calls.

For example, a test case that tests an item delegate that returns editors with autocompletion:

// application has a main window, where the main widget
// is a tree view
auto v = QApplication::focusWindow();
// go to a specific row
QTest::keyClick(v, Qt::Key_Down,  Qt::NoModifier, 10);
QTest::keyClick(v, Qt::Key_Down,  Qt::NoModifier, 10);
// start editing
QTest::keyClick(v, Qt::Key_F2,    Qt::NoModifier, 10);
// with nullptr, QTest does its own focus
// dependent window/widget selection
// the focusWindow() doesn't work here with keyClicks()
QTest::keyClicks(nullptr, "Rec",  Qt::NoModifier, 10);
// navigate the completion list popup
QTest::keyClick(v, Qt::Key_Down,  Qt::NoModifier, 10);
QTest::keyClick(v, Qt::Key_Down,  Qt::NoModifier, 10);
// select completion and finish editing
QTest::keyClick(v, Qt::Key_Enter, Qt::NoModifier, 10);
QTest::keyClick(v, Qt::Key_Enter, Qt::NoModifier, 10);
// verify that the change is stored in the model ...

Upvotes: 2

Related Questions