[Development] iOS Force Touch *almost* working

Jason H jhihn at gmx.com
Mon Oct 28 19:10:33 CET 2019


I am trying to get 3D Force Touch (finger pressure) working in my Qt App. I've been doing a lot of detective work and it almost works. I am going to share what I know to hopefully figure out what I am missing/what Qt is missing.

There seems to be some support in QTabletEvent (https://doc.qt.io/qt-5/qtabletevent.html) for the Apple Pencil. (I've not tested that)
But per this link and quote: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/Adopting3DTouchOniPhone/
"In iOS 9, the UITouch class has two new properties to support custom implementation of 3D Touch in your app: force and maximumPossibleForce. For the first time on iOS devices, these properties let you detect and respond to touch pressure in the UIEvent objects your app receives."

And, in the Apple Pencil support change: https://codereview.qt-project.org/c/qt/qtbase/+/193134/9/src/plugins/platforms/ios/quiview.mm
We see the calculation I'd expect: touchPoint.pressure = uiTouch.force / uiTouch.maximumPossibleForce;

So I made a simple app with QQuickItem subclass and override the event() with a static cast to QTabletEvent
///
class TabletItem : public QQuickItem
		/* QQuickItem - QuickItem does not have tabletEvent()
		 * QQuickWidget - QuickItem does not have tabletEvent() */
{
	Q_OBJECT

public:
	TabletItem() {}

	bool event(QEvent *event) override  {
		QTabletEvent *tabEvent = static_cast<QTabletEvent *>(event);
		if (tabEvent) {
			if (event->type() == QEvent::TabletPress) {
				qDebug() << "begin" << tabEvent->x() << tabEvent->y() << tabEvent->pressure();
			} else if (event->type() == QEvent::TabletMove) {
				qDebug() << "update" << tabEvent->x() << tabEvent->y() << tabEvent->pressure();
			} else if (event->type() == QEvent::TabletRelease) {
				qDebug() << "end" << tabEvent->x() << tabEvent->y() << tabEvent->pressure();
			}
			qDebug() << "pressure" << tabEvent->pressure();
			pressureChanged(tabEvent->pressure());
			return true;
		}
		return false;
	}

signals:
	void pressureChanged(double pressure);
};

////
However at run time, here's what I get:
pressure 5.3141e-314
pressure 5.31406e-314
pressure nan
pressure 4.2483e-314
pressure 5.3141e-314
pressure 4.2483e-314
pressure 1.00938e-320
pressure 0
pressure 4.25599e-314
pressure 5.31408e-314
pressure 0
pressure 5.31406e-314
pressure 0
pressure 5.31406e-314
pressure 4.2483e-314
pressure 5.31406e-314
pressure 4.2483e-314
pressure 5.31406e-314
pressure 4.2483e-314
pressure 0

They are all 0, near zero, or division by zero. This seems so close to actually working. Does anyone know what might be wrong? I think maybe it's because it's looking for a pencil touch specifically. It seems that supportsPressure should be true for all iOS devices?




More information about the Development mailing list