QT CSS Property Selectors

I have a tree widget that I use for the user / room concept. How can I style rooms regardless of the users in the rooms? I assume this has something to do with sorting QT properties in CSS? I would like to be able to do this:

QTreeView::item[isUser="true"] { background: blue; }
QTreeView::item[isRoom="true"] { background: red; }
+3
source share
2 answers

Since the elements in the model are not QObjects (or QWidgets), you cannot add a property to an element or style them using style sheets.

I have two suggestions for doing what you want to do:

1) ( ++) QTreeView QStandardItemModel, QStandardItem, QStandardItem::setBackground() Qt::blue Qt::red , - .

2) (++ CSS) QStyledItemDelegate, QTreeView. QStyledItemDelegate::paint() QLabel , QLabel. :

QLabel[isUser="true"] { background: blue; }
QLabel[isRoom="true"] { background: red; }
+3

, , , setProperty , setItemWidget, QLabel QTreeWidgetItem. , " QTreeWidgetItem", , , QLabel, QTreeWidgetItem. my topLevelItem QTreeWidget :

QTreeWidgetItem *topItem = ui->treeWidget->topLevelItem(0);
currentLabel = new QLabel;
currentLabel->setProperty("room",true);
currentLabel->setText(QString("Room Lobby"));
ui->treeWidget->setItemWidget(topItem,0,currentLabel);`

QLabel[room="true"] { background: red; }
+1

Source: https://habr.com/ru/post/1763872/


All Articles