PDA

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



mangoByte
30th May 2018, 20:11
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:


MyButton::MyButton(QWidget *parent) :
QPushButton(parent)
{
}

void MyButton::mousePressEvent(QMouseEvent *event)
{
qDebug() << "MOUSE PRESS!";
QPushButton::mousePressEvent(event);
}

void MyButton::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << "MOUSE RELEASE!";
QPushButton::mouseReleaseEvent(event);
}

void MyButton::mouseMoveEvent(QMouseEvent *event)
{
qDebug() << "MOUSE MOVE!";
QPushButton::mouseMoveEvent(event);
}

Which after a single click, output was:



MOUSE PRESS!
MOUSE MOVE!
MOUSE MOVE!
MOUSE MOVE!
MOUSE MOVE!
MOUSE MOVE!
MOUSE MOVE!

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

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


a.setAttribute(Qt::AA_SynthesizeMouseForUnhandledT ouchEvents);

Secondly, at the widget level I tried:


ui->pushButton->setAttribute(Qt::WA_AcceptTouchEvents);

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:



QMouseEvent event( QEvent::MouseButtonRelease, ui->pushButton->pos(), Qt::AllButtons, 0, 0 );
QApplication::sendEvent( ui->pushButton, &event );

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.)

jimbo
30th May 2018, 20:33
Does this help:-
https://stackoverflow.com/questions/20722823/qt-get-mouse-pressed-event-even-if-a-button-is-pressed

mangoByte
30th May 2018, 21:19
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".

jimbo
30th May 2018, 23:13
What about:-

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

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

Added after 1 2 minutes:


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().

mangoByte
31st May 2018, 15:33
I found that installing an eventFilter on the QPushButton where I return false for Mouse Press/Release made it respond as expected!


bool MyEvent::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress)
{
qDebug() << "MouseButtonPress";
return false;
}
else if (event->type() == QEvent::MouseMove)
{
qDebug() << "MouseMove";
return false;
}
else if (event->type() == QEvent::MouseButtonRelease)
{
qDebug() << "MouseButtonRelease";
return false;
}
else
{
return QObject::eventFilter(obj, event);
}
}
And added in MainWindow:


auto myEvent = new MyEvent(this);
ui->pushButton->installEventFilter(myEvent);