View Full Version : Alternating colors in QTableWidget

4th March 2011, 17:22

We are trying to use some custom items in a QTableWidget via setCellWidget() call. We are adding some QPushButton and QComboBox objects.

That works fine, but we also want to use the alternating colors feature of QTableView. In our stylesheet we specify alternating colors for QPushButton, QComboBox, etc like this:

QMenu {
selection-color: black;
selection-background-color: lightgrey;
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 black, stop: 1 rgb(128, 128, 128));
alternate-background-color: rgb(255, 0, 127);
min-width: 14px;
min-height: 14px;
border: 1px solid gray;
padding: 2px;
border-radius: 3px;

However, when push buttons and combo boxes are added to the table they all appear the same color with a little bit of the background color being drawn at the very corners.

Any ideas on how to fix this?


5th March 2011, 00:05
Widget's are not tied to the model in any way, they have no concept of being "alternate" and the view doesn't handle them in any way other than moving them when it scrolls its viewport.

5th March 2011, 18:20
And since the view has no idea about the buttons, you need to set the backgroundRole for the button's palette to the alternate color manually when you insert the buttons into the cell. Alternatively, set the button's autoFillBackground to false and the underlying cell color will be visible. You will need to remove the background color specification from the style sheet, because the style sheet overrides this setting.

You could also try playing around with the button opacity, but this will probably affect the foreground text as well as the background fill.