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 .
Canvas
Setbgimage on to canvas
set foreground image to canvas
paint text on the canvasthe text size varies for different background images set.
regards
Gayathri -
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() ; font.setPointSize(12); /* set the modified font to the painter */ painter->setFont(font); /* draw text etc. */ painter.drawText(....);
-
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
-
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.drawRect(rect); p.fillRect(rect, QBrush(QColor(0, 0, 0, 128))); p.setPen(QColor(200,200,180)); QRect rect1(50,350, 800, 300); QFont font1 = p.font() ; font1.setPointSize(12); /* set the modified font to the painter */ p.setFont(font1); p.drawText(rect1,Qt::TextWordWrap,narrator);
-
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. -
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 ? -
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
-
@Gayathri
Super.
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.
using
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. -
@Gayathri
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) {
FontSize--;
}
So you dont hang your app if u ever get a text that just cannot fit :)