The interfaces can imitate users’ operations.

QTest::keyClick
QTest::keyPress
QTest::mouseClick

QSignalSpy listens for signal emission, it is a list of QVariant lists.
It will not shield off signal to make slot invalid.

  QCheckBox *box = ...;
  QSignalSpy spy(box, SIGNAL(clicked(bool)));
//...
  QList<QVariant> arguments = spy.takeFirst(); // take the first signal
  QVERIFY(arguments.at(0).toBool() == true); // verify the first argument

We can use installEventFilter to make mouse left button, right button, and middle button press invalid.
Example code:

class MousePressEater : public QObject
{
    Q_OBJECT
public:
    explicit MousePressEater(QObject *parent = nullptr);
protected:
    bool eventFilter(QObject *obj, QEvent *event) override;
};

bool MousePressEater::eventFilter(QObject *obj, QEvent *event)

{

    if (event->type() == QEvent::MouseButtonRelease) {

        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);

        qDebug("mouseEvent press %d", mouseEvent->button());

        return true;

    } else {

        // standard event processing

        return QObject::eventFilter(obj, event);

    }

}

//...

    pressEater = new MousePressEater (this);

    ui->pushButton->installEventFilter( pressEater );

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page