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. Unwanted vertical offset with with QRubberBand
Forum Updated to NodeBB v4.3 + New Features

Unwanted vertical offset with with QRubberBand

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 3 Posters 5.5k 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.
  • B Offline
    B Offline
    Bidski
    wrote on last edited by
    #1

    I am trying to draw a QRubberBand around a portion of an image (which I am rendering in a QLabel). I also want to clip the rubber band to the borders of the QLabel.

    Unfortunately, I keep getting a negative vertical offset on the QRubberBand that forces it to leave the top border of the QLabel (see image) and makes it unable to reach the bottom border of the QLabel. There appears to be no horizontal offset, just a vertical one.

    Can anyone tell me what is happening here and how to go about fixing it?

    Regards,
    Bidski

    QRubberBand leaving top of QLabel geometry

    mrjjM 1 Reply Last reply
    0
    • B Bidski

      I am trying to draw a QRubberBand around a portion of an image (which I am rendering in a QLabel). I also want to clip the rubber band to the borders of the QLabel.

      Unfortunately, I keep getting a negative vertical offset on the QRubberBand that forces it to leave the top border of the QLabel (see image) and makes it unable to reach the bottom border of the QLabel. There appears to be no horizontal offset, just a vertical one.

      Can anyone tell me what is happening here and how to go about fixing it?

      Regards,
      Bidski

      QRubberBand leaving top of QLabel geometry

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Bidski
      Hi
      If you move your main window more from top/ side , will this offset grow?
      Im think about global / local coordinates.

      B 1 Reply Last reply
      0
      • mrjjM mrjj

        @Bidski
        Hi
        If you move your main window more from top/ side , will this offset grow?
        Im think about global / local coordinates.

        B Offline
        B Offline
        Bidski
        wrote on last edited by
        #3

        @mrjj said in Unwanted vertical offset with with QRubberBand:

        @Bidski
        Hi
        If you move your main window more from top/ side , will this offset grow?
        Im think about global / local coordinates.

        I am not entirely sure if I understand what you mean? More from the top/side? As in unmaximising the window and moving it around the screen?
        If so, then no, the offset seems to be constant.

        I guess I should have mentioned originally, the code you see in the image is actually the mouse event code I am using to control the rubber band.

        Regards,
        Bidski

        mrjjM 1 Reply Last reply
        0
        • B Bidski

          @mrjj said in Unwanted vertical offset with with QRubberBand:

          @Bidski
          Hi
          If you move your main window more from top/ side , will this offset grow?
          Im think about global / local coordinates.

          I am not entirely sure if I understand what you mean? More from the top/side? As in unmaximising the window and moving it around the screen?
          If so, then no, the offset seems to be constant.

          I guess I should have mentioned originally, the code you see in the image is actually the mouse event code I am using to control the rubber band.

          Regards,
          Bidski

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Bidski

          Hi

          • As in unmaximising the window and moving it around the screen?
            yes and then grab the wanted area.

          But if the offset is constant, then its something else.

          B 1 Reply Last reply
          0
          • mrjjM mrjj

            @Bidski

            Hi

            • As in unmaximising the window and moving it around the screen?
              yes and then grab the wanted area.

            But if the offset is constant, then its something else.

            B Offline
            B Offline
            Bidski
            wrote on last edited by
            #5

            @mrjj said in Unwanted vertical offset with with QRubberBand:

            @Bidski

            Hi

            • As in unmaximising the window and moving it around the screen?
              yes and then grab the wanted area.

            But if the offset is constant, then its something else.

            To be sure that there isnt something else in my window that may be having an affect, here is a screenshot of the entire window.
            Full Window

            mrjjM 1 Reply Last reply
            0
            • B Bidski

              @mrjj said in Unwanted vertical offset with with QRubberBand:

              @Bidski

              Hi

              • As in unmaximising the window and moving it around the screen?
                yes and then grab the wanted area.

              But if the offset is constant, then its something else.

              To be sure that there isnt something else in my window that may be having an affect, here is a screenshot of the entire window.
              Full Window

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @Bidski
              well from fast read- it looks and seems ok.
              If this on windows 10, it might be a bug.
              We had questions of incorrect frame thickness. 7 and not 1.
              Not sure this is related.
              You code looks fine. Try debug it and look at the values.

              B 1 Reply Last reply
              0
              • mrjjM mrjj

                @Bidski
                well from fast read- it looks and seems ok.
                If this on windows 10, it might be a bug.
                We had questions of incorrect frame thickness. 7 and not 1.
                Not sure this is related.
                You code looks fine. Try debug it and look at the values.

                B Offline
                B Offline
                Bidski
                wrote on last edited by
                #7

                @mrjj said in Unwanted vertical offset with with QRubberBand:

                @Bidski
                well from fast read- it looks and seems ok.
                If this on windows 10, it might be a bug.
                We had questions of incorrect frame thickness. 7 and not 1.
                Not sure this is related.
                You code looks fine. Try debug it and look at the values.

                This is actually on linux (Arch Linux to be precise) and Gnome 3 desktop environment and Qt5.8.

                I will get to debugging and dumping some values tomorrow and get back to you.

                mrjjM 1 Reply Last reply
                1
                • B Bidski

                  @mrjj said in Unwanted vertical offset with with QRubberBand:

                  @Bidski
                  well from fast read- it looks and seems ok.
                  If this on windows 10, it might be a bug.
                  We had questions of incorrect frame thickness. 7 and not 1.
                  Not sure this is related.
                  You code looks fine. Try debug it and look at the values.

                  This is actually on linux (Arch Linux to be precise) and Gnome 3 desktop environment and Qt5.8.

                  I will get to debugging and dumping some values tomorrow and get back to you.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Bidski
                  Super.
                  Also if you can upload the small? project and give link here then
                  its just to run and see for oneself also.

                  B 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @Bidski
                    Super.
                    Also if you can upload the small? project and give link here then
                    its just to run and see for oneself also.

                    B Offline
                    B Offline
                    Bidski
                    wrote on last edited by
                    #9

                    @mrjj said in Unwanted vertical offset with with QRubberBand:

                    @Bidski
                    Super.
                    Also if you can upload the small? project and give link here then
                    its just to run and see for oneself also.

                    Ok, I have dumped some quick values.
                    The (x, y, width, height) of the QLabel geometry is reported as (188, 9, 613, 512)
                    When the QRubberBand is stretched above the QLabel (as shown in the original image), its geometry is reported as (335, 9, 62, 175)
                    When attempting to stretch the QRubberBand down to the bottom of the QLabel, the extent of its geometry reaches (340, 364, 272, 157)

                    The black border of the QLabel (where the QRubberBand should be stopping) has top coordinate of 31 and a bottom coordinate of 544.
                    Based on this, the QRubberBand is overshooting the top by 22 pixels and undershooting the bottom by 22 pixels.

                    These measurements were taken with the window in a non-maximised state. When maximised, the offset remains the same.

                    I will organise you a link to the code shortly

                    1 Reply Last reply
                    1
                    • B Offline
                      B Offline
                      Bidski
                      wrote on last edited by
                      #10

                      Hopefully you should be able to view this repo.
                      Bitbucket Repo

                      mrjjM 1 Reply Last reply
                      0
                      • B Bidski

                        Hopefully you should be able to view this repo.
                        Bitbucket Repo

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #11

                        @Bidski
                        It still sounds as difference between client coordinates and global coordinates :)
                        Like the height of the caption.

                        It will let me into
                        https://bitbucket.org/Bidski/imageannotator/src

                        but i cant seem to find the download as zip ?
                        I guess bitbucket do not offer such option. :)

                        Called Downloads :) ( doh)

                        B 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @Bidski
                          It still sounds as difference between client coordinates and global coordinates :)
                          Like the height of the caption.

                          It will let me into
                          https://bitbucket.org/Bidski/imageannotator/src

                          but i cant seem to find the download as zip ?
                          I guess bitbucket do not offer such option. :)

                          Called Downloads :) ( doh)

                          B Offline
                          B Offline
                          Bidski
                          wrote on last edited by
                          #12

                          @mrjj said in Unwanted vertical offset with with QRubberBand:

                          @Bidski
                          It still sounds as difference between client coordinates and global coordinates :)
                          Like the height of the caption.

                          What is the correct way to handle the difference in coordinate systems?

                          J.HilkJ mrjjM 2 Replies Last reply
                          0
                          • B Bidski

                            @mrjj said in Unwanted vertical offset with with QRubberBand:

                            @Bidski
                            It still sounds as difference between client coordinates and global coordinates :)
                            Like the height of the caption.

                            What is the correct way to handle the difference in coordinate systems?

                            J.HilkJ Online
                            J.HilkJ Online
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #13

                            @Bidski

                            for some reason, the QMenuBar is the problem. You can verify this, if you add

                            ui->menubar->hide();
                            

                            in your constructor.

                            No Offset with that.

                            Not sure why thats the case, but now we can narrow it down :)


                            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.

                            B 1 Reply Last reply
                            1
                            • B Bidski

                              @mrjj said in Unwanted vertical offset with with QRubberBand:

                              @Bidski
                              It still sounds as difference between client coordinates and global coordinates :)
                              Like the height of the caption.

                              What is the correct way to handle the difference in coordinate systems?

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #14

                              @Bidski

                              Hi
                              Its these functions.
                              http://doc.qt.io/qt-5/qwidget.html#mapFromGlobal
                              QPoint QWidget::mapToParent(const QPoint &pos) const

                              I didnt get to run it ( will tonight) but my best guess is that parent for RubberBand and
                              parent for Label is not the same and the offset comes from that.

                              A Child is relative to its parent. So its x,y is from the parent x,y. and not from zero,zero

                              So I think its where the diff comes from.
                              The mouse is in global, but the Label is in local with x,y.
                              I think RubberBand is relative to parent (if any) and not 0,0.

                              Its talked about here
                              http://stackoverflow.com/questions/16527248/qrubberband-on-a-definite-label

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

                                @Bidski

                                for some reason, the QMenuBar is the problem. You can verify this, if you add

                                ui->menubar->hide();
                                

                                in your constructor.

                                No Offset with that.

                                Not sure why thats the case, but now we can narrow it down :)

                                B Offline
                                B Offline
                                Bidski
                                wrote on last edited by
                                #15

                                @J.Hilk @mrjj

                                I had a look at my code again. The QMenuBar, QLabel, and QRubberBand all share the same parent (which is AnnotateApp), so the mouse event position should have been in the same coordinate system as everything else? It appears that the QMenuBar is somehow absent in the calculation of the coordinates?

                                However, I made some code changes based on the SO link @mrjj provided. The QLabel is now the parent of QRubberBand and the mouse event coordinates are now mapped to global and then mapped from global to QLabel, as shown below.

                                void AnnotateApp::mousePressEvent(QMouseEvent *event)
                                {
                                    // Create a rubber band if the mouse event is happening inside the image.
                                    if (ui->image->geometry().contains(event->pos()))
                                    {
                                        origin = ui->image->mapFromGlobal(this->mapToGlobal(event->pos()));
                                
                                        if (!rubberBand)
                                        {
                                            rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->image);
                                        }
                                
                                        rubberBand->setGeometry(QRect(origin, QSize()));
                                        rubberBand->show();
                                    }
                                }
                                
                                void AnnotateApp::mouseMoveEvent(QMouseEvent *event)
                                {
                                    // Only act if we actually have a rubber band to work with.
                                    if (rubberBand && rubberBand->isVisible())
                                    {
                                        QPoint pos = ui->image->mapFromGlobal(this->mapToGlobal(event->pos());
                                
                                        // Check to see if the mouse is still inside the iamge or not.
                                        // If it is, then update the rubber band to the new coordinates.
                                        if (ui->image->geometry().contains(pos))
                                        {
                                            rubberBand->setGeometry(QRect(origin, pos).normalized());
                                        }
                                
                                        // If it is not, then clamp the coordinates to the image boundary.
                                        else
                                        {
                                            int x = std::min(ui->image->geometry().right(),  std::max(pos.x(), ui->image->geometry().left()));
                                            int y = std::min(ui->image->geometry().bottom(), std::max(pos.y(), ui->image->geometry().top()));
                                
                                            rubberBand->setGeometry(QRect(origin, QPoint(x, y)).normalized());
                                        }
                                    }
                                }
                                

                                This simply changes where the offset occurs. In the first image, I can't drag the left and top sides of the rubber band past where they are (started dragging from the middle of the QLabel). In the second image I started dragging from the top-left of the QLabel, immediately a small x-offset is appears and the width of the QRubberBand snaps to where the x-boundary occurs in the first image.

                                More offset issues
                                Even more issues

                                J.HilkJ 1 Reply Last reply
                                0
                                • B Bidski

                                  @J.Hilk @mrjj

                                  I had a look at my code again. The QMenuBar, QLabel, and QRubberBand all share the same parent (which is AnnotateApp), so the mouse event position should have been in the same coordinate system as everything else? It appears that the QMenuBar is somehow absent in the calculation of the coordinates?

                                  However, I made some code changes based on the SO link @mrjj provided. The QLabel is now the parent of QRubberBand and the mouse event coordinates are now mapped to global and then mapped from global to QLabel, as shown below.

                                  void AnnotateApp::mousePressEvent(QMouseEvent *event)
                                  {
                                      // Create a rubber band if the mouse event is happening inside the image.
                                      if (ui->image->geometry().contains(event->pos()))
                                      {
                                          origin = ui->image->mapFromGlobal(this->mapToGlobal(event->pos()));
                                  
                                          if (!rubberBand)
                                          {
                                              rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->image);
                                          }
                                  
                                          rubberBand->setGeometry(QRect(origin, QSize()));
                                          rubberBand->show();
                                      }
                                  }
                                  
                                  void AnnotateApp::mouseMoveEvent(QMouseEvent *event)
                                  {
                                      // Only act if we actually have a rubber band to work with.
                                      if (rubberBand && rubberBand->isVisible())
                                      {
                                          QPoint pos = ui->image->mapFromGlobal(this->mapToGlobal(event->pos());
                                  
                                          // Check to see if the mouse is still inside the iamge or not.
                                          // If it is, then update the rubber band to the new coordinates.
                                          if (ui->image->geometry().contains(pos))
                                          {
                                              rubberBand->setGeometry(QRect(origin, pos).normalized());
                                          }
                                  
                                          // If it is not, then clamp the coordinates to the image boundary.
                                          else
                                          {
                                              int x = std::min(ui->image->geometry().right(),  std::max(pos.x(), ui->image->geometry().left()));
                                              int y = std::min(ui->image->geometry().bottom(), std::max(pos.y(), ui->image->geometry().top()));
                                  
                                              rubberBand->setGeometry(QRect(origin, QPoint(x, y)).normalized());
                                          }
                                      }
                                  }
                                  

                                  This simply changes where the offset occurs. In the first image, I can't drag the left and top sides of the rubber band past where they are (started dragging from the middle of the QLabel). In the second image I started dragging from the top-left of the QLabel, immediately a small x-offset is appears and the width of the QRubberBand snaps to where the x-boundary occurs in the first image.

                                  More offset issues
                                  Even more issues

                                  J.HilkJ Online
                                  J.HilkJ Online
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by J.Hilk
                                  #16

                                  @Bidski

                                  I don't have a bugfix for you, but a workaround.

                                  reverte mousePressEvent back to how it was and

                                  void AnnotateApp::mouseMoveEvent(QMouseEvent *event)
                                  {
                                      // Only act if we actually have a rubber band to work with.
                                      if (rubberBand && rubberBand->isVisible())
                                      {
                                          // Check to see if the mouse is still inside the iamge or not.
                                          // If it is, then update the rubber band to the new coordinates.
                                          QRect fixedRect (ui->image->x(),
                                                           ui->image->y()+ui->menubar->height(),
                                                           ui->image->width(),
                                                           ui->image->height());
                                          if (fixedRect.contains(event->pos()))
                                          {
                                              rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
                                          }
                                  
                                          // If it is not, then clamp the coordinates to the image boundary.
                                          else
                                          {
                                              int x = std::min(fixedRect.right(),  std::max(event->pos().x(), fixedRect.left()));
                                              int y = std::min(fixedRect.bottom(), std::max(event->pos().y(), fixedRect.top()));
                                  
                                              rubberBand->setGeometry(QRect(origin, QPoint(x, y)).normalized());
                                          }
                                      }
                                  }
                                  

                                  but its a workaround! Hopefully someone can give you a better solution.


                                  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.

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

                                    @Bidski

                                    I don't have a bugfix for you, but a workaround.

                                    reverte mousePressEvent back to how it was and

                                    void AnnotateApp::mouseMoveEvent(QMouseEvent *event)
                                    {
                                        // Only act if we actually have a rubber band to work with.
                                        if (rubberBand && rubberBand->isVisible())
                                        {
                                            // Check to see if the mouse is still inside the iamge or not.
                                            // If it is, then update the rubber band to the new coordinates.
                                            QRect fixedRect (ui->image->x(),
                                                             ui->image->y()+ui->menubar->height(),
                                                             ui->image->width(),
                                                             ui->image->height());
                                            if (fixedRect.contains(event->pos()))
                                            {
                                                rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
                                            }
                                    
                                            // If it is not, then clamp the coordinates to the image boundary.
                                            else
                                            {
                                                int x = std::min(fixedRect.right(),  std::max(event->pos().x(), fixedRect.left()));
                                                int y = std::min(fixedRect.bottom(), std::max(event->pos().y(), fixedRect.top()));
                                    
                                                rubberBand->setGeometry(QRect(origin, QPoint(x, y)).normalized());
                                            }
                                        }
                                    }
                                    

                                    but its a workaround! Hopefully someone can give you a better solution.

                                    B Offline
                                    B Offline
                                    Bidski
                                    wrote on last edited by
                                    #17

                                    @J.Hilk
                                    Is this actually a bug?

                                    mrjjM 1 Reply Last reply
                                    0
                                    • B Bidski

                                      @J.Hilk
                                      Is this actually a bug?

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by mrjj
                                      #18

                                      @Bidski
                                      Hi
                                      Not in Qt. ( maybe if old)

                                      I would guess from the attempt to snap the
                                      coordinates but cant say for sure.

                                      However, please see this sample

                                      https://www.dropbox.com/s/gp9rvylkb18xnl3/myrubberband.zip?dl=0

                                      alt text

                                      Drag the rubberband and release
                                      It cuts what is expected and show in label
                                      So all seems to be fine :)

                                      #include <QMouseEvent>
                                      
                                      MainWindow::MainWindow(QWidget *parent) :
                                          QMainWindow(parent),
                                          ui(new Ui::MainWindow)
                                      {
                                          ui->setupUi(this);
                                          move_rubberband = false;
                                          rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
                                          rubberBand->setGeometry(0,0,50,50);
                                          rubberBand->show();
                                      }
                                      
                                      void MainWindow::mousePressEvent(QMouseEvent *e)
                                      {
                                          if(rubberBand->geometry().contains(e->pos()))
                                          {
                                              rubberband_offset = e->pos() - rubberBand->pos();
                                              move_rubberband = true;
                                          }
                                      }
                                      
                                      void MainWindow::mouseMoveEvent(QMouseEvent *e)
                                      {
                                          if(move_rubberband)
                                          {
                                              rubberBand->move(e->pos() - rubberband_offset);
                                          }
                                      }
                                      
                                      void MainWindow::mouseReleaseEvent(QMouseEvent *e)
                                      {
                                          move_rubberband = false;
                                      
                                          QRect r=rubberBand->geometry();
                                          rubberBand->hide();
                                          QPixmap p=grab(r); // no need for mapTo here. 
                                          ui->label->setPixmap(p);
                                          rubberBand->show();
                                          rubberBand->raise();
                                      }
                                      
                                      
                                      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