Qt Image on label
-
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 :)