Nominate our 2022 Qt Champions!

Qt Image on label

  • Hi,

    I am new to Qt. I have been using QPainter on the label to show a background image, a foreground image on it and a dialog on the canvas. For some of the background images , i find that the text displayed increases in font size and for some background images text size is what i set it as. Could you please help me with this .

    Setbgimage on to canvas
    set foreground image to canvas
    paint text on the canvas

    the text size varies for different background images set.


  • Lifetime Qt Champion

    Hi and welcome
    Im not sure 100% what you ask, but it sounds like
    that text you write on canvas, has different size sometimes.
    Have you tried to set the size directly ?

    QFont font = painter->font() ;
    /* set the modified font to the painter */
    /* draw text etc. */

  • Yes i have tried to set the size inspite of it some of the text comes very large for some background images.

    I have a canvas label. I paint a background scene .. place a character image on the scene and then a dialogue text on the scene. for some of the background images i place on the scene , my dialogue text is very very big . thats my problem . Does this explanation help mrjj ?. Am sorry for being unclear

  • Lifetime Qt Champion

    @Gayathri said:
    ok. how do you place the dialogue text ?
    Its it a label or do you draw it with drawText?
    Normally, it uses the font you set.

    ANd when you say scene , you mean a QLabel with pixmap you draw on ?

  • Yes it is a QLabel with pixmap and then i use painter and drawText for the dialog . I set the font size

    const QPixmap *orig_bg = canvas_bg_img->pixmap();
    QImage orig_bg_img = orig_bg->toImage();

    QPainter p(&orig_bg_img);
    QRect rect(0,350,640,200);
    p.fillRect(rect, QBrush(QColor(0, 0, 0, 128)));
    QRect rect1(50,350, 800, 300);
    QFont font1 = p.font() ;
    /* set the modified font to the painter */

  • Lifetime Qt Champion

    ok. thank you for code.
    So even with this code, it sometimes is way bigger than 12 points?

    I wonder if some of the images makes setPointSize scale.
    Can you try with
    void QFont::setPixelSize(int pixelSize)
    and see if it still makes big text sometimes.
    Then we know why it happens.

  • Ok i will try that

  • Lifetime Qt Champion

    oh I forgot to ask.
    Can you make it draw big text each time if u use the image that does it?
    I mean, is it repeatable ?

  • yes every time i use that background pic is big !

  • Hi. SetPixel size worked !!!!!! Could you explain to me why is that so ?

  • @mrjj

    Setpixel size worked ! ... Could u explain as to why this worked

    Also i have another question.

    Now i have textwrap for my text in drawtext. But it does go out of view. Is it possible to change font to fit the size of the space i have ???? . Changing font dynamically depending on the size of the dialogue and the space

  • Lifetime Qt Champion

    Well setPointSize is relative to resolution of the device/surface.
    so when you say 12. it will try to make that look like 12 across many different
    screens. Its call device independent.

    setPixelSizeon the other hand just use the value you give it so
    on other screen, it might look much smaller.

    so I think setPointSize somehow thinks that your big image is much higher res
    and then try to help you scale the font.

    ++ change font to fit the size
    Well you can code it yes.
    QFontMetrics fm(myFont);
    int width=fm.width(str);
    you can get size info about what you draw
    so you can check if it fits inside the rect you have.
    if NOT, then you can reduce font size and try again
    until it fits.

  • Thanks a lot @mrjj. You were of great help !

  • Lifetime Qt Champion

    Super. Please mark as solved if possible.

    Final note:
    if you make a loop to find font size then make sure
    to make a counter guard to exit if looping too many times.
    while ( TestSize(FontSize,str)==false & count++ < 100) {
    So you dont hang your app if u ever get a text that just cannot fit :)

  • sure i will ...thanks mrjj

Log in to reply