Results 1 to 16 of 16

Thread: Backgound color of QWidget

  1. #1
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Backgound color of QWidget

    Hi ,

    I want to put different backgound color for different widget. In Qt3 there was some thing setBackgoundColor() methods. In Qt-4.1, I got setBackgroundRole(), it takes QPalete::-- as argument. I want to set backgound color through QColor object, because from QColorDialog i will get QColor object. Is there any method to achieve this.

    Thanks
    Jnana

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Backgound color of QWidget

    Qt Code:
    1. QPalette p( widget->palette() );
    2. p.setColor( QPalette::Window, Qt::red );
    3. widget->setPalette( p );
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 17th March 2006 at 10:14. Reason: changed Qt::Window to QPalette::Window

  3. #3
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Backgound color of QWidget

    Yup,

    You have to use QPalette now

    Something like this :
    Qt Code:
    1. QPalette *palette = your_widget->palette();
    2. palette->setBrush(QPalette::Base, QColor(your_color));
    3. your_widget->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

    Guilugi.

  4. #4
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Backgound color of QWidget

    Oops

    Too fast for me, Jacek.

    I bow

  5. #5
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Backgound color of QWidget

    Even there is a simpler method than that:

    QColor c1="<your_color>";
    setPalette(c1);

  6. The following user says thank you to Sarma for this useful post:

    jnana (15th March 2006)

  7. #6
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Backgound color of QWidget

    Thanks all of u.

  8. #7
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Backgound color of QWidget

    Hi All,

    How to set background color of QLabel objects. I tried with setPalette(), it does not work for labels, it works for other case.

    Thanks
    Jnana

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Backgound color of QWidget

    Hmm, it certainly does work for a QLabel..

    Are you sure that you are setting the palette's color for correct role?
    Btw, I suggest using the method introduced by jacek.. (just change Qt::Window -> QPalette::Window).

    (Are you sure that you don't override the color in the paint event?)

  10. #9
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Backgound color of QWidget

    Hi ,

    I tried with following code. I got no result.
    Qt Code:
    1. QPalette p( ui.label21->palette() );
    2. p.setBrush( QPalette::Window, Qt::darkCyan );
    3. ui.label21->setPalette(p);
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Backgound color of QWidget

    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QPalette>
    4.  
    5. int main( int argc, char **argv )
    6. {
    7. QApplication app( argc, argv );
    8.  
    9. QLabel l;
    10. QPalette p( l.palette() );
    11. p.setColor( QPalette::Window, Qt::darkCyan );
    12. l.setPalette( p );
    13.  
    14. l.show();
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    I use Qt 4.1.1 under PLD Linux.
    Attached Images Attached Images

  12. #11
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: Backgound color of QWidget

    Whilst the example below or above works,
    this doesn't:

    Qt Code:
    1. #ifndef BWIDGET_H
    2. #define BWIDGET_H
    3.  
    4. #include <QWidget>
    5. class QLabel;
    6.  
    7. class BWidget : public QWidget
    8. {
    9. public:
    10. BWidget(QWidget *parent = 0);
    11. ~BWidget(){};
    12.  
    13. private:
    14. QLabel *_myLabel;
    15. };
    16.  
    17. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "bwidget.h"
    2. #include <QLabel>
    3.  
    4. BWidget::BWidget(QWidget *parent) : QWidget(parent)
    5. {
    6. _myLabel = new QLabel(this);
    7. QPalette p = _myLabel->palette();
    8. p.setColor(QPalette::Window, Qt::red);
    9. _myLabel->setPalette(p);
    10. _myLabel->setText("OK");
    11. // setPalette(p);
    12. }
    To copy to clipboard, switch view to plain text mode 

    After the window is redrawn, the background colour of the label is displayed as desired for a split second (e.g. after it was hidden behind another window and it's shown again. You have to toggle very fast to see it, though).
    But something redraws it with the standard gray.
    Any ideas?


  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Backgound color of QWidget

    IMO this is a Qt bug (I use Qt 4.1.1 under PLD Linux). One can reproduce it even in Qt Designer (just create a form with a label, change its background colour with palette editor and see the preview).

  14. #13
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: Background color of QWidget

    mmh, really looks like a bug.
    I played around with it a bit and whilst your example works even if modified (using the background image) mine doesn't work.
    Somewhere in the doc is an example how to transform the QT3 setPaletteBackgroundPixmap to QT4.
    Using QT += qt3support int the pro file and a Q3TextEdit the background image or colour still shows up.
    So if it's a bug. Is Trolltech aware of it?

    Qt Code:
    1. #include "bwidget.h"
    2.  
    3. #include <QLabel>
    4. #include <QTextEdit>
    5. #include <Q3TextEdit>
    6. #include <QPalette>
    7. #include <QPixmap>
    8. #include <QApplication>
    9. #include <QVBoxLayout>
    10.  
    11. BWidget::BWidget(QWidget *parent) : QWidget(parent)
    12. {
    13. QVBoxLayout *layout = new QVBoxLayout(this);
    14. _myLabel = new QLabel();
    15. _myLabel->setText("QLabel");
    16. QTextEdit *anEdit = new QTextEdit();
    17. anEdit->append("QTextEdit");
    18. anEdit->setFixedHeight(24);
    19. Q3TextEdit *a3Edit = new Q3TextEdit;
    20. a3Edit->append("Q3TextEdit");
    21. layout->addWidget(_myLabel);
    22. layout->addWidget(anEdit);
    23. layout->addWidget(a3Edit);
    24.  
    25. QPalette p = _myLabel->palette();
    26. // p.setColor(QPalette::Window, Qt::red);
    27. p.setColor(_myLabel->backgroundRole(), Qt::red);
    28. QPixmap pixmap(QCoreApplication::applicationDirPath () +"/img/debug.png");
    29. p.setBrush( _myLabel->backgroundRole(), QBrush(pixmap));
    30. _myLabel->setPalette(p);
    31. anEdit->setPalette(p);
    32.  
    33. a3Edit->setPaletteBackgroundPixmap ( pixmap );
    34. // a3Edit->setBackgroundColor(Qt::red);
    35.  
    36. // setPalette(p);
    37. setLayout(layout);
    38. }
    To copy to clipboard, switch view to plain text mode 

    (Using Qt 4.1.1 self compiled on Linux - SuSE 9.3)
    Attached Images Attached Images

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Background color of QWidget

    Quote Originally Posted by Moppel
    So if it's a bug. Is Trolltech aware of it?
    I couldn't find anything similar in the task tracker, so it will be better to ask the Trolls.

  16. #15
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: Background color of QWidget

    My previous example was incorrect.
    For the background of the text widget you need to set QPalette::Brush.
    So the example below works correctly for the textedit.
    Uncomment either the setColor or the setBrush to see the picture or the colour in the background of the edit.
    No matter that you uncomment for the label, no changes can be seen.
    That is so for qt 4.1.1 on Windows, SuSE 9.3, SuSE 10.0 and qt 4.1.0 on SuSE 9.3 and 10.0.


    Qt Code:
    1. #include "bwidget.h"
    2.  
    3. #include <QLabel>
    4. #include <QTextEdit>
    5. #include <QPalette>
    6. #include <QPixmap>
    7. #include <QApplication>
    8. #include <QVBoxLayout>
    9.  
    10. BWidget::BWidget(QWidget *parent) : QWidget(parent)
    11. {
    12. QVBoxLayout *layout = new QVBoxLayout(this);
    13. _myLabel = new QLabel();
    14. _myLabel->setText("OK");
    15. QTextEdit *anEdit = new QTextEdit();
    16. anEdit->append("an edit");
    17.  
    18. layout->addWidget(_myLabel);
    19. layout->addWidget(anEdit);
    20.  
    21. QPixmap pixmap(QCoreApplication::applicationDirPath () +"/img/debug.png");
    22.  
    23. QPalette p = _myLabel->palette();
    24. p.setColor(QPalette::WindowText, Qt::green);
    25. p.setColor(QPalette::Window, Qt::red);
    26. // p.setBrush(QPalette::Window, QBrush(pixmap));
    27. // p.setColor(_myLabel->backgroundRole(), Qt::red);
    28. _myLabel->setPalette(p);
    29.  
    30. // p.setColor(QPalette::Base, Qt::red);
    31. p.setBrush( QPalette::Base, QBrush(pixmap));
    32. anEdit->setPalette(p);
    33.  
    34.  
    35. // setPalette(p);
    36. setLayout(layout);
    37. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  17. #16
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: Backgound color of QWidget

    Right, no bug. It's all in the documentation.
    If the line below is added, it works.

    Qt Code:
    1. _myLabel->setAutoFillBackground ( true );
    To copy to clipboard, switch view to plain text mode 

    Thanks Volker from Trolltech to pointing this out to me.

  18. The following user says thank you to Moppel for this useful post:

    barnabyr (15th April 2006)

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2008, 15:36
  2. Dynamic updates of a QWidget in a QScrollArea
    By plamkata in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2008, 23:45
  3. Error in put one QWidget in another QWidget
    By xjtu in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2008, 16:05

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.