Set bold lines in QTreeView

I have a custom QTreeView subclass in a pyqt application. I'm trying to give the user the ability to highlight and "highlight" (for lack of a better term) lines. Highlighted lines should have bold text and (optionally) a different background color. Any ideas?
I'm considering StyleSheets as an option, but so far haven't been able to get this to work. If I set the stylesheet QTreeView:

self.setStyleSheet("QTreeView::item:selected {border: 1px solid #567dbc;}")

      

I can't figure out how to manually enable "states" that will only contain the lines you want in a specific state. If I try to set the stylesheet of individual elements:

#modelIndex is a valid QModelIndex
modelIndex.internalPointer().setStyleSheet()

      

I am getting segfault.
I am not convinced that styles are the way to go, I am open to all ideas. Thanks!

+2


a source to share


2 answers


From what you said, it seems like the simplest solution would be to define a personalized item for your tree and set the font of the items to weight to bold when needed. Pls check if the example below will work for you, it should create a tree structure with a custom element delegate that will change the font style of the element.

import sys
from PyQt4 import QtGui, QtCore

class BoldDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        # decide here if item should be bold and set font weight to bold if needed 
        option.font.setWeight(QtGui.QFont.Bold)
        QtGui.QStyledItemDelegate.paint(self, painter, option, index)


class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        model = QtGui.QStandardItemModel()

        for k in range(0, 4):
            parentItem = model.invisibleRootItem()
            for i in range(0, 4):
                item = QtGui.QStandardItem(QtCore.QString("item %0 %1").arg(k).arg(i))
                parentItem.appendRow(item)
                parentItem = item

        self.view = QtGui.QTreeView()
        self.view.setModel(model)
        self.view.setItemDelegate(BoldDelegate(self))

        self.setCentralWidget(self.view)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

      



hope this helps, believes

+5


a source


I can think of several ways to do this. The easiest, if you have access to the model, is to add some tracking of the state of the indexes to the model and return the correct parameters for the roles requested in the data () function. The downside to this is that you are using the same model in different views and want the highlighting to be local to the same view.

The second easiest is probably to create a proxy model that tracks the data itself and gets all other data from the original model. In this situation (where you don't change the rows or columns of the original model) it will probably be pretty easy.



The most difficult one would be to make a custom delegate that keeps track of which rows / columns should be selected and draws itself differently based on the row / column of the model index it is drawing. You will need to have access to the delegate so you can specify which rows and columns should be set. You will also need to deal with what happens when the model changes.

+3


a source







All Articles