Results 1 to 6 of 6

Thread: How to show single image as background in label

  1. #1
    Join Date
    Sep 2008
    Posts
    58
    Thanks
    11
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Post How to show single image as background in label

    Hi,

    I want to show single image in background of QLabel. If the label size is big(say 640 by 480) and if image size is small (say 64 by 64). then background will show so many no of images.

    But I want to show only single image with original size in center.

    Can anybody suggest me right way to do this.

    I am trying following...

    Qt Code:
    1. QLabel label;
    2. QPalette palette;
    3.  
    4. label.setGeometry(0,0,640,480);
    5. QImage img("/home/nirav/img.png");
    6.  
    7. palette.setBrush(label.backgroundRole(), QBrush(img));
    8.  
    9. label.setPalette(palette);
    10.  
    11. label.show();
    To copy to clipboard, switch view to plain text mode 

    Nirav
    "Your suggestion would be a great help for me"

  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: How to show single image as background in label


  3. #3
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to show single image as background in label

    Quote Originally Posted by nrabara View Post
    Hi,

    I want to show single image in background of QLabel. If the label size is big(say 640 by 480) and if image size is small (say 64 by 64). then background will show so many no of images.

    But I want to show only single image with original size in center.

    Can anybody suggest me right way to do this.

    I am trying following...

    Qt Code:
    1. QLabel label;
    2. QPalette palette;
    3.  
    4. label.setGeometry(0,0,640,480);
    5. QImage img("/home/nirav/img.png");
    6.  
    7. palette.setBrush(label.backgroundRole(), QBrush(img));
    8.  
    9. label.setPalette(palette);
    10.  
    11. label.show();
    To copy to clipboard, switch view to plain text mode 

    Nirav
    "Your suggestion would be a great help for me"
    Pls try this.
    1. You need to use a frame.
    2. Put QLabel in the frame.
    3. Set your image as QPixmap to that label.
    4. Set the Alignment Center (HCenter | VCenter)
    5. Set Pixmap scaling to content turned off.

  4. The following user says thank you to yogeshgokul for this useful post:

    nrabara (16th December 2009)

  5. #4
    Join Date
    Sep 2008
    Posts
    58
    Thanks
    11
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to show single image as background in label

    I have tries as per your suggestion.
    Qt Code:
    1. QImage img("/home/nirav/img.png");
    2.  
    3. QFrame frame;
    4. frame.setGeometry(0,0,250,250);
    5. frame.show();
    6.  
    7. QLabel label(&frame);
    8. label.setGeometry(0,0,200,200);
    9.  
    10. QPixmap pixmap;
    11. pixmap = pixmap.fromImage(img);
    12.  
    13. QPalette palette;
    14. palette.setBrush(label.backgroundRole, QBrush(pixmap));
    15.  
    16. label.setPalette(palette);
    17. label.show();
    To copy to clipboard, switch view to plain text mode 

    but still I am getting some many no of images in label .

    I thing this happens because when I create a object of QBrush by passing Pixmap as argument in constructor it takes Qt::TexturePattern. that's way I am getting no of image instead of single one.

    This Qt:: TexturePattern is default for QBrush::QBrush(const QImage & img) & QBrush::QBrush(const QPixmap & pixmap)

    Can anybody help me solve this problem & how to show a single image in background instead of so many images.

    Suggestion would be appreciated.

  6. #5
    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: How to show single image as background in label

    Quote Originally Posted by nrabara View Post
    but still I am getting some many no of images in label .

    I thing this happens because when I create a object of QBrush by passing Pixmap as argument in constructor it takes Qt::TexturePattern. that's way I am getting no of image instead of single one.
    What a surprise. Use QLabel::setPixmap()!

  7. The following user says thank you to Lykurg for this useful post:

    nrabara (16th December 2009)

  8. #6
    Join Date
    Sep 2008
    Posts
    58
    Thanks
    11
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default [Solved] How to show single image as background in label

    Yes, QLabel::setPixmap() .

    Thanks .

Similar Threads

  1. Adding image in background
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 6
    Last Post: 9th August 2009, 17:39
  2. QCalendarWidget background image
    By khcbabu in forum Newbie
    Replies: 0
    Last Post: 31st March 2009, 13:51
  3. How to stretch background image used in brush-palette
    By Hookem in forum Qt Programming
    Replies: 5
    Last Post: 24th December 2008, 18:03
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  5. [QT4.1.1 XP] background image
    By incapacitant in forum Newbie
    Replies: 3
    Last Post: 1st March 2006, 14:02

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.