QStandardItem QtCore.Qt.ForegroundRole issue when out of focus
-
hello,
I have a treeview where I'd like to control the text color and font size based on leaf and node of the model.
Currently I'm assigning a color and font to an items data in the QtCore.Qt.ForegroundRole and FontRole. This is working as expected, until the user looses focus on the tree. The text color will go black, I want the text to remain its assigned forgroundRole.Is there a role I can control the this on the item's data or is this part of the treeview's pallet's HighlightedText property?
The only way I can see to control this atm is to connect a function to the view based on entered or pressed. I have the model index, so I can get the item's ForegroundRole and assign a new palette to the view overriding the HighlightedText on each item (this seems very wrong to me)
Cheers
to recreate:
open treeview
select item
select lineedit to change focusqt 5.12 python 3.7 win10
from PySide2 import QtWidgets, QtGui, QtCore import sys style = "QTreeView{background-color: rgb(18, 18, 18);outline: none;padding-left: 10px;}" \ "QTreeView::item:selected {border: none;}" \ "QTreeView::item:hover {border: none;}" \ "QTreeView::item:hover:selected {border: none;}" \ "QTreeView::item:selected:!active{outline: none;}" def populate_model(model): topics = { 'parent_a': {'parent_b': ['node', 'node', 'node'], 'parent_bb': ['node'], 'parent_bbb': ['node']}, 'parent_aa': ['node', 'node', 'node'], } topic_items = [] for topic in topics.items(): parent, children = topic item = QtGui.QStandardItem(parent) item.setDropEnabled(False) item.setEditable(False) child_items = [] title_font = QtGui.QFont() title_font.setPixelSize(20) category_font = QtGui.QFont() category_font.setPixelSize(14) node_font = QtGui.QFont() node_font.setPixelSize(14) item.setData(title_font, QtCore.Qt.FontRole) item.setData(QtGui.QColor(200, 200, 200), QtCore.Qt.ForegroundRole) if type(children) is dict: for sub_child in children.items(): child, grand_children = sub_child child_item = QtGui.QStandardItem(child) child_item.setData(category_font, QtCore.Qt.FontRole) child_item.setData(QtGui.QColor(140, 140, 140), QtCore.Qt.ForegroundRole) gc_items = [] for gc in grand_children: gc_item = QtGui.QStandardItem(gc) gc_item.setData(node_font, QtCore.Qt.FontRole) gc_item.setData(QtGui.QColor(111, 172, 175), QtCore.Qt.ForegroundRole) gc_items.append(gc_item) child_item.appendColumn(gc_items) child_items.append(child_item) item.appendColumn(child_items) else: for child in children: child_item = QtGui.QStandardItem(child) child_item.setData(node_font, QtCore.Qt.FontRole) child_item.setData(QtGui.QColor(111, 172, 175), QtCore.Qt.ForegroundRole) child_items.append(child_item) item.appendColumn(child_items) topic_items.append(item) model.appendColumn(topic_items) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) widget = QtWidgets.QWidget() widget.setStyleSheet(style) line_edit = QtWidgets.QLineEdit() tree = QtWidgets.QTreeView() tree.setHeaderHidden(True) layout = QtWidgets.QVBoxLayout() model = QtGui.QStandardItemModel() tree.setModel(model) layout.addWidget(line_edit) layout.addWidget(tree) widget.setLayout(layout) populate_model(model) widget.show() rec = app.exec_() sys.exit(rec)