How to get the target window of QTouchEvent
-
@Wei-Wei You can use https://doc.qt.io/qt-6/qtouchevent.html#target and then check whether you can cast it to QWidget* if you can use https://doc.qt.io/qt-6/qwidget.html#window
@jsulm The return of https://doc.qt.io/qt-6/qwidget.html#window is a QWidget*, but i need a QWindow*
I find i can get the right window like this:
// target is a QObject* such as a event receiver QWidget* targetWidget = static_cast<QWidget*>(target); targetWindow = targetWidget->window()->windowHandle();
-
@jsulm The return of https://doc.qt.io/qt-6/qwidget.html#window is a QWidget*, but i need a QWindow*
I find i can get the right window like this:
// target is a QObject* such as a event receiver QWidget* targetWidget = static_cast<QWidget*>(target); targetWindow = targetWidget->window()->windowHandle();
Just a side-note to casting: in Qt world you can use
qobject_cast<>()
, it's as fast as static cast but as flexible as dynamic cast. Works only on subclasses of QObject. -
Just a side-note to casting: in Qt world you can use
qobject_cast<>()
, it's as fast as static cast but as flexible as dynamic cast. Works only on subclasses of QObject. -
@jsulm The return of https://doc.qt.io/qt-6/qwidget.html#window is a QWidget*, but i need a QWindow*
I find i can get the right window like this:
// target is a QObject* such as a event receiver QWidget* targetWidget = static_cast<QWidget*>(target); targetWindow = targetWidget->window()->windowHandle();
@Wei-Wei said in How to get the target window of QTouchEvent:
is a QWidget*, but i need a QWindow*
Please read again what I wrote...
-
@Wei-Wei said in How to get the target window of QTouchEvent:
is a QWidget*, but i need a QWindow*
Please read again what I wrote...
-
@jsulm emmm,like this?
if (event->target()->isWidgetType()) { QWidget* targetWidget = qobject_cast<QWidget*>(event->target()); QWidget* window = targetWidget->window(); }
-
@Wei-Wei Yes, but you also should always check pointers after casting. Casting can fail you then end up dereferencing an invalid pointer...
-
@jsulm After I checked that isWidgetType() is true, cast can still fail? Is a bug of Qt?
And thank you for the ideas, i find "QMutableEventPoint::window(const QEventPoint &)" that solved my problem.
@Wei-Wei said in How to get the target window of QTouchEvent:
@jsulm After I checked that isWidgetType() is true, cast can still fail? Is a bug of Qt?
Maybe, maybe not, But for the sake of an extra statement you should always check the return result of a
qobject_cast<>()
or adynamic_cast<>()
as a matter of course.Q_ASSERT(targetWidget);
would suffice if you're sure it should be true. -
@Wei-Wei said in How to get the target window of QTouchEvent:
@jsulm After I checked that isWidgetType() is true, cast can still fail? Is a bug of Qt?
Maybe, maybe not, But for the sake of an extra statement you should always check the return result of a
qobject_cast<>()
or adynamic_cast<>()
as a matter of course.Q_ASSERT(targetWidget);
would suffice if you're sure it should be true. -