Results 1 to 9 of 9

Thread: Setting background image for Central Widget

  1. #1
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Setting background image for Central Widget

    I want to create something that looks like a typical email application (thunderbird, mac mail, …) - one column on the left and two panes, one above the other, on the right. In addition, I want a single, large, continuous background image spreading across all the panes.

    I'm trying to set the background image for the Central Widget of QMainWindow, but to no avail.

    Is it actually possible to do something like that?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting background image for Central Widget

    Yes, it's possible. Just set the background for the central widget using stylesheets or QPalette.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Setting background image for Central Widget

    I had another go, and still no luck. If you say it's possible, then it must the case.

    I'm using stylesheets and the css file is picked up for QLabels, but no luck with the background image. Any idea what am I doing wrong? Thanks again.

    Qt Code:
    1. #include "CentralContainer.h"
    2.  
    3.  
    4. CentralContainer::CentralContainer(QWidget *parent) : QWidget(parent)
    5. {
    6. this->setObjectName("CentralContainer");
    7.  
    8. QLabel *titleLabel = new QLabel("Central Container");
    9. titleLabel->setObjectName("CentralContainerTitle");
    10.  
    11. leftPanel = new LeftPanel(this);
    12. topPanel = new TopPanel(this);
    13. bottomPanel = new BottomPanel(this);
    14.  
    15. QGridLayout *layout = new QGridLayout();
    16. layout->addWidget(titleLabel, 0, 0, 1, 2);
    17. layout->addWidget(leftPanel, 1, 0, 2, 1);
    18. layout->addWidget(topPanel, 1, 1);
    19. layout->addWidget(bottomPanel, 2, 1);
    20.  
    21. setLayout(layout);
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QWidget#CentralContainer {
    2. background-image: url(images/background.png);
    3. }
    4.  
    5. QLabel#CentralContainerTitle {
    6. background-color: aqua;
    7. color: purple;
    8. }
    9.  
    10. CentralContainer QLabel {
    11. background-color: transparent;
    12. color: blue;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  4. #4
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting background image for Central Widget

    You have to call setStyleSheet() in the constructor of CentralContainer passing the contents of mystyle.css.

    Qt Code:
    1. CentralContainer::CentralContainer(QWidget *parent) : QWidget(parent)
    2. {
    3. this->setObjectName("CentralContainer");
    4.  
    5. QLabel *titleLabel = new QLabel("Central Container");
    6. titleLabel->setObjectName("CentralContainerTitle");
    7.  
    8. leftPanel = new LeftPanel(this);
    9. topPanel = new TopPanel(this);
    10. bottomPanel = new BottomPanel(this);
    11.  
    12. QGridLayout *layout = new QGridLayout();
    13. layout->addWidget(titleLabel, 0, 0, 1, 2);
    14. layout->addWidget(leftPanel, 1, 0, 2, 1);
    15. layout->addWidget(topPanel, 1, 1);
    16. layout->addWidget(bottomPanel, 2, 1);
    17.  
    18. setLayout(layout);
    19.  
    20. QFile cssFile("mystyle.css");
    21. if(cssFile.open(QIODevice::ReadOnly)) {
    22. QString css = QString::fromLocal8Bit(cssFile.readAll());
    23. setStyleSheet(css);
    24. cssFile.close();
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by victor.fernandez; 11th September 2009 at 16:31.

  5. #5
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Setting background image for Central Widget

    I was actually calling setStyleSheet() in main.cpp, but moving it to CentralContainer() doesn't make any difference. Still failing badly.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting background image for Central Widget

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5. QWidget *w = new QWidget;
    6. mw.setCentralWidget(w);
    7. w->setAutoFillBackground(true);
    8. w->setStyleSheet("background-color: red;");
    9. mw.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    or...
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5. QWidget *w = new QWidget;
    6. mw.setCentralWidget(w);
    7. w->setAutoFillBackground(true);
    8. // w->setStyleSheet("background-color: red;");
    9. QPalette p = w->palette();
    10. p.setColor(QPalette::Background, Qt::yellow);
    11. w->setPalette(p);
    12. mw.show();
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    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 

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

    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

  9. #9
    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

  10. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.