QPainter setfont letter size problem



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


  • Qt Champions 2016

    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();


  • Moderators

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.