Results 1 to 9 of 9

Thread: Setting background image for Central Widget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2010
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting background image for Central Widget

    I created a BackgroundWidget class that does what you want:

    Qt Code:
    1. #ifndef BACKGROUNDWIDGET_H
    2. #define BACKGROUNDWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QPixmap>
    6. #include <QString>
    7. #include <QSize>
    8. #include <QPaintEvent>
    9.  
    10. class BackgroundWidget: public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. BackgroundWidget(QWidget* parent, QString bgImagePath);
    16. ~BackgroundWidget();
    17.  
    18.  
    19. void setPicture(const QString& bgImagePath);
    20. void setPixmap(QPixmap* bgImage);
    21.  
    22. QSize sizeHint() const;
    23.  
    24. signals:
    25. void backgroundPainted();
    26.  
    27. protected:
    28. void paintEvent(QPaintEvent *event);
    29.  
    30. private:
    31. QPixmap Image;
    32. QBrush Brush;
    33. };
    34.  
    35.  
    36. #endif // BACKGROUNDWIDGET_H
    To copy to clipboard, switch view to plain text mode 
    First Ctor arg is your widget (QMainWindow for example), second arg is the path to the picture, possibly stored as resource.

    Here is the implementation:
    Qt Code:
    1. #include "BackgroundWidget.h"
    2. #include <QWidget>
    3. #include <QPixmap>
    4. #include <QString>
    5. #include <QSize>
    6. #include <QPaintEvent>
    7. #include <QPainter>
    8.  
    9. BackgroundWidget::BackgroundWidget(QWidget *parent, QString bgImagePath): QWidget(parent), Image(), Brush()
    10. {
    11. parentWidget()->stackUnder(this);
    12. setPicture(bgImagePath);
    13. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    14. lower();
    15. Brush = QBrush(Image);
    16. }
    17.  
    18.  
    19. BackgroundWidget::~BackgroundWidget()
    20. {
    21.  
    22. }
    23.  
    24.  
    25. QSize BackgroundWidget::sizeHint() const
    26. {
    27. return parentWidget()->visibleRegion().boundingRect().size();
    28. }
    29.  
    30. void BackgroundWidget::setPicture(const QString& bgImagePath)
    31. {
    32. Image = QPixmap(bgImagePath);
    33. }
    34.  
    35. void BackgroundWidget::setPixmap(QPixmap* bgImage)
    36. {
    37. if (! bgImage)
    38. return;
    39. Image = QPixmap(*bgImage);
    40. }
    41.  
    42. void BackgroundWidget::paintEvent(QPaintEvent *event)
    43. {
    44. resize(parentWidget()->visibleRegion().boundingRect().size());
    45. QPainter painter(this);
    46. painter.setBrush(Brush);
    47. int x = (width() - Image.width()) / 2;
    48. int y = (height() - Image.height()) / 2;
    49. painter.drawPixmap(x, y, Image.width(), Image.height(), Image);
    50. emit backgroundPainted();
    51. }
    To copy to clipboard, switch view to plain text mode 

    See attachment for complete code

    rvgrouik
    Attached Files Attached Files

  2. The following user says thank you to rvgrouik for this useful post:

    thaihoangluu (22nd December 2011)

Similar Threads

  1. Replies: 10
    Last Post: 10th November 2010, 03:12
  2. setting my widget background as desktop
    By sanjayshelke in forum Qt Programming
    Replies: 4
    Last Post: 8th July 2009, 05:35
  3. Replies: 0
    Last Post: 6th April 2009, 01:20
  4. Background image and semitransparent widget
    By asieriko in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2007, 16:43
  5. Setting background image of QFrame
    By Claymore in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2007, 19:50

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
  •  
Qt is a trademark of The Qt Company.