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. Precision of QFontMetrics
Forum Updated to NodeBB v4.3 + New Features

Precision of QFontMetrics

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 580 Views 3 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 the following two QLabels.

    943856bd-5860-4456-ac9c-52268427efa7-image.png
    d5c491e2-1e65-4fcd-8514-9aa18dc1dbeb-image.png

    The top one has been resized using QFontMetrics, like so:

            QFontMetrics fm(QFont("DejaVu Sans", 12));    
            //Line that stores QFont's size   
            QString labelStyle = "color: navy; font: 12px \"DejaVu Sans\"; background: transparent; alignment: center";    
        
            //Build the main line.    
            QFrame *frontLine = new QFrame(this);    
            frontLine->setStyleSheet("border: 10px solid #000080;");    
            frontLine->setGeometry(X, Y, SPLITLINEWIDTH, SPLITLINEHEIGHT);    
            QString labelText = "Initial Temp";    
        
            QLabel *iniTemp = new QLabel(this);    
            iniTemp->setText(labelText);
            iniTemp->setStyleSheet(labelStyle);    
            iniTemp->resize(fm.horizontalAdvance(labelText), LABELHEIGHT);    
            //iniTemp->setFrameShadow(QFrame::Raised);    
            iniTemp->setFrameStyle(QFrame::Panel);    
            iniTemp->setLineWidth(2);
            qDebug() << (iniTemp->width());
            iniTemp->move(X - (iniTemp->width())/2, Y+SPLITLINEHEIGHT+LABELDISTANCE);
    
    

    The bottom one has not been altered.

        QFontMetrics fm(QFont("DejaVu Sans", 12));
        //Line that stores QFont's size
        QString labelStyle = "color: navy; font: 12px \"DejaVu Sans\"; background: transparent; alignment: center";
    
        //Build the main line.
        QFrame *frontLine = new QFrame(this);
        frontLine->setStyleSheet("border: 10px solid #000080;");
        frontLine->setGeometry(X, Y, SPLITLINEWIDTH, SPLITLINEHEIGHT);
        QString labelText = "Initial Temp";
    
        QLabel *iniTemp = new QLabel(this);
        iniTemp->setText(labelText);
        iniTemp->setStyleSheet(labelStyle);
        //iniTemp->resize(fm.horizontalAdvance(labelText), LABELHEIGHT);
        //iniTemp->setFrameShadow(QFrame::Raised);
        iniTemp->setFrameStyle(QFrame::Panel);
        iniTemp->setLineWidth(2);
        qDebug() << (iniTemp->width());
        iniTemp->move(X - (iniTemp->width())/2, Y+SPLITLINEHEIGHT+LABELDISTANCE);
    

    How precise is QFontMetrics ususally? I wanna know this because I believe my fonts are correctly labelled unless I've misunderstood something. Please let me know if more information is required.

    1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      @mrjj Thank you for your response. I am currently operating on Windows 10, is that enough additional information?

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

      @Dummie1138

      Hi
      Well in that case it should be pretty much on spot. At least in win 7/10.

      Do note that sometimes mixing Stylesheets and Fonts might give surprises. :)

      Dummie1138D 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi
        Normally QFontMetrics is pretty precise, and horizontalAdvance even more so than the old Width function but
        it also depends on the OS.

        Dummie1138D 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Normally QFontMetrics is pretty precise, and horizontalAdvance even more so than the old Width function but
          it also depends on the OS.

          Dummie1138D Offline
          Dummie1138D Offline
          Dummie1138
          wrote on last edited by
          #3

          @mrjj Thank you for your response. I am currently operating on Windows 10, is that enough additional information?

          mrjjM 1 Reply Last reply
          0
          • Dummie1138D Dummie1138

            @mrjj Thank you for your response. I am currently operating on Windows 10, is that enough additional information?

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

            @Dummie1138

            Hi
            Well in that case it should be pretty much on spot. At least in win 7/10.

            Do note that sometimes mixing Stylesheets and Fonts might give surprises. :)

            Dummie1138D 1 Reply Last reply
            0
            • mrjjM mrjj

              @Dummie1138

              Hi
              Well in that case it should be pretty much on spot. At least in win 7/10.

              Do note that sometimes mixing Stylesheets and Fonts might give surprises. :)

              Dummie1138D Offline
              Dummie1138D Offline
              Dummie1138
              wrote on last edited by Dummie1138
              #5

              @mrjj I see, thanks. In that case, what are some other ways I can control the font size of the text in QLabel? I haven't been able to find any within the QLabel documentation.

              M 1 Reply Last reply
              0
              • Dummie1138D Dummie1138

                @mrjj I see, thanks. In that case, what are some other ways I can control the font size of the text in QLabel? I haven't been able to find any within the QLabel documentation.

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #6

                @Dummie1138 said in Precision of QFontMetrics:

                what are some other ways I can control the font size of the text in QLabel?

                    auto label=new QLabel("Hello Wolrd");
                    auto font=label->font();    // default font
                    font.setPointSize(font.pointSize()+2);
                    label->setFont(font);
                    label->show();
                
                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