Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QStandardItem QtCore.Qt.ForegroundRole issue when out of focus

QStandardItem QtCore.Qt.ForegroundRole issue when out of focus

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 581 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • alomA Offline
    alomA Offline
    alom
    wrote on last edited by alom
    #1

    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 focus

    qt 5.12 python 3.7 win10
    treeview.jpg

    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)
    
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved