Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidgetItem.setItemWidget(item, k, QCheckBox()) not working (checkbox doesn't show up)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 1.8k 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.
  • E Offline
    E Offline
    enjoysmath
    wrote on 11 Mar 2018, 20:44 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
    • E Offline
      E Offline
      enjoysmath
      wrote on 11 Mar 2018, 20:51 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

      1/2

      11 Mar 2018, 20:44

      • Login

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