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. Incorrect Bounding Rect?

Incorrect Bounding Rect?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qfontmetricsqpainter
3 Posts 2 Posters 507 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.
  • C Offline
    C Offline
    CJha
    wrote on 25 Feb 2023, 13:12 last edited by
    #1

    Hi, I have a code to generate a label for axis:

    void npi::PlotWidget::createLabelForAxis(Axis* axis)
    {  
        // labelSize is QSize, labelFont.font is "Courier", labelFont.metrics is QFontMetrics for labelFont.font, axis->label is QString, colors.text is Qt::black
        if(axis != nullptr) {
            axis->labelSize.setHeight(labelFont.metrics.height());
            axis->labelSize.setWidth(labelFont.metrics.boundingRect(axis->label).width());
            axis->labelPix = QPixmap{axis->labelSize};
            axis->labelPix.fill(QColor{255, 0, 0, 50});
    	QPainter painter(&axis->labelPix);
    	painter.setPen(QPen{colors.text});
    	painter.setFont(labelFont.font);
    	painter.drawText(0, 0, axis->labelSize.width(), axis->labelSize.height(), Qt::AlignCenter, axis->label);
    	painter.drawRect(labelFont.metrics.boundingRect(axis->label));
    	qDebug() << axis->label << labelFont.metrics.boundingRect(axis->label);
        }
    }
    

    When it is run it draws:
    BoundingRect_Problem.png

    and outputs: "Time [s]" QRect(0,-14 75x18)

    Why is it giving me -14 as the y position of the bounding rect? Also, the left side T in the drawn label is truncated, why is that? Since I am making the labelPix the width of the boundingRect of label string and using Qt::AlignCenter the entire text should fit inside the labelPix.

    P 1 Reply Last reply 25 Feb 2023, 15:19
    0
    • C CJha
      25 Feb 2023, 13:12

      Hi, I have a code to generate a label for axis:

      void npi::PlotWidget::createLabelForAxis(Axis* axis)
      {  
          // labelSize is QSize, labelFont.font is "Courier", labelFont.metrics is QFontMetrics for labelFont.font, axis->label is QString, colors.text is Qt::black
          if(axis != nullptr) {
              axis->labelSize.setHeight(labelFont.metrics.height());
              axis->labelSize.setWidth(labelFont.metrics.boundingRect(axis->label).width());
              axis->labelPix = QPixmap{axis->labelSize};
              axis->labelPix.fill(QColor{255, 0, 0, 50});
      	QPainter painter(&axis->labelPix);
      	painter.setPen(QPen{colors.text});
      	painter.setFont(labelFont.font);
      	painter.drawText(0, 0, axis->labelSize.width(), axis->labelSize.height(), Qt::AlignCenter, axis->label);
      	painter.drawRect(labelFont.metrics.boundingRect(axis->label));
      	qDebug() << axis->label << labelFont.metrics.boundingRect(axis->label);
          }
      }
      

      When it is run it draws:
      BoundingRect_Problem.png

      and outputs: "Time [s]" QRect(0,-14 75x18)

      Why is it giving me -14 as the y position of the bounding rect? Also, the left side T in the drawn label is truncated, why is that? Since I am making the labelPix the width of the boundingRect of label string and using Qt::AlignCenter the entire text should fit inside the labelPix.

      P Offline
      P Offline
      Pl45m4
      wrote on 25 Feb 2023, 15:19 last edited by Pl45m4
      #2

      @CJha

      It seems, that you are painting on PlotWidget using QPainter outside of its paintEvent. This can lead to unwanted bahavior and shouldn't be done.

      In your case, your text won't update() (is redrawn) together with the rest of the widget, for example, when resizing.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      C 1 Reply Last reply 25 Feb 2023, 18:18
      1
      • P Pl45m4
        25 Feb 2023, 15:19

        @CJha

        It seems, that you are painting on PlotWidget using QPainter outside of its paintEvent. This can lead to unwanted bahavior and shouldn't be done.

        In your case, your text won't update() (is redrawn) together with the rest of the widget, for example, when resizing.

        C Offline
        C Offline
        CJha
        wrote on 25 Feb 2023, 18:18 last edited by
        #3

        @Pl45m4 In this function I am painting on axis->labelPix which is a QPixmap. The axis->labelPix is then put in its correct position using QPainter in the paintEvent() of PlotWidget.

        1 Reply Last reply
        0

        2/3

        25 Feb 2023, 15:19

        • Login

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