PDA

View Full Version : QPalette::brush



tpf80
16th July 2007, 23:42
Currently, I am using a QTableWidget that has alternating colors, however I wanted to alternate the color every other row instead of every row. I made a function for this and it works properly. The problem is, that I still want to use QPalette::AlternateBase and QPalette::Base for the colors.

Currently I can color the brush as follows:


QBrush tableNoHighlight;
tableNoHighlight.setStyle (Qt::SolidPattern);
tableNoHighlight.setColor(Qt::white);

QBrush tableYesHighlight;
tableYesHighlight.setStyle (Qt::SolidPattern);
tableYesHighlight.setColor(Qt::lightGray);

However, since the colors are hard set, the highlighting does not match the particular look and feel of the OS that the program might be run in. Thus I wanted to use QPalette::AlternateBase and QPalette::Base.

I tried the following:


tableNoHighlight = QPalette::brush(QPalette::AlternateBase);
tableNoHighlight = QPalette::brush(QPalette::Base);

and I got this error:


alculatorform.cpp:81: error: cannot call member function `const QBrush& QPalette::brush(QPalette::ColorRole) const' without object
alculatorform.cpp:87: error: cannot call member function `const QBrush& QPalette::brush(QPalette::ColorRole) const' without object

so I tried:


tableNoHighlight = this->brush(QPalette::AlternateBase);
tableNoHighlight = this->brush(QPalette::Base);

and


tableNoHighlight = ui->brush(QPalette::AlternateBase);
tableNoHighlight = ui->brush(QPalette::Base);

and both yielded errors about having no member named brush.

How can I get the brush of QPalette::AlternateBase and QPalette::Base (or at least the color value) to use elsewhere?

rajesh
17th July 2007, 07:03
use the following:

setBackgroundRole( QPalette::AlternateBase);
setBackgroundRole( QPalette::Base);

OR

QPalette palette;
palette.setColor(m_tabel->backgroundRole(), Qt::transparent); // any Qt::color
m_tabel->setPalette(palette);

tpf80
17th July 2007, 20:08
perhaps i didn't explain well enough what I am doing. I am coloring the rows in a QTableWidget in alternating colors, but 2 rows 1 color, then 2 rows the other. The only way I have found so far was to color the cells as I generate the rows:



ui.tablewidget->item(0, 3)->setBackground(highlightBrush);
ui.tablewidget->item(0, 4)->setBackground(highlightBrush);
//etc...

This works, however it does not completely match the base and alternate base color (because I don't know how to construct brushes that take from those colors)

The background role method:


//this produces errors as a qtablewidgetitem does not have setBackgroundRole()
ui.tableWidget->item(0, 1)->setBackgroundRole(QPalette::AlternateBase);
ui.tableWidget->item(0, 2)->setBackgroundRole(QPalette::Base);

Will not work because QTableWidgetItem does not accept setBackgroundRole, it only accepts a brush in setBackground()

so all I need to know really, is how to create a brush from the color used in QPalette::AlternateBase and QPalette::Base or use the existing brush for those colors (but how to reference it I have no clue.

jacek
17th July 2007, 22:11
so all I need to know really, is how to create a brush from the color used in QPalette::AlternateBase and QPalette::Base
Ask QPalette to give you one.

tpf80
17th July 2007, 22:20
I actually tried this (see above) but I got errors. (this was the way I really wanted to do it)


tableNoHighlight = QPalette::brush(QPalette::AlternateBase);
tableNoHighlight = QPalette::brush(QPalette::Base);

it asks for an object, but I am not quite sure what object its asking for.

jacek
17th July 2007, 23:21
it asks for an object, but I am not quite sure what object its asking for.
QPalette::brush() isn't a static method, so you have to create a QPalette object first:
QPalette p;
tableNoHighlight = p.brush(QPalette::Base);

tpf80
17th July 2007, 23:23
ah I see.. I knew it was something simple that I was missing...