Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can QTreeWidget have checkboxes at top level if there are no sub-nodes for that parent element?



  • I create an item at the top level with the following code.

    l1 = QTreeWidgetItem(["String A", "String B", "String C","String D"])
    l1.setFlags(l1.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
    l1.setTextAlignment(2, QtCore.Qt.AlignRight)
    

    Everything works fine and the checkbox shows up but only if there are sub-nodes, i.e. child elements. For some top level nodes there are no child elements but I need a checkbox with them. Is that possible? Thanks.


  • Lifetime Qt Champion

    @ACollins Is it same if you remove Qt.ItemIsTristate ?


  • Banned

    @ACollins not sure what you are doing with the rest of your code and/or where you are wanting that check box but this snippet of code works to put a checkbox in the 4th column in the top level of a childless item and was pulled and tweaked from a MUC example I posted here to another question

            treItem_1 = QTreeWidgetItem(["String A", "String B", "String C"])
            treItem_1.setFlags(treItem_1.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
            treItem_1.setCheckState(3, Qt.Unchecked)
    


  • @Denni-0 The key was the third line: treItem_1.setCheckState(3, Qt.Unchecked)

    I put that in and it started working. I thought it would default to the unchecked state but guess not. Thanks for the tip.


Log in to reply