Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidgetItem.setItemWidget(item, k, QCheckBox()) not working (checkbox doesn't show up)
Qt 6.12 LTS is out! See what's new in the release blog

QTreeWidgetItem.setItemWidget(item, k, QCheckBox()) not working (checkbox doesn't show up)

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 2.1k 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.
  • enjoysmathE Offline
    enjoysmathE Offline
    enjoysmath
    wrote on last edited by
    #1

    I'm trying to recursively build a QTreeWidget using a python dictionary. A "[ ]" in a dictionary key/value means add a checkbox. No checkboxes show up no matter what column k I add the checkbox to. I also have another solution for adding a checkbox using one and only one column, should I go ahead and use that? It would be nice to have two columns though, since the code is cleaner.

    from PyQt5.QtWidgets import QTreeWidgetItem, QCheckBox, QDockWidget
    from PyQt5.QtCore import Qt
    from ui_cec_spec_filter_dock import Ui_CECSpecFilterDock
    from collections import OrderedDict
    
    HDMI_CEC_Spec = OrderedDict({
        "Initiator" : {
            "TV | [ ]": {
                "Nibble": "0",
                "Comment": "",
            },
            "Recording Device 1 | [ ]": {
                "Nibble": "1",
                "Comment": "_",
            },
        },
        "Follower" : {
        },
        "Command" : {
            
        }
    })
    
    class CECSpecFilterDock(QDockWidget, Ui_CECSpecFilterDock):
        def __init__(self, new=True):
            super().__init__()
            super().__init__()
            self.setupUi(self)
            #self.setFocusPolicy(Qt.NoFocus)         # BUGFIX: gets rid of weird dotted selection line around tree items
            self.checkbox = {}
            if new:
                self.buildTree(HDMI_CEC_Spec, self.treeWidget.invisibleRootItem()) 
    
        def buildTree(self, tree, item):
            if isinstance(tree, dict):
                for string, subtree in tree.items():
                    tree_item = self.buildTreeItem(string)
                    item.addChild(tree_item)
                    self.buildTree(subtree, tree_item)
            else:   # expects a string
                item.addChild(self.buildTreeItem(tree))
            
        def buildTreeItem(self, string):
            columns = string.split(" | ")
            tree_item = QTreeWidgetItem()
            for k in range(0, len(columns)):
                column = columns[k]
                if "[ ]" in column or "[*]" in column:
                    self.checkbox[string] = QCheckBox("dur")
                    self.treeWidget.setItemWidget(tree_item, k, self.checkbox[string])
                    if "*" in column:
                        checkbox.setChecked(True)
                else:
                    if "_" in column:
                        column = column[:-1]
                        tree_item.setFlags(tree_item.flags() | Qt.ItemIsEditable)
                    tree_item.setText(k, column)
            return tree_item
    

    https://github.com/enjoysmath
    https://math.stackexchange.com/users/26327/exercisingmathematician

    1 Reply Last reply
    0
    • enjoysmathE Offline
      enjoysmathE Offline
      enjoysmath
      wrote on last edited by
      #2

      Fixed it.

      You have to make sure that the QTreeWidgetItem is actually in the tree first before calling setItemWidget on the tree.

      https://github.com/enjoysmath
      https://math.stackexchange.com/users/26327/exercisingmathematician

      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
      • Unsolved