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. Overlay in QPainter, text needs to be resized as window resizes
Qt 6.11 is out! See what's new in the release blog

Overlay in QPainter, text needs to be resized as window resizes

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.9k 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.
  • Pradeep KumarP Offline
    Pradeep KumarP Offline
    Pradeep Kumar
    wrote on last edited by Pradeep Kumar
    #1

    Hi,

    I have overlay text in QPainter ,i need to resize the overlay text when the window resizes, right now the text is of same size even if i resize the window.

    QPainter painter(this);
        QRect rect(this->m_pQWidget->x(),
                   this->m_pQWidget->y(),
                   this->m_pQWidget->width(),
                   this->m_pQWidget->height());
        painter.fillRect(rect,Qt::black);
        QDateTime dateTimeObject;
        QPen oPen1;
        oPen1.setWidth(1);
        oPen1.setColor("green");
        painter.setPen(oPen1);
        painter.drawText(rect,Qt::AlignBottom | Qt::AlignCenter,
                         dateTimeObject.currentDateTime().toString());
    

    How can i achieve this?.

    Thanks,

    Pradeep Kumar
    Qt,QML Developer

    mrjjM 1 Reply Last reply
    0
    • Pradeep KumarP Pradeep Kumar

      Hi,

      I have overlay text in QPainter ,i need to resize the overlay text when the window resizes, right now the text is of same size even if i resize the window.

      QPainter painter(this);
          QRect rect(this->m_pQWidget->x(),
                     this->m_pQWidget->y(),
                     this->m_pQWidget->width(),
                     this->m_pQWidget->height());
          painter.fillRect(rect,Qt::black);
          QDateTime dateTimeObject;
          QPen oPen1;
          oPen1.setWidth(1);
          oPen1.setColor("green");
          painter.setPen(oPen1);
          painter.drawText(rect,Qt::AlignBottom | Qt::AlignCenter,
                           dateTimeObject.currentDateTime().toString());
      

      How can i achieve this?.

      Thanks,

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

      @Pradeep-Kumar
      Hi
      Use http://doc.qt.io/qt-5/qfontmetrics.html
      to know size of text, then use
      the font to size it by using a larger point size.

      1 Reply Last reply
      1
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by Pradeep Kumar
        #3
        QFontMetrics fm = painter.fontMetrics();
        int width = fm.width(dateTimeObject.currentDateTime().toString());
        

        Is this the way to use QFontMetrics?.

        And needs to be updated in paintevent()?.

        Thanks,

        Pradeep Kumar
        Qt,QML Developer

        mrjjM 1 Reply Last reply
        0
        • Pradeep KumarP Pradeep Kumar
          QFontMetrics fm = painter.fontMetrics();
          int width = fm.width(dateTimeObject.currentDateTime().toString());
          

          Is this the way to use QFontMetrics?.

          And needs to be updated in paintevent()?.

          Thanks,

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

          @Pradeep-Kumar
          well yes, you can use that information to know
          if you should increase or decrease font size.

          1 Reply Last reply
          1
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            Then after i got width from QFontMetrics, we need to add to Qpen setwidth?.

            Pradeep Kumar
            Qt,QML Developer

            mrjjM 1 Reply Last reply
            0
            • Pradeep KumarP Pradeep Kumar

              Then after i got width from QFontMetrics, we need to add to Qpen setwidth?.

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

              @Pradeep-Kumar
              No QFont :)
              Draw text smaller or bigger
              http://doc.qt.io/qt-5/qfont.html#setPointSize

              1 Reply Last reply
              2

              • Login

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