Press touch screen and hot plug the touch board, qt app(push button) cannot receive mousepress event, button click not work
-
@JoeCFD
Yes but I assumed (untested) thatQTouchEvent touchEvent = static_cast<QTouchEvent>(event);
would static-cast-except if it is not a
QTouchEvent
? Oh, I can't remember any more, doesn'tstatic_cast
throw ab exception wheredynamic_cast
would (for a pointer) returnnullptr
? Or isstatic_cast
just the dangerous C cast, no exception? Yeah, I'm beginning to think it's that.... -
@JonB
std::static_cast:
No runtime checks: If you downcast a pointer to a derived class incorrectly, it leads to undefined behavior.
Faster: No runtime overhead.std::dynamic_cast:
Performs a runtime check to ensure the cast is valid.
For pointers: nullptr if the cast fails.
For references: Throws std::bad_cast if the cast fails.
Involves runtime type information (RTTI) overhead.I think std::dynamic_cast is needed.
-
@JoeCFD
OK, I think I was confusing some "static cast" in C# which did "assert" if incorrect. Got it.I don't think you can use
dyanmic_cast
here becauseevent
is not a pointer here?dynamic_cast<QTouchEvent>(event)
would not be able to return its usualnullptr
if wrong type, so I don't see how you can here? -
@JoeCFD
Here is is copied verbatim from what OP posted in https://forum.qt.io/post/823816 above:bool MainWindow::event(QEvent *event) { QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
That is
static_cast<QTouchEvent>
, notstatic_cast<QTouchEvent *>
. I think I get it. OP did not use```
:( So the*
asterisk for pointer did italic instead of visible. That's the problem with no Code tags :( I get it now, misunderstanding. -
@JonB Hi,thanks for your idea.
My code is not safe. Should be:
bool MainWindow::event(QEvent *event)
{
if (event->type() == QEvent::TouchBegin ||
event->type() == QEvent::TouchUpdate ||
event->type() == QEvent::TouchEnd) {
QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
qDebug() << "New event:" << touchEvent->type();QObject *target = childAt(touchEvent->touchPoints().first().pos().toPoint()); qDebug() << "target: " << target; if (qobject_cast<QPushButton*>(target) || qobject_cast<QRadioButton*>(target) || qobject_cast<QComboBox*>(target) || qobject_cast<QSlider*>(target) || qobject_cast<QLineEdit*>(target) || qobject_cast<QLabel*>(target) || qobject_cast<QScrollBar*>(target)) { qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Include."; return QMainWindow::event(event); } qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Will return true."; return true;//if false, QPushButton will not be deliver } qDebug() << "----------------------------------------------------------------- Will goto last return."; return QMainWindow::event(event);
}
Do you have any idea about my problem? After hot plug touch board usb cable(finger is touching on screen at the same time), all the pushbuttons, sliders and so on, can not response. But if i hot plug the touch board usb cable without touching the touch screen, all is well. This problem always occurs on widget app. The QML app works well.
-
Based on the log you provided, the system correctly recognizes your touchscreen after reconnecting. However, Qt has detected the device as a touchscreen through libinput (event3). This means that Qt is handling touch events instead of mouse events.
You might want to try adding QCoreApplication::setAttribute(Qt::AA_SynthesizeMouseForUnhandledTouchEvents); in your main application to see if that resolves the issue.
-
@Kevin-Hoang said in Press touch screen and hot plug the touch board, qt app(push button) cannot receive mousepress event, button click not work:
orUnhandled
Hi Kevin Hoang, I did what you told me. But also not work.
My project is : https://github.com/hezonghua/linux_QT_touch_screen_widget_appI will appreciate if you can have a look at my project. Thanks for advance.
-
@Zonghua-He
Please use the forum posts' Code tags (the</>
button, or a line of```
above and below) when posting blocks of code if you wish to avoid the incorrect code you show and my misunderstanding about what you actually have.I am sorry but I do not know about your problem.
-
I really don’t have time to help you, but I noticed that when you unplug the screen, the TouchCancel event is triggered (TouchCancel also causes MouseButtonRelease to be sent to all widgets for processing). I suspect that during the TouchCancel process, something gets stuck, preventing proper event handling when you plug the screen back in.
Since the Qt version you’re using is quite old, I’m not sure if there’s any existing bug related to this.
You might try calling QApplication::processEvents() in the screenAdd event and see if that helps.
Also, before unplugging the screen (when everything is working fine), check whether the Input DeviceType is TouchScreen and compare if there are any differences between the initial state of the application and the state after hot-plugging.
-
@Kevin-Hoang Hi,i tried to call QApplication::processEvents() in the screenAdd event,i found that it has no help. It still does not work. Thanks for your suggestion.