QTest::mouseClick and QCheckBox

  • Hi,

    I'm brand new to the Qt Unit Test Framework, and having some trouble using the QTest::mouseClick() method with a QCheckBox as the widget to "click".

    Seems when this is invoked, the "click" isn't happening on the check box. Any ideas? The code below outputs:

    Initial checkbox state: 0
    State after click: 0


    @void testMethod()
    QCheckBox tmp;

    qDebug() << "Initial checkbox state: " << tmp.checkState();

    QTest::mouseClick( &tmp, Qt::LeftButton );

    qDebug() << "State after click: " << tmp.checkState();

  • Your widget will not listen for mouse events if it's hidden =)

    try this:
    void testMethod()
    QCheckBox tmp;

    qDebug() << "Initial checkbox state: " << tmp.checkState();

    QTest::mouseClick( &tmp, Qt::LeftButton );

    qDebug() << "State after click: " << tmp.checkState();

    It worked here.

  • @./newtest
    Initial checkbox state: 0
    State after click: 2 @

  • Wonderful! That works, thank you.

    I'm a bit confused though, because I have a different test method I worked on before this, which uses a QPushButton instead of a QCheckBox. Same sample code as above, but replace "QCheckBox" with "QPushButton". That mouse click occurred even though I didn't ".show()" the QPushButton.


  • Man, I believe you found a bug in Qt...

    I made some tests, it happens because of the way how QPushButton and QCheckBox implements its hitButton method.

    QPushButton uses QAbstractButton::hitButton(), which returns true even if the element is invisible.

    However, QCheckBox implements its own hitButton method, using an strange QStyleOptionButton::subElementRect() method, which returns false when it's not visible.

    My final judgment: call show() method always as possible.

  • Thanks for the info, and thanks for doing the tests to prove it.

    So how do we move forward submitting the bug report? This does seem like reasonably inconsistent behavior, or at the very least needs a QTest documentation update.

    Are you a developer for Qt?

  • You need to file a bug report "here":http://bugreports.qt.nokia.com, can you do it?

    And no, I'm not. Nokia/Trolltech employees have a green "TROLL" badge. =)

  • Moderators

    jimpacyga: JUst head over to "our bugtracker":http://bugreports.qt.nokia.com/, create an account and report your bug.

    You need to create an account since we want to be able to contact a bug reporter about status updates, questions, etc.

  • Will do. Thanks to both of you.


  • Another consequence of this is that QMouseClick does not seem to trigger the checkbox if its orientation is set RightToLeft (i.e. if you want the label to be before the box rather than after it.)

    Certainly, in my code this seems to be the case. I had to specify the location of the click to be 3/4 of the way across the check box's rectangle in order for it to "hit" the actual check part of the box.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.