Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-89614

Width and height of QRect are off by one in the center and moveCenter methods.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.12.10
    • Core: Other
    • None

    Description

      Sorry, ignore this message. I had a wrong understanding of how QRect works. Even the fix is wrong. I had an issue that I thought was related to QRect and somehow modifying how the center is computed fixed it. So the issue is probably in my code.

      In the center and moveCenter methods of QRect, the width and the height computed there are one less than the actual values.
      This will result in the wrong computation of the center coordinates.

      Example:

      qDebug() << QRect(0, 0, 2, 2).center();
      // Result QPoint(0, 0) should be QPoint(1, 1)
      QRect a(0, 0, 2, 2);
      a.moveCenter(QPoint(-1, -1));
      qDebug() << a;
      // Result QRect(-1,-1 2x2) should be QRect(-2,-2 2x2)
      

      Possible fix:

      // center
      { return QPoint(int((qint64(x1)+x2+1)/2), int((qint64(y1)+y2+1)/2)); }
      
      // moveCenter
      {
          int w = x2 - x1;
          int h = y2 - y1;
          x1 = p.x() - (w + 1)/2;
          y1 = p.y() - (h + 1)/2;
          x2 = x1 + w;
          y2 = y1 + h;
      }

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            ste Stefano Moretti
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes