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 628 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 Wei Wei
    25 Jul 2023, 11:39

    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

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 25 Jul 2023, 13:33 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 26 Jul 2023, 02:04
    0
    • J jsulm
      25 Jul 2023, 13:33

      @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 26 Jul 2023, 02:04 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();
      
      S J 2 Replies Last reply 26 Jul 2023, 05:16
      0
      • W Wei Wei
        26 Jul 2023, 02:04

        @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();
        
        S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 26 Jul 2023, 05:16 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 26 Jul 2023, 06:18
        1
        • S sierdzio
          26 Jul 2023, 05:16

          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 26 Jul 2023, 06:18 last edited by
          #5

          @sierdzio got it, thank you

          1 Reply Last reply
          0
          • W Wei Wei
            26 Jul 2023, 02:04

            @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();
            
            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 26 Jul 2023, 06:46 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 26 Jul 2023, 12:54
            0
            • J jsulm
              26 Jul 2023, 06:46

              @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 26 Jul 2023, 12:54 last edited by
              #7

              @jsulm emmm,like this?

              if (event->target()->isWidgetType()) {
                  QWidget* targetWidget = qobject_cast<QWidget*>(event->target());
                  QWidget* window = targetWidget->window();
              }
              
              J 1 Reply Last reply 27 Jul 2023, 05:41
              0
              • W Wei Wei
                26 Jul 2023, 12:54

                @jsulm emmm,like this?

                if (event->target()->isWidgetType()) {
                    QWidget* targetWidget = qobject_cast<QWidget*>(event->target());
                    QWidget* window = targetWidget->window();
                }
                
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 27 Jul 2023, 05:41 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 27 Jul 2023, 06:39
                1
                • J jsulm
                  27 Jul 2023, 05:41

                  @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 27 Jul 2023, 06:39 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.

                  J 1 Reply Last reply 27 Jul 2023, 08:09
                  0
                  • W Wei Wei
                    27 Jul 2023, 06:39

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 27 Jul 2023, 08:09 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 27 Jul 2023, 08:50
                    0
                    • J JonB
                      27 Jul 2023, 08:09

                      @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 27 Jul 2023, 08:50 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 27 Jul 2023, 08:57 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 27 Jul 2023, 09:19

                        11/12

                        27 Jul 2023, 08:50

                        • Login

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