Results 1 to 6 of 6

Thread: Drawing over QLabel

  1. #1
    Join Date
    Sep 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drawing over QLabel

    Hi,

    I have a QLabel on which I have set a pixmap of an image

    QImage image(fileName);
    if (image.isNull()) {
    QMessageBox::information(this, tr("Image Viewer"),
    tr("Cannot load %1.").arg(fileName));
    return;
    }
    imageLabel->setPixmap(QPixmap::fromImage(image));

    Now, I want to write text over this image which i have loaded into QLabel

    Please tell me how to do this.

    Cheers

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Drawing over QLabel

    You can paint on the pixmap itself (simply set a painter on it and draw) or you reimplemented the paint event of the label and do the painting there. Second would be nicer I think.

    And is there any reason why you load the file as a QImage to convert it then to a QPixmap?

  3. #3
    Join Date
    Sep 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing over QLabel

    Hi

    Thanks for the help. Could you tell me the code snippet to do the same(reimplement the paint event of the label and paint there).

    I am new to Qt and I used this snippet from an example "Image Viewer". I load an image file using QImage.

  4. #4
    Join Date
    Apr 2011
    Location
    San Francisco Bay area
    Posts
    5
    Qt products
    Platforms
    MacOS X Windows

    Default Re: Drawing over QLabel

    In reverse order:

    Qt Code:
    1. QPixmap pixmap(filename);
    2. if (pixmap.isNull())
    3. ...
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class MySuperCoolLabel (QLabel);
    2.  
    3. void MySuperCoolLabel::paintEvent (QPaintEvent *event)
    4. {
    5. painter = QPainter(this);
    6. painter.save();
    7. painter.drawPixmap(0, 0, my_pixmap);
    8. painter.setFont(font());
    9. painter.drawText(x, y, my_text);
    10. painter.restore();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Please read the class documentation in addition to the tutorials.

  5. #5
    Join Date
    Mar 2009
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Drawing over QLabel

    How about this
    label->setStyleSheet("background-image: url(logobackground.png)");
    label.png
    Last edited by yycking; 22nd April 2011 at 02:38.

  6. #6
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Drawing over QLabel

    Use the image to set the background, then write a normal label.

Similar Threads

  1. Drawing over an image displayed on a QLabel
    By franco.amato in forum Qt Programming
    Replies: 10
    Last Post: 25th August 2010, 05:40
  2. problem in drawing image in QLabel!
    By mismael85 in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2010, 15:16
  3. Replies: 1
    Last Post: 29th September 2009, 19:44
  4. drawing over QLabel
    By tommy in forum Qt Programming
    Replies: 18
    Last Post: 17th November 2007, 18:39
  5. Replies: 3
    Last Post: 4th June 2007, 09:51

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.