Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to mapToGlobal a QRect?

How to mapToGlobal a QRect?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
15 Posts 4 Posters 7.1k Views
  • 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.
  • S Offline
    S Offline
    Sriu1
    wrote on last edited by Sriu1
    #1

    I have a QWidget (Width-500,500) inside which I have 2 child QWidgets.The 2 child QWidgets have different geometry's.One having ( 0,0,100,100) other at (200,0,100,100).I want to check if a QPoint lies inside these child widgets. I have tried this

    QPoint p1;//which is inside 1st child
    QPoint p2;//which is inside 2nd child
    rectChildWidget1.contains(p1) and rectChildWidget2.contains(p2)

    But rectChildWidget2.contains(p2) is always getting false , rectChildWidget1.contains(p1) is working fine(If the point goes outside its returning false).I tried mapping cordinates of rectChildWidget2 to global with setX and setY's. No good.Please suggest how to map the rects to global so that rect.contains(point) works fine no matter where the rect is.....

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How is p1 and p2 created ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        How is p1 and p2 created ?

        S Offline
        S Offline
        Sriu1
        wrote on last edited by
        #3

        @SGaist Hi,
        I'm getting it from
        mouseMoveEvent(QMouseEvent* event)
        event->pos()

        1 Reply Last reply
        0
        • M Offline
          M Offline
          medyakovvit
          wrote on last edited by
          #4

          @Sriu1 said in How to mapToGlobal a QRect?:

          mouseMoveEvent

          is it parent's mouseMoveEvent?

          S 1 Reply Last reply
          0
          • M medyakovvit

            @Sriu1 said in How to mapToGlobal a QRect?:

            mouseMoveEvent

            is it parent's mouseMoveEvent?

            S Offline
            S Offline
            Sriu1
            wrote on last edited by
            #5

            @medyakovvit
            No its child's

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you show the complete code ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              0
              • SGaistS SGaist

                Can you show the complete code ?

                S Offline
                S Offline
                Sriu1
                wrote on last edited by Sriu1
                #7

                @SGaist

                void ChildWidget::mouseMoveEvent(QMouseEvent* event)
                {

                if (event->buttons() & Qt::LeftButton)
                {
                QRect r=this->geometry();

                   if(r.contains(event->globalPos()))//Also tried event->pos()
                    {
                        qDebug()<<"point inside";
                    }
                    else
                    {
                      qDebug()<<"point outside";
                    }
                

                }

                QWidget::mouseMoveEvent(event);

                }

                If the child widget starts at 0,0 works fine...If child widget is not at origin "point outside" will be printed even if the pos is inside.Tried making point global.

                J.HilkJ 1 Reply Last reply
                0
                • S Sriu1

                  @SGaist

                  void ChildWidget::mouseMoveEvent(QMouseEvent* event)
                  {

                  if (event->buttons() & Qt::LeftButton)
                  {
                  QRect r=this->geometry();

                     if(r.contains(event->globalPos()))//Also tried event->pos()
                      {
                          qDebug()<<"point inside";
                      }
                      else
                      {
                        qDebug()<<"point outside";
                      }
                  

                  }

                  QWidget::mouseMoveEvent(event);

                  }

                  If the child widget starts at 0,0 works fine...If child widget is not at origin "point outside" will be printed even if the pos is inside.Tried making point global.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Sriu1 said in How to mapToGlobal a QRect?:

                  If the child widget starts at 0,0 works fine...If child widget is not at origin "point outside" will be printed even if the pos is inside.Tried making point global.

                  It's been a while since I used this, but mapToGlobal should work just fine!?

                  > void ChildWidget::mouseMoveEvent(QMouseEvent* event)
                   {
                   
                     if (event->buttons() & Qt::LeftButton)
                     {
                          qDebug() << "Position relative to ChildWidget:" << objectName() << event->pos();
                          qDebug() << "Position relative to physical screen:" << objectName() << mapToGlobal(event->pos());
                     }
                  
                   }
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  S 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @Sriu1 said in How to mapToGlobal a QRect?:

                    If the child widget starts at 0,0 works fine...If child widget is not at origin "point outside" will be printed even if the pos is inside.Tried making point global.

                    It's been a while since I used this, but mapToGlobal should work just fine!?

                    > void ChildWidget::mouseMoveEvent(QMouseEvent* event)
                     {
                     
                       if (event->buttons() & Qt::LeftButton)
                       {
                            qDebug() << "Position relative to ChildWidget:" << objectName() << event->pos();
                            qDebug() << "Position relative to physical screen:" << objectName() << mapToGlobal(event->pos());
                       }
                    
                     }
                    
                    S Offline
                    S Offline
                    Sriu1
                    wrote on last edited by
                    #9

                    @J.Hilk Hi,
                    event()->globalPos() and mapToGlobal(event->pos()) both give same.My problem was with rect.contains().how to map the rect of childWidget to global? So that even if event is occuring anywhere in the screen I should be able to know if its inside the qrect or outside

                    J.HilkJ 1 Reply Last reply
                    0
                    • S Sriu1

                      @J.Hilk Hi,
                      event()->globalPos() and mapToGlobal(event->pos()) both give same.My problem was with rect.contains().how to map the rect of childWidget to global? So that even if event is occuring anywhere in the screen I should be able to know if its inside the qrect or outside

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by J.Hilk
                      #10

                      @Sriu1 said in How to mapToGlobal a QRect?:

                      @J.Hilk Hi,
                      event()->globalPos() and mapToGlobal(event->pos()) both give same.My problem was with rect.contains().how to map the rect of childWidget to global? So that even if event is occuring anywhere in the screen I should be able to know if its inside the qrect or outside

                      Well, the same way.

                      QRect r=this->geometry();
                      QRect globalRect(mapToGlobal( r.pos() ), r.size() );
                      

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      S 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @Sriu1 said in How to mapToGlobal a QRect?:

                        @J.Hilk Hi,
                        event()->globalPos() and mapToGlobal(event->pos()) both give same.My problem was with rect.contains().how to map the rect of childWidget to global? So that even if event is occuring anywhere in the screen I should be able to know if its inside the qrect or outside

                        Well, the same way.

                        QRect r=this->geometry();
                        QRect globalRect(mapToGlobal( r.pos() ), r.size() );
                        
                        S Offline
                        S Offline
                        Sriu1
                        wrote on last edited by Sriu1
                        #11

                        @J.Hilk Hi,
                        I tried this
                        QRect r=this->geometry();
                        QPoint p(r.x(),r.y());
                        QRect globalRect(mapToGlobal(p, r.size());

                        getting error
                        no matching function for call to ‘ChildWidget::mapToGlobal(QPoint&, QSize)’
                        QRect globalRect(mapToGlobal(p, r.size());

                        J.HilkJ 1 Reply Last reply
                        0
                        • S Sriu1

                          @J.Hilk Hi,
                          I tried this
                          QRect r=this->geometry();
                          QPoint p(r.x(),r.y());
                          QRect globalRect(mapToGlobal(p, r.size());

                          getting error
                          no matching function for call to ‘ChildWidget::mapToGlobal(QPoint&, QSize)’
                          QRect globalRect(mapToGlobal(p, r.size());

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @Sriu1 there was a )missing in my example,


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          S 1 Reply Last reply
                          0
                          • J.HilkJ J.Hilk

                            @Sriu1 there was a )missing in my example,

                            S Offline
                            S Offline
                            Sriu1
                            wrote on last edited by Sriu1
                            #13

                            @J.Hilk
                            Still getting....
                            error: no matching function for call to ‘ChildWidget::mapToGlobal(QPoint&, QSize)’
                            QRect globalRect(mapToGlobal(p, r.size()));

                            QRect r=this->geometry();
                            QPoint p(r.x(),r.y());
                            QRect globalRect(mapToGlobal(p, r.size()));

                            if(!r.contains(event->globalPos()))

                            J.HilkJ 1 Reply Last reply
                            0
                            • S Sriu1

                              @J.Hilk
                              Still getting....
                              error: no matching function for call to ‘ChildWidget::mapToGlobal(QPoint&, QSize)’
                              QRect globalRect(mapToGlobal(p, r.size()));

                              QRect r=this->geometry();
                              QPoint p(r.x(),r.y());
                              QRect globalRect(mapToGlobal(p, r.size()));

                              if(!r.contains(event->globalPos()))

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #14

                              @Sriu1 again,
                              look at the parenthesis, mapToGlobal expects 1 argument, a Qpoint, and returns the maped point, you try to pass it a Qpoint and QSize!


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              1
                              • S Offline
                                S Offline
                                Sriu1
                                wrote on last edited by
                                #15

                                @J-Hilk
                                Compiled.. But still I'm getting "point outside" event if its inside.

                                1 Reply Last reply
                                0

                                • Login

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