Understanding Deprecated Code: Alternatives to QEventPoint::normalizedPosition() in Qt
What it did
In older Qt versions (pre-6.0), QEventPoint::normalizedPosition()
provided a way to obtain the position of a touch event point relative to the widget or window that received the event. This position was normalized to a range of 0.0 to 1.0 for both the X and Y coordinates.
- 1.0 represented the bottom-right corner.
- 0.0 represented the top-left corner of the widget/window.
This normalization could be useful for gesture recognition or scaling touch input based on the widget's size.
Why it's deprecated
The primary reason for deprecating normalizedPosition()
is the introduction of per-point handling in Qt Quick. In Qt Quick applications, it's more common to work with the individual touch points directly within an Item
or EventHandler
. This allows for finer-grained control over touch interactions.
Alternatives
Here are some recommended approaches for handling touch events in Qt, depending on your scenario:
- Qt Quick
- Access touch points directly within an
Item
orEventHandler
using properties likeTouchEvent.touchPoints
or specific touch point methods. This provides more control over individual touch interactions.
- Access touch points directly within an
- Qt Widgets
- Use
QTouchEvent::globalPos()
orQTouchEvent::pos()
to get the position of a touch point in global or widget-relative coordinates, respectively. You can then calculate the normalized position yourself if needed.
- Use
Example (Qt Widgets)
void MyWidget::mousePressEvent(QMouseEvent *event) {
QPointF globalPos = event->globalPos();
QPointF widgetPos = event->pos();
// Calculate normalized position if needed
qreal normalizedX = widgetPos.x() / width();
qreal normalizedY = widgetPos.y() / height();
// Handle touch event based on position
}
Qt Widgets (Using globalPos() and pos())
#include <QtWidgets>
class MyWidget : public QWidget {
Q_OBJECT
public:
MyWidget(QWidget *parent = nullptr) : QWidget(parent) {}
protected:
void mousePressEvent(QMouseEvent *event) override {
if (event->button() == Qt::LeftButton) {
// Get global position for absolute reference
QPointF globalPos = event->globalPos();
// Get position relative to widget
QPointF widgetPos = event->pos();
// Handle touch event based on position
qDebug() << "Touch at global position:" << globalPos;
qDebug() << "Touch relative to widget:" << widgetPos;
}
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
Qt Quick (Using TouchEvent.touchPoints)
import QtQuick 2.15
Item {
width: 400; height: 300
Text {
id: touchText; text: ""
anchors.centerIn: parent
}
onTouchEvent: {
touchText.text = "Touch points:" + touchPoints.length
for (var i = 0; i < touchPoints.length; ++i) {
var touchPoint = touchPoints[i]
console.log("Touch point " + (i + 1) + ": position = (" + touchPoint.x + ", " + touchPoint.y + ")")
}
}
}
This example demonstrates handling multiple touch points using a loop and accessing individual touch point properties like x
and y
.
Qt Widgets
- QTouchEvent::pos()
This method returns the touch point's position relative to the widget that received the event. - QTouchEvent::globalPos()
This method returns the touch point's position in global coordinates (relative to the entire screen).
Example
void MyWidget::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
QPointF globalPos = event->globalPos();
QPointF widgetPos = event->pos();
// Handle touch event based on position (global or widget-relative)
qDebug() << "Touch at global position:" << globalPos;
qDebug() << "Touch relative to widget:" << widgetPos;
}
}
Qt Quick
In Qt Quick, you can access touch point information directly within an Item
or EventHandler
using properties and methods:
- Touch point methods
EachTouchPoint
object has properties likex
,y
,pressure
, and others that provide detailed information about the touch event. - TouchEvent.touchPoints
This property provides a list ofTouchPoint
objects, each representing a single touch point.
Example
import QtQuick 2.15
Item {
width: 400; height: 300
Text {
id: touchText; text: ""
anchors.centerIn: parent
}
onTouchEvent: {
touchText.text = "Touch points:" + touchPoints.length
for (var i = 0; i < touchPoints.length; ++i) {
var touchPoint = touchPoints[i]
console.log("Touch point " + (i + 1) + ": position = (" + touchPoint.x + ", " + touchPoint.y + ")")
}
}
}
This approach offers more granular control over individual touch interactions compared to the normalized position provided by QEventPoint::normalizedPosition()
.
- Use
TouchEvent.touchPoints
and touch point methods in Qt Quick for handling multiple touch points and accessing detailed touch information. - Use
globalPos()
orpos()
in Qt Widgets if you need the absolute or widget-relative position for a single touch event.