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
    Jan 2009
    Posts
    20
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanks
    7

    Default Re: Setting background image for Central Widget

    Thanks for all your replies. However, despite my efforts and I'm still struggling and there are lots of things I need to learn.

    I'm still on the backgrounds and stylesheets (need to use them rather than QPalette). Could you please explain what's the difference between 1) which gives me red background and 2) with the default (grey) background.

    1)
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. QWidget *w = new QWidget;
    4. setCentralWidget(w);
    5. w->setAutoFillBackground(true);
    6. w->setStyleSheet("background-color: red; ");
    7. }
    To copy to clipboard, switch view to plain text mode 

    2)
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. CentralContainer *w = new CentralContainer();
    4. setCentralWidget(w);
    5. w->setAutoFillBackground(true);
    6. w->setStyleSheet("background-color: red; ");
    7. }
    8.  
    9. CentralContainer::CentralContainer() : QWidget()
    10. {
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Setting background image for Central Widget

    Hi,
    a question:
    is it possible to resize automatically a background image inserted with the style sheet instruction
    background-image url("..."); ?????
    I searched in the documentation, but I found nothing about this issue!

    Thanks a lot,
    Alex

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

    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

  4. 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, 04:12
  2. setting my widget background as desktop
    By sanjayshelke in forum Qt Programming
    Replies: 4
    Last Post: 8th July 2009, 06:35
  3. Replies: 0
    Last Post: 6th April 2009, 02:20
  4. Background image and semitransparent widget
    By asieriko in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2007, 17:43
  5. Setting background image of QFrame
    By Claymore in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2007, 20: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.