Results 1 to 4 of 4

Thread: How to embed text to pixmap in QLabel?

  1. #1

    Default How to embed text to pixmap in QLabel?

    Hello,

    How to embed text to pixmap in QLabel?
    Suppose I have the following code inherited from a QLabel class:
    void myLabel::setImage(QImage myImage, QString myText)
    {
    QPixmap pixmap = QPixmap::fromImage(myImage);
    setPixmap();
    // once this statements are done myImage is shown properly
    // now if I do for the text:
    setText(myText); // I don't see myText at all.
    }

    I've tried using QPainter but still did not show the text embedded in the QLabel.
    Help please!

    Tom

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to embed text to pixmap in QLabel?

    QPixmap pixmap = QPixmap::fromImage(myImage);
    setPixmap();
    I'd like to know how you manage to set a pixmap with this code.

    From the QLabel docs:

    Setting the pixmap clears any previous content.
    and

    Setting the text clears any previous content.
    You get one or the other, but not both. And according to the "code" you posted, you should be seeing the text and not the pixmap.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3

    Default Re: How to embed text to pixmap in QLabel?

    Thank you, and I just solve it.

    Qt Code:
    1. QPainter p(&myImage);
    2. p.save();
    3. // set the text style here
    4. p.drawText(..., myText); // place it wherever with the alignment flag
    5. p.restore();
    6.  
    7. //then:
    8. void myLabel::setImage(QImage myImage, QString myText)
    9. {
    10. QPixmap pixmap = QPixmap::fromImage(myImage);
    11. setPixmap();
    12. ...
    13. }
    14.  
    15. In other word, I did what someone suggested. put the text on top of the image at first, before the image is mapped into the pixmap.
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to embed text to pixmap in QLabel?

    Yes, that's about the only way to do it with a standard QLabel. The QPainter save() and restore() operations are meaningless, since your QPainter variable is created on the stack, is deleted after it is used to paint the image, and has no relationship to any QPainter instance used anywhere else in your code.

    Your code should also be "style aware" so that the text is painted using the style that the rest of your app uses. Maybe you are doing that. "..." hides a lot of relevant details.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Can't refresh QLabel's Pixmap properly
    By deltamac in forum Newbie
    Replies: 3
    Last Post: 7th June 2015, 13:28
  2. QLabel| Pixmap Delay
    By vProgramm in forum Newbie
    Replies: 3
    Last Post: 30th January 2015, 01:45
  3. geting QLabel text ontop of other QLabel displaying image
    By krystosan in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2013, 18:35
  4. Pixmap updating QLabel
    By Matt in forum Newbie
    Replies: 11
    Last Post: 17th August 2010, 22:11
  5. empty pixmap as a QLabel
    By tommy in forum Qt Programming
    Replies: 16
    Last Post: 11th December 2007, 22:15

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.