Results 1 to 8 of 8

Thread: QPalette help pls

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QPalette help pls

    Hi,

    I am setting the palette of a QLabel. But what I am seeing is that the color looks different on the winxp theme and it is different on the classic theme.

    But when I draw something on the widget it is same on all the themes. Is this correct or am I missing something ?

    Please Help
    Thanks a lot.

  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: QPalette help pls

    It is correct. WindowsXP theme doesn't allow all combinations of colours for some widgets. If you want to know why -- ask Microsoft. If you draw yourself, you don't use windows calls so you have total control over what you draw.

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPalette help pls

    Thanks for the reply.

    Is there a way by which I can make the background of a label have same color in both the themes ?

    Thanks a lot.

  4. #4
    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: QPalette help pls

    You can always subclass windowsXP style and reimplement what you need. But you should be able to set the background of a label without it, I can't believe it's not possible...

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPalette help pls

    So there is no way by which i can simply change the background of the label?

  6. #6
    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: QPalette help pls

    The usual way works just fine for me on WinXP:
    Qt Code:
    1. QPalette p = label->palette();
    2. p.setColor(label->backgroundRole(), Qt::magenta);
    3. label->setPalette(p);
    4. label->setAutoFillBackground(true);
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPalette help pls

    Can you please try one more thing ?

    Set the background of label as QColor(140,134,115) and draw (using the paintEvent) something on the parent widget with the same color.

    Is it looking ok to you in both the xp theme and the classic theme?

    Thanks a lot for your time.

  8. #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: QPalette help pls

    I'm not sure if I understood it correctly..
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class ColorDialog: public QDialog
    4. {
    5. protected:
    6. void paintEvent(QPaintEvent* event)
    7. {
    8. // draw "something" on parent widget with the same color
    9. QDialog::paintEvent(event);
    10. QPainter p(this);
    11. p.fillRect(rect(), QColor(140,134,115));
    12. }
    13. };
    14.  
    15. class ColorLabel: public QLabel
    16. {
    17. public:
    18. ColorLabel(const QString& text, QWidget* parent = 0)
    19. : QLabel(text, parent)
    20. {
    21. // a label with background palette..
    22. setAlignment(Qt::AlignCenter);
    23. QPalette p = palette();
    24. p.setColor(backgroundRole(), QColor(140,134,115));
    25. setPalette(p);
    26. setAutoFillBackground(true);
    27. // a frame just to indicate where are the boundaries of the label
    28. setFrameStyle(QFrame::Box);
    29. }
    30. };
    31.  
    32. int main(int argc, char *argv[])
    33. {
    34. QApplication a(argc, argv);
    35. ColorDialog* dialog = new ColorDialog;
    36. ColorLabel* label = new ColorLabel("test");
    37. lay->addWidget(label);
    38. dialog->setLayout(lay);
    39. dialog->show();
    40. return a.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 
    If you mean something different, please supply a minimal compilable example to test.
    Attached Images Attached Images
    J-P Nurmi

Similar Threads

  1. QPalette won't set QLabel's Window & Foreground color
    By koklim in forum Qt Programming
    Replies: 6
    Last Post: 23rd January 2006, 10:27

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.