PDA

View Full Version : QPalette isn't working



roleroz
19th October 2006, 19:18
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


#include <QtGui/QApplication>
#include <QtGui/QLabel>
#include <QtGui/QTableWidget>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QTableWidget table(5,1);
table.setAlternatingRowColors(true);
table.show();
QPalette palette;
QWidget *widget;

widget = new QLabel("bla", &table);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(0, 0, widget);

widget = new QLabel("ble", &table);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(1, 0, widget);

widget = new QLabel("bli", &table);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(2, 0, widget);

widget = new QLabel("blo", &table);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(3, 0, widget);

widget = new QLabel("blu", &table);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(4, 0, widget);

return app.exec();
}

Any ideas??

munna
19th October 2006, 19:45
Is this what you are looking for ?




int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QTableWidget table(5,1);
QPalette palette;
QWidget *widget;

widget = new QLabel("bla", &table);
palette = widget->palette();
palette.setColor(QPalette::Window, Qt::green);
widget->setPalette(palette);
table.setCellWidget(0, 0, widget);

widget = new QLabel("ble", &table);
palette = widget->palette();
palette.setColor(QPalette::Window, Qt::red);
widget->setPalette(palette);
table.setCellWidget(1, 0, widget);

widget = new QLabel("bli", &table);
palette = widget->palette();
palette.setColor(QPalette::Window, Qt::green);
widget->setPalette(palette);
table.setCellWidget(2, 0, widget);

widget = new QLabel("blo", &table);
palette = widget->palette();
palette.setColor(QPalette::Window, Qt::red);
widget->setPalette(palette);
table.setCellWidget(3, 0, widget);

widget = new QLabel("blu", &table);
palette = widget->palette();
palette.setColor(QPalette::Window, Qt::green);
widget->setPalette(palette);
table.setCellWidget(4, 0, widget);

table.show();
return app.exec();
}

roleroz
19th October 2006, 19:56
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



int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QTableWidget table(5,1);
table.setAlternatingRowColors(true);
table.show();
QPalette palette;
QWidget *widget;

widget = new QLabel("bla", &table);
widget->setAutoFillBackground(true);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(0, 0, widget);

widget = new QLabel("ble", &table);
widget->setAutoFillBackground(true);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(1, 0, widget);

widget = new QLabel("bli", &table);
widget->setAutoFillBackground(true);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(2, 0, widget);

widget = new QLabel("blo", &table);
widget->setAutoFillBackground(true);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(3, 0, widget);

widget = new QLabel("blu", &table);
widget->setAutoFillBackground(true);
palette = widget->palette();
palette.setColor(QPalette::Base, Qt::green);
palette.setColor(QPalette::AlternateBase, Qt::red);
widget->setPalette(palette);
table.setCellWidget(4, 0, widget);

return app.exec();
}

munna
19th October 2006, 20:25
Try this:



int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QTableWidget table(5,1);
QPalette palette;
QWidget *widget;

widget = new QLabel("bla", &table);
widget->setAutoFillBackground(true);
palette = widget->palette;
palette.setColor(QPalette::Window, Qt::green);
widget->setPalette(palette);
table.setCellWidget(0, 0, widget);

widget = new QLabel("ble", &table);
widget->setAutoFillBackground(true);
palette = widget->palette();
palette.setColor(QPalette::Window, Qt::red);
widget->setPalette(palette);
table.setCellWidget(1, 0, widget);

widget = new QLabel("bli", &table);
widget->setAutoFillBackground(true);
palette = widget->palette();
palette.setColor(QPalette::Window, Qt::green);
widget->setPalette(palette);
table.setCellWidget(2, 0, widget);

widget = new QLabel("blo", &table);
widget->setAutoFillBackground(true);
palette = widget->palette();
palette.setColor(QPalette::Window, Qt::red);
widget->setPalette(palette);.
table.setCellWidget(3, 0, widget);

widget = new QLabel("blu", &table);
widget->setAutoFillBackground(true);
palette = widget->palette();
palette.setColor(QPalette::Window, Qt::green);
widget->setPalette(palette);
table.setCellWidget(4, 0, widget);

table.show();
return app.exec();
}

roleroz
19th October 2006, 20:27
I did, didn't work, as i need to define both colors

jpn
19th October 2006, 21:15
The alternating row colors (base and alternate base) are supposed to set for the table itself, not for widgets set on the table.


QPalette p = table.palette();
p.setColor(QPalette::Base, Qt::green);
p.setColor(QPalette::AlternateBase, Qt::red);
table.setPalette(p);


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.