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. Scale QLabel based on text
Qt 6.11 is out! See what's new in the release blog

Scale QLabel based on text

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 513 Views 1 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    Hi. I have some text in a QLabel.

    5d74d39e-82f7-406c-a0b1-9ce87cf6afe3-image.png
    According to my code, the size of the two QLabels are 100px. I was wondering whether is a way of scaling the QLabel's size based on the length of text. I am aware that setScaledContents is a thing, but that one is for QPixmaps and only scales the Pixmap instead of the QLabel.

    I would like the QLabel to be scaled because I am trying to place the QLabel's center exactly under the QFrames (the vertical bars).

        QFrame *frontLine = new QFrame(this);
        frontLine->setStyleSheet("border: 10px solid #000080;");
        frontLine->setGeometry(X, Y, SPLITLINEWIDTH, SPLITLINEHEIGHT);
        QLabel *iniTemp = new QLabel(this);
        iniTemp->setText("REEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
        iniTemp->setStyleSheet("color: navy; background: green; font: 12px; alignment: left;");
        iniTemp->setFrameShadow(QFrame::Raised);
        iniTemp->setFrameStyle(QFrame::Panel);
        iniTemp->setLineWidth(2);
        qDebug() << (iniTemp->width());  //How can this be 0?
        iniTemp->move(X - (iniTemp->width()-65), Y+SPLITLINEHEIGHT+LABELDISTANCE);
    
        QFrame *backLine = new QFrame(this);
        backLine->setStyleSheet("border: 10px solid #000080;");
        X = MAINLINEX + MAINLINEW - SPLITLINEWIDTH/2;
        backLine->setGeometry(X, Y, SPLITLINEWIDTH, SPLITLINEHEIGHT);
        QLabel *finTemp = new QLabel(this);
        finTemp->setText("Final Temp");
        finTemp->setStyleSheet("color: navy; background: green; font: 12px; alignment: center;");
        finTemp->setFrameShadow(QFrame::Raised);
        finTemp->setFrameStyle(QFrame::Panel);
        finTemp->setLineWidth(2);
        qDebug() << (finTemp->width());
        finTemp->move(X - (finTemp->width())/2, Y+SPLITLINEHEIGHT+LABELDISTANCE);
    

    Please let me know if more information is required.

    jsulmJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      Hi. I have some text in a QLabel.

      5d74d39e-82f7-406c-a0b1-9ce87cf6afe3-image.png
      According to my code, the size of the two QLabels are 100px. I was wondering whether is a way of scaling the QLabel's size based on the length of text. I am aware that setScaledContents is a thing, but that one is for QPixmaps and only scales the Pixmap instead of the QLabel.

      I would like the QLabel to be scaled because I am trying to place the QLabel's center exactly under the QFrames (the vertical bars).

          QFrame *frontLine = new QFrame(this);
          frontLine->setStyleSheet("border: 10px solid #000080;");
          frontLine->setGeometry(X, Y, SPLITLINEWIDTH, SPLITLINEHEIGHT);
          QLabel *iniTemp = new QLabel(this);
          iniTemp->setText("REEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
          iniTemp->setStyleSheet("color: navy; background: green; font: 12px; alignment: left;");
          iniTemp->setFrameShadow(QFrame::Raised);
          iniTemp->setFrameStyle(QFrame::Panel);
          iniTemp->setLineWidth(2);
          qDebug() << (iniTemp->width());  //How can this be 0?
          iniTemp->move(X - (iniTemp->width()-65), Y+SPLITLINEHEIGHT+LABELDISTANCE);
      
          QFrame *backLine = new QFrame(this);
          backLine->setStyleSheet("border: 10px solid #000080;");
          X = MAINLINEX + MAINLINEW - SPLITLINEWIDTH/2;
          backLine->setGeometry(X, Y, SPLITLINEWIDTH, SPLITLINEHEIGHT);
          QLabel *finTemp = new QLabel(this);
          finTemp->setText("Final Temp");
          finTemp->setStyleSheet("color: navy; background: green; font: 12px; alignment: center;");
          finTemp->setFrameShadow(QFrame::Raised);
          finTemp->setFrameStyle(QFrame::Panel);
          finTemp->setLineWidth(2);
          qDebug() << (finTemp->width());
          finTemp->move(X - (finTemp->width())/2, Y+SPLITLINEHEIGHT+LABELDISTANCE);
      

      Please let me know if more information is required.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Dummie1138 You can use https://doc.qt.io/qt-6/qfontmetrics.html to figure out how wide your QLabel has to be to show the given string with the given font.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

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