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?
QtWS25 Last Chance

Incorrect Bounding Rect?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qfontmetricsqpainter
3 Posts 2 Posters 503 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on 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.

    Pl45m4P 1 Reply Last reply
    0
    • CJhaC CJha

      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.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on 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

      CJhaC 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

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

        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on 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

        • Login

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