Results 1 to 5 of 5

Thread: In Qt 5.10.1, why do I need to click twice on a QPushButton to get a release event?

  1. #1
    Join Date
    May 2018
    Posts
    3
    Qt products
    Qt5 Qt/Embedded

    Question In Qt 5.10.1, why do I need to click twice on a QPushButton to get a release event?

    I recently upgraded to Qt 5.10.1 from 5.2 and found that my QPushButtons required more than a single click for the release signal to be emitted. For a pared down example, consider:
    Qt Code:
    1. MyButton::MyButton(QWidget *parent) :
    2. QPushButton(parent)
    3. {
    4. }
    5.  
    6. void MyButton::mousePressEvent(QMouseEvent *event)
    7. {
    8. qDebug() << "MOUSE PRESS!";
    9. QPushButton::mousePressEvent(event);
    10. }
    11.  
    12. void MyButton::mouseReleaseEvent(QMouseEvent *event)
    13. {
    14. qDebug() << "MOUSE RELEASE!";
    15. QPushButton::mouseReleaseEvent(event);
    16. }
    17.  
    18. void MyButton::mouseMoveEvent(QMouseEvent *event)
    19. {
    20. qDebug() << "MOUSE MOVE!";
    21. QPushButton::mouseMoveEvent(event);
    22. }
    To copy to clipboard, switch view to plain text mode 

    Which after a single click, output was:

    Qt Code:
    1. MOUSE PRESS!
    2. MOUSE MOVE!
    3. MOUSE MOVE!
    4. MOUSE MOVE!
    5. MOUSE MOVE!
    6. MOUSE MOVE!
    7. MOUSE MOVE!
    To copy to clipboard, switch view to plain text mode 

    ... etc, and NO "MOUSE RELEASE!" For that, I had to click a second time!

    First, I tried adding in main.cpp on QApplication a:

    Qt Code:
    1. a.setAttribute(Qt::AA_SynthesizeMouseForUnhandledTouchEvents);
    To copy to clipboard, switch view to plain text mode 

    Secondly, at the widget level I tried:

    Qt Code:
    1. ui->pushButton->setAttribute(Qt::WA_AcceptTouchEvents);
    To copy to clipboard, switch view to plain text mode 

    Neither of which seemed to help.
    Now, in my production code, I found that for a QPushButton which opened a QDialog, I could trigger it on the "pressed" signal and after it's return, add:

    Qt Code:
    1. QMouseEvent event( QEvent::MouseButtonRelease, ui->pushButton->pos(), Qt::AllButtons, 0, 0 );
    2. QApplication::sendEvent( ui->pushButton, &event );
    To copy to clipboard, switch view to plain text mode 

    And that worked fine!
    However, for the QPushButtons in my QDialog, I still have the problem of the buttons not providing a "release" after a "click".

    Any insight would be greatly appreciated! (This is a cross-compiled Linux application on a device with a touch screen.)

  2. #2
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: In Qt 5.10.1, why do I need to click twice on a QPushButton to get a release even


  3. #3
    Join Date
    May 2018
    Posts
    3
    Qt products
    Qt5 Qt/Embedded

    Default Re: In Qt 5.10.1, why do I need to click twice on a QPushButton to get a release even

    Thank you for your response. I read that question and the answer to it was very close to the first example code I provided. There was no mention, however, of the release event NOT happening with a single "click".

  4. #4
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: In Qt 5.10.1, why do I need to click twice on a QPushButton to get a release even

    What about:-

    QAbstractButton Class
    Inherited By:
    QCheckBox, QPushButton, QRadioButton, and QToolButton


    [override virtual protected] void QAbstractButton::keyReleaseEvent(QKeyEvent *e)


    Added after 1 2 minutes:


    Quote Originally Posted by jimbo View Post
    What about:-

    QAbstractButton Class
    Inherited By:
    QCheckBox, QPushButton, QRadioButton, and QToolButton


    [override virtual protected] void QAbstractButton::keyReleaseEvent(QKeyEvent *e)
    Shou;d have said:-
    [override virtual protected] void QAbstractButton::mouseReleaseEvent(QMouseEvent *e)
    Reimplemented from QWidget::mouseReleaseEvent().
    Last edited by jimbo; 30th May 2018 at 22:13.

  5. #5
    Join Date
    May 2018
    Posts
    3
    Qt products
    Qt5 Qt/Embedded

    Default Re: In Qt 5.10.1, why do I need to click twice on a QPushButton to get a release even

    I found that installing an eventFilter on the QPushButton where I return false for Mouse Press/Release made it respond as expected!
    Qt Code:
    1. bool MyEvent::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (event->type() == QEvent::MouseButtonPress)
    4. {
    5. qDebug() << "MouseButtonPress";
    6. return false;
    7. }
    8. else if (event->type() == QEvent::MouseMove)
    9. {
    10. qDebug() << "MouseMove";
    11. return false;
    12. }
    13. else if (event->type() == QEvent::MouseButtonRelease)
    14. {
    15. qDebug() << "MouseButtonRelease";
    16. return false;
    17. }
    18. else
    19. {
    20. return QObject::eventFilter(obj, event);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    And added in MainWindow:
    Qt Code:
    1. auto myEvent = new MyEvent(this);
    2. ui->pushButton->installEventFilter(myEvent);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Event Filter Mouse Press/Release Event not called
    By Alundra in forum Qt Programming
    Replies: 3
    Last Post: 22nd September 2016, 00:59
  2. QPushButton mouse release event issue
    By Rajesh.Rathod in forum Qt Programming
    Replies: 3
    Last Post: 1st September 2016, 17:37
  3. qpushbutton slot not fired for mouse click event
    By dpn in forum Qt Programming
    Replies: 0
    Last Post: 22nd September 2013, 12:55
  4. Replies: 14
    Last Post: 17th January 2012, 09:01
  5. Round Icon shaped QPushButton Click event
    By kcsomisetty in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th June 2010, 06:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.