Results 1 to 3 of 3

Thread: Draw text on images

  1. #1
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Draw text on images

    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


    Qt Code:
    1. QPainter painter(&image);
    2.  
    3. painter.setPen(m_myPenColor);//(QPen(m_myPenColor, myPenWidth,
    4. painter.setBrush(QBrush(m_myPenColor));
    5.  
    6. painter.setFont(m_myFont );
    7.  
    8. QFontMetricsF fm(m_myFont);
    9. qreal pixelsHigh = fm.height();
    10. qreal pixelwidthMil = fm.width("First Text");
    11. qreal pixelwidthMm = fm.width("Second Text");
    12. qreal pixelWidthMax= (pixelwidthMil>=pixelwidthMm)?pixelwidthMil:pixelwidthMm;
    13. int iX=0,iY=0;
    14. iX = endPoint.x();
    15.  
    16. iY = endPoint.y()+(int)pixelsHigh;
    17.  
    18. painter.drawText(iX+2, iY, "First Text");
    19. iY = iY + (int)pixelsHigh+1;
    20. painter.drawText(iX+2, iY, "Second Text");
    21.  
    22. pixelsHigh = (pixelsHigh*2)+6;
    23.  
    24. pixelWidthMax = pixelWidthMax+6;
    25. update(endPoint.x()-2, endPoint.y()-2, (int)pixelWidthMax,(int)pixelsHigh);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Draw text on images

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

  3. #3
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Draw text on images

    Thanks to your reply...,
    I have solved it myself by changing
    QFontMetricsF fm(m_myFont);
    to
    QFontMetricsF fm(painter.fontMetrics());

Similar Threads

  1. How to draw a line with tiled images with QPainter?
    By MadBear in forum Qt Programming
    Replies: 5
    Last Post: 25th July 2011, 06:36
  2. Replies: 10
    Last Post: 10th February 2011, 23:31
  3. To draw images in QLabel
    By augusbas in forum Qt Programming
    Replies: 5
    Last Post: 11th October 2010, 06:27
  4. QLineEdit, draw only text
    By Radagast in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2009, 11:15
  5. Qt Draw fast png images
    By anafor2004 in forum Newbie
    Replies: 4
    Last Post: 4th November 2008, 09:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.