Unsolved mouseReleaseEvent not called when the mouse is released under certain circumstances
-
I had a radio button that was working unreliably with a trackpad. Clicking the trackpad or clicking the mouse always toggles the radio button. However, tapping the trackpad doesn't seem to work every time. So I wrote an MCVE:
#include <iostream> #include <QtGui/qpainter.h> #include <QtWidgets/qmainwindow.h> #include <QtWidgets/qapplication.h> #include <QtWidgets/qabstractbutton.h> class Button final : public QAbstractButton { public: explicit Button(QWidget *parent) : QAbstractButton{parent} { setToolTip("Tooltip"); setCheckable(true); setFixedSize(200, 200); } private: void paintEvent(QPaintEvent *) override { QPainter painter{this}; painter.fillRect(rect(), isChecked() ? Qt::red : Qt::blue); } void mousePressEvent(QMouseEvent *event) override { std::cout << "Down\n"; QAbstractButton::mousePressEvent(event); } void mouseReleaseEvent(QMouseEvent *event) override { std::cout << "Up\n"; QAbstractButton::mouseReleaseEvent(event); } }; int main(int argc, char **argv) { QApplication app{argc, argv}; QMainWindow window; window.setFixedSize(200, 200); Button button{&window}; window.show(); return app.exec(); }
I found two situations where only the mouse-press event is received so the radio isn't toggled (because it listens for mouse up). Clicking on the desktop (so that the window loses focus) then clicking on the button only triggers mouse-press. Hovering over the button and then clicking when the tooltip appears only triggers mouse-press. Note that by "click" I mean "tap the trackpad".
It seems odd to me that only one of the [mouse-press, mouse-release] pair is received. A workaround for this that works fine in my situation is this:
void mousePressEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { toggle(); } } void mouseReleaseEvent(QMouseEvent *) override {}
Is this just a quirk of the OS (macOS Mojave on a 2013 MacBook Air) or does this have something to do with Qt (5.12.3)?
-
@Kerndog73
The event is received.
By default, the QAbstractButton just perform the nextCheckState action after it is released and the release pos contain the Button Pos.mouseReleaseEvent() -> click() -> nextCheckState()
Note: -> means the order of calls.
You can confirm it on source code.
-
@KillerSmath I understand that QAbstractButton listens to mouseReleaseEvent (I mentioned that in my original post). What I am saying is that mouseReleaseEvent isn’t called under very specific circumstances.
-
Is anyone able to reproduce this on a mac?
-
@Kerndog73
Just tested it, can't reproduce it.
Up and Down come in all cases, with prior focus or without, with tooltip or without.Tested with trackpad and Magic Trackpad on macOS 10.14.4, 2015 MacBookPro
-
I'm running 10.14.3 so maybe this was a minor bug that was fixed in 10.14.4. I'll mark this thread as solved if the software update fixes the problem.
-
@J.Hilk Updating to 10.14.4 didn't solve the problem. It's frustrating that I seem to be the only one having this problem. Maybe we're not doing the same thing? In case I wasn't clear enough, I'm tapping the trackpad. I'm not clicking the diving board trackpad (not hearing an audible "click").
It looks like I'll have to keep using my workaround. Are there any problems with the workaround? Is there a better way?
-
@Kerndog73
oh,
do you have "tab for click" active ?
I do not, and I remember an other poster in this forum, reporting inconsistencies with tab to click on macOS and the later versions of Qt! -
@J.Hilk Yes, I have tap-to-click enabled.
(Damn! I love that window screenshot feature! The drop-shadow looks so cool!)
Do you have a link to the other thread where this problem was mentioned? It's not that big of a deal though. I don't really mind overriding
mousePressEvent
and callingtoggle
(or even just ignoring the problem). After reading the docs, I should probably callnextCheckState
instead. -
@Kerndog73 said in mouseReleaseEvent not called when the mouse is released under certain circumstances:
Do you have a link to the other thread where this problem was mentioned?
that took me an abyssal amount of time to dig up.
But here you gohttps://forum.qt.io/topic/99808/no-mouse-release-event-anymore-on-tap-macos-qt-5-12
-
Apologies for posting to an old thread, but I wanted to add a clue that might be helpful.
I am seeing this behavior in a Qt project of my own, but only on Mac laptops from 2015 and later (specifically, models with a force touch trackpad), when "Tap to Click" is enabled. On earlier laptops without a force touch trackpad, the bug does not present.