Results 1 to 6 of 6

Thread: QPalette isn't working

  1. #1
    Join Date
    Aug 2006
    Posts
    15
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy QPalette isn't working

    I'm trying to have a list of special kind of widgets, with different background colors for each one and the alternatingRowColor flag doesn't seem to work

    I'm using a QTableWidget and setting the widgets for each cell. Using the 4.1.4 version of QT

    have some code, that should do it, but doesn't work

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtGui/QLabel>
    3. #include <QtGui/QTableWidget>
    4.  
    5. int main(int argc, char* argv[])
    6. {
    7. QApplication app(argc, argv);
    8. QTableWidget table(5,1);
    9. table.setAlternatingRowColors(true);
    10. table.show();
    11. QPalette palette;
    12. QWidget *widget;
    13.  
    14. widget = new QLabel("bla", &table);
    15. palette = widget->palette();
    16. palette.setColor(QPalette::Base, Qt::green);
    17. palette.setColor(QPalette::AlternateBase, Qt::red);
    18. widget->setPalette(palette);
    19. table.setCellWidget(0, 0, widget);
    20.  
    21. widget = new QLabel("ble", &table);
    22. palette = widget->palette();
    23. palette.setColor(QPalette::Base, Qt::green);
    24. palette.setColor(QPalette::AlternateBase, Qt::red);
    25. widget->setPalette(palette);
    26. table.setCellWidget(1, 0, widget);
    27.  
    28. widget = new QLabel("bli", &table);
    29. palette = widget->palette();
    30. palette.setColor(QPalette::Base, Qt::green);
    31. palette.setColor(QPalette::AlternateBase, Qt::red);
    32. widget->setPalette(palette);
    33. table.setCellWidget(2, 0, widget);
    34.  
    35. widget = new QLabel("blo", &table);
    36. palette = widget->palette();
    37. palette.setColor(QPalette::Base, Qt::green);
    38. palette.setColor(QPalette::AlternateBase, Qt::red);
    39. widget->setPalette(palette);
    40. table.setCellWidget(3, 0, widget);
    41.  
    42. widget = new QLabel("blu", &table);
    43. palette = widget->palette();
    44. palette.setColor(QPalette::Base, Qt::green);
    45. palette.setColor(QPalette::AlternateBase, Qt::red);
    46. widget->setPalette(palette);
    47. table.setCellWidget(4, 0, widget);
    48.  
    49. return app.exec();
    50. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas??

  2. #2
    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 isn't working

    Is this what you are looking for ?

    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QTableWidget table(5,1);
    5. QPalette palette;
    6. QWidget *widget;
    7.  
    8. widget = new QLabel("bla", &table);
    9. palette = widget->palette();
    10. palette.setColor(QPalette::Window, Qt::green);
    11. widget->setPalette(palette);
    12. table.setCellWidget(0, 0, widget);
    13.  
    14. widget = new QLabel("ble", &table);
    15. palette = widget->palette();
    16. palette.setColor(QPalette::Window, Qt::red);
    17. widget->setPalette(palette);
    18. table.setCellWidget(1, 0, widget);
    19.  
    20. widget = new QLabel("bli", &table);
    21. palette = widget->palette();
    22. palette.setColor(QPalette::Window, Qt::green);
    23. widget->setPalette(palette);
    24. table.setCellWidget(2, 0, widget);
    25.  
    26. widget = new QLabel("blo", &table);
    27. palette = widget->palette();
    28. palette.setColor(QPalette::Window, Qt::red);
    29. widget->setPalette(palette);
    30. table.setCellWidget(3, 0, widget);
    31.  
    32. widget = new QLabel("blu", &table);
    33. palette = widget->palette();
    34. palette.setColor(QPalette::Window, Qt::green);
    35. widget->setPalette(palette);
    36. table.setCellWidget(4, 0, widget);
    37.  
    38. table.show();
    39. return app.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2006
    Posts
    15
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPalette isn't working

    No, what i'm trying to do is each odd label (bla, bli and blu) to be painted with a green background, and each even label (ble and blo) to be painted with a red background

    I have changed the code a little bit, so each label is painted green, but cannot get the alternating row colors working

    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QTableWidget table(5,1);
    5. table.setAlternatingRowColors(true);
    6. table.show();
    7. QPalette palette;
    8. QWidget *widget;
    9.  
    10. widget = new QLabel("bla", &table);
    11. widget->setAutoFillBackground(true);
    12. palette = widget->palette();
    13. palette.setColor(QPalette::Base, Qt::green);
    14. palette.setColor(QPalette::AlternateBase, Qt::red);
    15. widget->setPalette(palette);
    16. table.setCellWidget(0, 0, widget);
    17.  
    18. widget = new QLabel("ble", &table);
    19. widget->setAutoFillBackground(true);
    20. palette = widget->palette();
    21. palette.setColor(QPalette::Base, Qt::green);
    22. palette.setColor(QPalette::AlternateBase, Qt::red);
    23. widget->setPalette(palette);
    24. table.setCellWidget(1, 0, widget);
    25.  
    26. widget = new QLabel("bli", &table);
    27. widget->setAutoFillBackground(true);
    28. palette = widget->palette();
    29. palette.setColor(QPalette::Base, Qt::green);
    30. palette.setColor(QPalette::AlternateBase, Qt::red);
    31. widget->setPalette(palette);
    32. table.setCellWidget(2, 0, widget);
    33.  
    34. widget = new QLabel("blo", &table);
    35. widget->setAutoFillBackground(true);
    36. palette = widget->palette();
    37. palette.setColor(QPalette::Base, Qt::green);
    38. palette.setColor(QPalette::AlternateBase, Qt::red);
    39. widget->setPalette(palette);
    40. table.setCellWidget(3, 0, widget);
    41.  
    42. widget = new QLabel("blu", &table);
    43. widget->setAutoFillBackground(true);
    44. palette = widget->palette();
    45. palette.setColor(QPalette::Base, Qt::green);
    46. palette.setColor(QPalette::AlternateBase, Qt::red);
    47. widget->setPalette(palette);
    48. table.setCellWidget(4, 0, widget);
    49.  
    50. return app.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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 isn't working

    Try this:

    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QTableWidget table(5,1);
    5. QPalette palette;
    6. QWidget *widget;
    7.  
    8. widget = new QLabel("bla", &table);
    9. widget->setAutoFillBackground(true);
    10. palette = widget->palette;
    11. palette.setColor(QPalette::Window, Qt::green);
    12. widget->setPalette(palette);
    13. table.setCellWidget(0, 0, widget);
    14.  
    15. widget = new QLabel("ble", &table);
    16. widget->setAutoFillBackground(true);
    17. palette = widget->palette();
    18. palette.setColor(QPalette::Window, Qt::red);
    19. widget->setPalette(palette);
    20. table.setCellWidget(1, 0, widget);
    21.  
    22. widget = new QLabel("bli", &table);
    23. widget->setAutoFillBackground(true);
    24. palette = widget->palette();
    25. palette.setColor(QPalette::Window, Qt::green);
    26. widget->setPalette(palette);
    27. table.setCellWidget(2, 0, widget);
    28.  
    29. widget = new QLabel("blo", &table);
    30. widget->setAutoFillBackground(true);
    31. palette = widget->palette();
    32. palette.setColor(QPalette::Window, Qt::red);
    33. widget->setPalette(palette);.
    34. table.setCellWidget(3, 0, widget);
    35.  
    36. widget = new QLabel("blu", &table);
    37. widget->setAutoFillBackground(true);
    38. palette = widget->palette();
    39. palette.setColor(QPalette::Window, Qt::green);
    40. widget->setPalette(palette);
    41. table.setCellWidget(4, 0, widget);
    42.  
    43. table.show();
    44. return app.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by munna; 19th October 2006 at 19:31.

  5. #5
    Join Date
    Aug 2006
    Posts
    15
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPalette isn't working

    I did, didn't work, as i need to define both colors

  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 isn't working

    The alternating row colors (base and alternate base) are supposed to set for the table itself, not for widgets set on the table.
    Qt Code:
    1. QPalette p = table.palette();
    2. p.setColor(QPalette::Base, Qt::green);
    3. p.setColor(QPalette::AlternateBase, Qt::red);
    4. table.setPalette(p);
    To copy to clipboard, switch view to plain text mode 

    There's not much point using QLabels as item widgets in QTableWidget if you're just showing text. Item widgets prevent the table from behaving correctly, like you cannot select cells containing item widgets and so on. You should use QTableWidgetItems instead and set text to them.
    J-P Nurmi

Similar Threads

  1. Connect not working
    By xgoan in forum Qt Programming
    Replies: 4
    Last Post: 24th July 2006, 11:27
  2. Replies: 1
    Last Post: 11th June 2006, 22:25
  3. Mac OS X UI not working
    By hvengel in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2006, 01:02
  4. Signals/Slots stopped working
    By Jimmy2775 in forum Qt Programming
    Replies: 8
    Last Post: 31st March 2006, 21:11
  5. QSettings - beginReadArray not working
    By Mike in forum Qt Programming
    Replies: 7
    Last Post: 9th January 2006, 21:24

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.