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. QPainter setfont letter size problem
Forum Updated to NodeBB v4.3 + New Features

QPainter setfont letter size problem

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.8k 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.
  • kybernetesK Offline
    kybernetesK Offline
    kybernetes
    wrote on last edited by
    #1

    Hello,

    I m writing a program which i used Qpainter and i have a problem,

    my screen size is scrH and scrW

        scrW = qApp->screens()[0]->size().width();
        scrH = qApp->screens()[0]->size().height();
    

    my program size is about screensize but when i write painter.setfont("helvatica", scrH/25) some screen is ok but someones are to much. its not fixed in my area,

    i want to fix my font size with my area for example

    i wanna write the letters in scrH/10 area and i write painter.setfont("helvatica", scrH/25) but somehow its font bigger than my area

    i cant make it smaller because other screen see letters to small

    how can i fixed my letter size in a square height.

    i thought i can write a while to change my letter size but i dont know how can i get the letter size.

    Thanks.

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

      hi
      Not 100% sure what u need:
      But you can use QFontMetrics to know the sizes of your text.

      QFont font("times", 24);
      QFontMetrics fm(font);
      int pixelsWide = fm.width("What's the width of this text?");
      int pixelsHigh = fm.height();

      1 Reply Last reply
      2
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        @kybernetes
        The code you used is not correct, but I'm assuming you meant something like this: painter.setFont(QFont("helvetica", scrH/25)).
        There are couple of problems here. First make sure you spelled the font name correctly (you didn't in your post). Then make sure the font is present on the target machine, otherwise a system default will be chosen. Next, you're using a constructor that takes font's point size. Point size is not a fixed metric and depends on system settings, scaling, dpi etc. The height you use is given in pixels, not points, so this usage is wrong. You can specify the font size in pixel units using its setPixelSize() method.

        kybernetesK 1 Reply Last reply
        1
        • Chris KawaC Chris Kawa

          @kybernetes
          The code you used is not correct, but I'm assuming you meant something like this: painter.setFont(QFont("helvetica", scrH/25)).
          There are couple of problems here. First make sure you spelled the font name correctly (you didn't in your post). Then make sure the font is present on the target machine, otherwise a system default will be chosen. Next, you're using a constructor that takes font's point size. Point size is not a fixed metric and depends on system settings, scaling, dpi etc. The height you use is given in pixels, not points, so this usage is wrong. You can specify the font size in pixel units using its setPixelSize() method.

          kybernetesK Offline
          kybernetesK Offline
          kybernetes
          wrote on last edited by
          #4

          @Chris-Kawa

          you are right i write wrong code on my topic but in my project it was like your said.

          than I read both of your answer and i write that in my constractor

              buttonsFont =  QFont("Helvetica");
          
               // *** set a convergent value *****
              buttonsFont.setPixelSize(scrH/10);
          
              QFontMetrics fm1(touchScreenFont);
          
              int pixelsWide = fm1.width("7LETTER");
              //    int pixelsHigh = fm.height();
          
              while (pixelsWide > scrW)
              {
                  buttonsFont.setPixelSize(buttonsFont.pixelSize() - 3);
                  QFontMetrics fm1(buttonsFont);
                  pixelsWide = fm1.width("7LETTER");
              }
          

          it is working now but if you may say better solution or correction my code its ok for me.

          thanks for your help.

          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