Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the target window of QTouchEvent
Forum Updated to NodeBB v4.3 + New Features

How to get the target window of QTouchEvent

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 627 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wei Wei
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • W Wei Wei

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      W 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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

        W Offline
        W Offline
        Wei Wei
        wrote on last edited by
        #3

        @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();
        
        sierdzioS jsulmJ 2 Replies Last reply
        0
        • W Wei Wei

          @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();
          
          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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.

          (Z(:^

          W 1 Reply Last reply
          1
          • sierdzioS sierdzio

            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.

            W Offline
            W Offline
            Wei Wei
            wrote on last edited by
            #5

            @sierdzio got it, thank you

            1 Reply Last reply
            0
            • W Wei Wei

              @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();
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              W 1 Reply Last reply
              0
              • jsulmJ jsulm

                @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...

                W Offline
                W Offline
                Wei Wei
                wrote on last edited by
                #7

                @jsulm emmm,like this?

                if (event->target()->isWidgetType()) {
                    QWidget* targetWidget = qobject_cast<QWidget*>(event->target());
                    QWidget* window = targetWidget->window();
                }
                
                jsulmJ 1 Reply Last reply
                0
                • W Wei Wei

                  @jsulm emmm,like this?

                  if (event->target()->isWidgetType()) {
                      QWidget* targetWidget = qobject_cast<QWidget*>(event->target());
                      QWidget* window = targetWidget->window();
                  }
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Wei-Wei Yes, but you also should always check pointers after casting. Casting can fail you then end up dereferencing an invalid pointer...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  W 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Wei-Wei Yes, but you also should always check pointers after casting. Casting can fail you then end up dereferencing an invalid pointer...

                    W Offline
                    W Offline
                    Wei Wei
                    wrote on last edited by
                    #9

                    @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.

                    JonBJ 1 Reply Last reply
                    0
                    • W Wei Wei

                      @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.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @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 a dynamic_cast<>() as a matter of course. Q_ASSERT(targetWidget); would suffice if you're sure it should be true.

                      W 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @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 a dynamic_cast<>() as a matter of course. Q_ASSERT(targetWidget); would suffice if you're sure it should be true.

                        W Offline
                        W Offline
                        Wei Wei
                        wrote on last edited by
                        #11

                        @JonB Check the return result of a qobject_cast<>() or a dynamic_cast<>() is a good habit, i will remember it.

                        1 Reply Last reply
                        1
                        • W Offline
                          W Offline
                          Wei Wei
                          wrote on last edited by
                          #12

                          I found QMutableEventPoint::window(const QEventPoint &) can solve my problem.

                          1 Reply Last reply
                          0
                          • W Wei Wei has marked this topic as solved on

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved