Qt Touch Event Handling Issue: Positional Offset and Poor Finger Response on Resistive Screen
-
Hello Qt Community,
I am currently working on an embedded project using the Allwinner A133 processor with the following configuration:
Operating System: Ubuntu 18.04
Kernel: Linux 4.9
Qt Version: Qt 5.12.12 (Qt Widgets)
Touch Type: Resistive touchscreenFor touch detection, I am using the following implementation:
void MainWindow::enableTouchOnAllWidgets(QWidget *widget)
{
if (!widget) return;
widget->setAttribute(Qt::WA_AcceptTouchEvents);
foreach (QWidget child, widget->findChildren<QWidget>()) {
child->setAttribute(Qt::WA_AcceptTouchEvents);
}
}bool MainWindow::event(QEvent *event)
{
if (event->type() == QEvent::TouchBegin ||
event->type() == QEvent::TouchUpdate ||
event->type() == QEvent::TouchEnd)
{
QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
if (touchEvent->touchPoints().isEmpty()) {
event->accept();
return true;
}if (event->type() == QEvent::TouchBegin) { touchProcessing = false; } if (event->type() == QEvent::TouchEnd) { if (touchProcessing) { event->accept(); return true; } QPoint globalPos = touchEvent->touchPoints().first().screenPos().toPoint(); QWidget *widget = QApplication::widgetAt(globalPos); if (widget) { QWidget *current = widget; while (current) { QPushButton *button = qobject_cast<QPushButton*>(current); if (button && button->isEnabled() && button->isVisible()) { touchProcessing = true; emit button->clicked(); event->accept(); return true; } current = current->parentWidget(); } QPoint targetPos = widget->mapFromGlobal(globalPos); QMouseEvent press(QEvent::MouseButtonPress, targetPos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(widget, &press); QMouseEvent release(QEvent::MouseButtonRelease, targetPos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(widget, &release); touchProcessing = true; } } event->accept(); return true; } return QWidget::event(event);}
However, I am experiencing a positional offset issue with the touch input. For buttons located at the bottom of the display, the touch response is shifted approximately 1 cm downward—meaning the button only responds when I touch about 1 cm below its actual position. Similarly, for buttons on the right side, the touch response is shifted approximately 1 cm to the right. The same behavior is observed for other directions as well.Additionally, I am facing another issue with the resistive touchscreen: touch input using a fingernail is detected accurately, but touch input using a finger is inconsistent or unreliable.
Could you please help me understand the possible causes of these issues and suggest solutions to resolve them?
Thank you.
-
Hello Qt Community,
I am currently working on an embedded project using the Allwinner A133 processor with the following configuration:
Operating System: Ubuntu 18.04
Kernel: Linux 4.9
Qt Version: Qt 5.12.12 (Qt Widgets)
Touch Type: Resistive touchscreenFor touch detection, I am using the following implementation:
void MainWindow::enableTouchOnAllWidgets(QWidget *widget)
{
if (!widget) return;
widget->setAttribute(Qt::WA_AcceptTouchEvents);
foreach (QWidget child, widget->findChildren<QWidget>()) {
child->setAttribute(Qt::WA_AcceptTouchEvents);
}
}bool MainWindow::event(QEvent *event)
{
if (event->type() == QEvent::TouchBegin ||
event->type() == QEvent::TouchUpdate ||
event->type() == QEvent::TouchEnd)
{
QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
if (touchEvent->touchPoints().isEmpty()) {
event->accept();
return true;
}if (event->type() == QEvent::TouchBegin) { touchProcessing = false; } if (event->type() == QEvent::TouchEnd) { if (touchProcessing) { event->accept(); return true; } QPoint globalPos = touchEvent->touchPoints().first().screenPos().toPoint(); QWidget *widget = QApplication::widgetAt(globalPos); if (widget) { QWidget *current = widget; while (current) { QPushButton *button = qobject_cast<QPushButton*>(current); if (button && button->isEnabled() && button->isVisible()) { touchProcessing = true; emit button->clicked(); event->accept(); return true; } current = current->parentWidget(); } QPoint targetPos = widget->mapFromGlobal(globalPos); QMouseEvent press(QEvent::MouseButtonPress, targetPos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(widget, &press); QMouseEvent release(QEvent::MouseButtonRelease, targetPos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(widget, &release); touchProcessing = true; } } event->accept(); return true; } return QWidget::event(event);}
However, I am experiencing a positional offset issue with the touch input. For buttons located at the bottom of the display, the touch response is shifted approximately 1 cm downward—meaning the button only responds when I touch about 1 cm below its actual position. Similarly, for buttons on the right side, the touch response is shifted approximately 1 cm to the right. The same behavior is observed for other directions as well.Additionally, I am facing another issue with the resistive touchscreen: touch input using a fingernail is detected accurately, but touch input using a finger is inconsistent or unreliable.
Could you please help me understand the possible causes of these issues and suggest solutions to resolve them?
Thank you.
@Ramanujum Why are you doing all this? QPushButton already supports touch events.
-
In my case it is not working after this only my qt ui is reacting for touch event and added #include<QTouchEvent>
@Ramanujum Not sure I understand. You get touch events in your Qt app, right?