PDA

View Full Version : Draw text on images



arunkumaraymuo1
19th August 2012, 03:45
I am developing a simple painting tool using Qt. For that I modified Qt's scribble code to draw text on an image. I can draw text on a normal jpgg image using the below code. But when I load big image(high resolution image), the text overlaps each other. I tried several technique but not get a perfect result

Code



QPainter painter(&image);

painter.setPen(m_myPenColor);//(QPen(m_myPenColor, myPenWidth,
painter.setBrush(QBrush(m_myPenColor));

painter.setFont(m_myFont );

QFontMetricsF fm(m_myFont);
qreal pixelsHigh = fm.height();
qreal pixelwidthMil = fm.width("First Text");
qreal pixelwidthMm = fm.width("Second Text");
qreal pixelWidthMax= (pixelwidthMil>=pixelwidthMm)?pixelwidthMil:pixelwidthMm;
int iX=0,iY=0;
iX = endPoint.x();

iY = endPoint.y()+(int)pixelsHigh;

painter.drawText(iX+2, iY, "First Text");
iY = iY + (int)pixelsHigh+1;
painter.drawText(iX+2, iY, "Second Text");

pixelsHigh = (pixelsHigh*2)+6;

pixelWidthMax = pixelWidthMax+6;
update(endPoint.x()-2, endPoint.y()-2, (int)pixelWidthMax,(int)pixelsHigh);

mvuori
19th August 2012, 10:09
(You don't provide enough info. The texts overlap, but why: is one of them positioned incorrectly or is the font size too large. Are the errors very small or big and obvious. Etc... Not to mention hard to understand code - pixelwidthMil and pixelwidthMm are cryptic variable names)

arunkumaraymuo1
23rd August 2012, 06:38
Thanks to your reply...,
I have solved it myself by changing
QFontMetricsF fm(m_myFont);
to
QFontMetricsF fm(painter.fontMetrics());