In our application, the touch screen is larger than the LCD screen, and touch buttons are set outside the LCD screen, as shown here:
However, Qt can only detect touch events within the LCD screen, and it cannot detect touch events outside of the LCD. We use QTouchEvent
to handle the touch.
#include <QApplication>
#include <QWidget>
#include <QDebug>
#include <QTouchEvent>
class TouchWidget : public QWidget {
public:
TouchWidget(QWidget *parent = nullptr) : QWidget(parent) {
setWindowTitle("Qt touch detect");
resize(380, 240);
setAttribute(Qt::WA_AcceptTouchEvents);
}
protected:
bool event(QEvent *event) override {
if (event->type() == QEvent::TouchBegin ||
event->type() == QEvent::TouchUpdate ||
event->type() == QEvent::TouchEnd) {
QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);
for (const QTouchEvent::TouchPoint &point : touchEvent->touchPoints()) {
qDebug() << "touch point:" << point.pos();
}
return true;
}
return QWidget::event(event);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QCoreApplication::setAttribute(Qt::AA_SynthesizeTouchForUnhandledMouseEvents);
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
TouchWidget window;
window.show();
return app.exec();
}
Is there any way to solve this problem?