ESD
ESD

Reputation: 544

QT not responding to touch event with external touch screen

I am trying to use an HMDI/USB touchscreen with a QT application on Ubuntu. I am able to use the touchscreen to navigate in the OS, but my QT application is completely unresponsive to the touch events. The application type is a Desktop QT 6.8.1 with a simple button: enter image description here

This is the content of my mainwindow.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_AcceptTouchEvents, true);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_pressed()
{
    qDebug() << "button pressed";
}

void MainWindow::on_pushButton_released()
{
    qDebug() << "button released";
}

bool MainWindow::event(QEvent* event)
{
    switch (event->type())
    {
    case QEvent::TouchBegin:
        qDebug() << "touch!";
        return true;
    case QEvent::TouchEnd:
        qDebug() << "touch end!";
        return true;
    case QEvent::MouseButtonDblClick:
        qDebug() << "double click";
        return true;
    case QEvent::MouseButtonPress:
        qDebug() << "pressed";
        return true;
    case QEvent::MouseButtonRelease:
        qDebug() << "released";
        return true;
    default:
        // call base implementation
        return QMainWindow::event(event);
    }
}

I am enabling touch events in the constructor. When I run the application, I can see the events debug messages when using the mouse. However, when using the touchscreen, none of the events are triggered. Not even the mouse ones!

Is there a reason why the touch event are not recognized?

Note: touch events are working completely fine on another UI framework (flutter).

Upvotes: 1

Views: 103

Answers (1)

Wisblade
Wisblade

Reputation: 1644

I had a similar problem (Qt5, however), and found out that accepting touch events wasn't recursively transmitted to children widgets...

So I wrote this function:

void AddTouchEventSupport ( QWidget* qw ) {
    qw->setAttribute(Qt::WA_AcceptTouchEvents, true) ;
    for ( auto&& w : qw->findChildren<QWidget*>(QString(),Qt::FindChildrenRecursively))
        w->setAttribute(Qt::WA_AcceptTouchEvents, true) ;
}

Replace your setAttribute(Qt::WA_AcceptTouchEvents, true); by AddTouchEventSupport(this); and test if it works.

Upvotes: 0

Related Questions