0

I try to set the color of a selected item in a QTreeView using the QAbstractItemModel in PySide2.

So what i am trying right now is creating a context menu and then having a method to colorize that selected item.

def colorize_item(self):
    index = self.treeview.selectionModel().currentIndex()

    self.model.setData(index, QtGui.QBrush(QtCore.Qt.red), QtCore.Qt.ForegroundRole)

Sadly it is not working at all. I successfully used the data() method in the QAbstactItemModel to change the color of all items text but i need to colorize specific items in the TreeView. I also used Google to find a solution but nothing worked for me :(

EDIT: i also tried to not override the setData() method just to check if my override just messed something up.

EDIT: Maybe a better question would be what to implement in setData to make it work

zwusel
  • 171
  • 12
  • QAbstractItemModel can not be used directly, so I think you have a class that inherits from QAbstractItemModel, so in that case you must implement the setData() method, it would be great if you show a [mcve] – eyllanesc Jan 15 '19 at 20:31
  • I found a solution. It is quite easy to give the items a bool variable (lets say to_be_colorized), set that to true if needed and then in the data() method check for that variable and return the correct brush. – zwusel Jan 16 '19 at 10:41

0 Answers0