0

System: Linux Mint, QT Creator from Repo -> QT Version 5.2, C++)

I've created a Customwidget wich Im using inside a QTreeView

OwnItem *OI = new OwnItem;
QTreeWidgetItem *itemN = new QTreeWidgetItem();
ui->ProjektListe->addTopLevelItem(itemN);
ui->ProjektListe->setItemWidget(itemN, 0, OI);

What I want is to set up a Stylesheet for the QTreeWidget including a Backgroundcolor and a Textcolor in Normal Mode and Selected Mode.

So far:

QTreeWidget::item{
    background-color: rgb(255, 255, 255);
    color: rgb(255, 255, 0);
}

QTreeWidget::item:selected{
    background-color: #157efb;
    color: rgb(255, 0, 0);
}

The Problem is that The Backgroundcolor works, the Color (TextColor) not (in both Cases). Im aware that when stylesheets for childs are set separately this won't work but the widget itself and all of its children (Some Labels and Buttons) are "Sylesheet" free.

The only Case "color: .... " for TextColor works is this case

QWidget{
    color: rgb(85, 0, 0);
}

But this wont work with the "selected" status

hypnomaki
  • 593
  • 9
  • 22

1 Answers1

0

My anser is in C++ not for the CSS but you can create a QPalette then set the values that you want to with the function void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color ) so for you it should me something like:

QTreeWidget tree(a);
QPalette palette;
palette.setColor(QPalette::Window, QColor(255, 255, 255));
palette.setColor(QPalette::WindowText, QColor(255, 255, 0));
palette.setColor(QPalette::Highlight, QColor(255, 0, 0))
palette.setColor(QPalette::HighlightedText, QColor(0, 0, 255));

QList<QTreeWidgetItem> treeItems = tree.findChildren<QTreeWidgetItem*>();
    foreach (QTreeWidgetItem *w : treeItems) {
        w.setPalette(palette);
    }

the findChildren will return a list with all the children to the widget then you can set the palette. To find the list of the colors groups you can go here: http://qt-project.org/doc/qt-4.8/qpalette.html#setColor then click on the ColorGroup type in the parameter then you will be are here: http://qt-project.org/doc/qt-4.8/qpalette.html#ColorGroup-enum

Good luck !

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
  • I just implemented your Code but it also doesn't work. I also couldn't find any examples of setting the foreground color in the qtdocs. Perhaps its not meant to work this way. (At least for QWidgets). I just implemented a QItemDelegate. This works fine as far. A other solution would be to use the Standart Items. In this case the "color" attribute also works. I could make more Collumns. – hypnomaki Feb 25 '15 at 18:09
  • But my custom QWidget is a little bit complex so reinvent this with QTreeWidgetItem and Collumns will not be an easy task – hypnomaki Feb 25 '15 at 18:17