How to get the target window of QTouchEvent
-
In qt5 there is a QTouchEvent::window() method to get which QWindow this touch event will be sent to, in Qt6 this interface has been removed, what can I do to replace it?
I've tried using this way:
QObject* target = te->target(); QWindow* targetWindow; if (target->isWindowType()) targetWindow = dynamic_cast<QWindow*>(target); else { WId wid = static_cast<QWidget*>(target)->winId(); targetWindow = QWindow::fromWinId(wid); }
but I've found that the result isn't entirely accurate, and the first time I receive QTouchEvent, I get an error QWindow instead of the QWidgetWindow I expected
-
@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();
-
@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:
@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. -