How to run multiple parallel Qt Quick tests?

Qt Quick tests seem to require the test window to have focus in order to send mouse and keyboard events. Well, when I run only one test. But when I try to run multiple instances of Qt Quick tests, they fight for the focus of the window, and this leads to test failures.

How to run Qt Quick tests without focusing the test window? Is it possible to force Qt Quick to use fake mouse and keyboard events instead of working with a real window system? How to run multiple instances of Qt Quick tests without focus issues?

+4
source share
1 answer

Ok, I will bite.

++ QML:

class Dispatcher : public QObject {
    Q_OBJECT
public slots:
    void click(qreal x, qreal y) {
        foreach (QObject * o, objects) {
            QMouseEvent * e1 = new QMouseEvent(QEvent::MouseButtonPress, QPointF(x, y), Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier);
            QCoreApplication::postEvent(o, e1);
            QMouseEvent * e2 = new QMouseEvent(QEvent::MouseButtonRelease, QPointF(x, y), Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier);
            QCoreApplication::postEvent(o, e2);
        }
    }
    void addTarget(QObject * obj) {
        objects.append(obj);
    }
private:
    QObjectList objects;
};

QML QML, , , .

QML, , , :

MouseArea, . . , , , .

Window {
    id: main
    visible: true
    width: 300
    height: 300
    x: 100
    y: 100

    MouseArea {
        anchors.fill: parent
        onClicked:  Dispatcher.click(mouseX, mouseY)
    }

    Component {
        id: comp
        Item {
            anchors.fill: parent
            Text {
                id: txt
            }
            MouseArea {
                id: ma
                anchors.fill: parent
                onClicked: txt.text = "clicked at " + mouseX + ", " + mouseY
            }
        }
    }

    Window {
        id: w1
        visible: true
        width: 300
        height: 300
        x: 420
        y: 100
        Loader {
            anchors.fill: parent
            sourceComponent: comp
        }
        Component.onCompleted: Dispatcher.addTarget(w1)
    }

    Window {
        id: w2
        visible: true
        width: 300
        height: 300
        x: 740
        y: 100
        Loader {
            anchors.fill: parent
            sourceComponent: comp
        }
        Component.onCompleted: Dispatcher.addTarget(w2)
    }
}

, , , , . . . , ++ .

+3

All Articles