pyside6qtreewidget

How can I disable only checkbox columns in pyside6 QTreeWidget?


import sys,random 
from PyQt5.QtWidgets import QApplication,QTreeWidgetItem,QTreeWidget,QHeaderView,QWidget,QLineEdit 
from PyQt5.QtCore import Qt 
from PyQt5.QtGui import QFont 

class TreeWidget(QWidget): 
    def __init__(self): 
        
        super(). __init__() 
        self.setWindowTitle("List Box Samples") 
        self.TreeWidget = QTreeWidget(self) 
        self.TreeWidget.resize(600,700) 
        ch_font = QFont() 
        ch_font.setBold(True) 
        ch_font.setPointSize(12) 
        
        self.TreeWidget.setColumnCount(2) 
        self.TreeWidget.setHeaderLabels(['  Test1  ','  Test2  ']) 
        self.TreeWidget.setAlternatingRowColors(True) 
        
        self.TreeWidget.header().setDefaultAlignment(Qt.AlignCenter) 
        self.TreeWidget.header().setFont(ch_font) 
        self.TreeWidget.header().setSectionResizeMode(0, QHeaderView.ResizeToContents) 
        self.TreeWidget.header().setSectionResizeMode(1, QHeaderView.ResizeToContents) 
        
        self.TreeWidget.setFont(ch_font) 
        a = random.randint(1,2) 
        for i in range(5): 
            item = QTreeWidgetItem(self.TreeWidget) 
            item.setText(0,"Test root") 
            item.setText(1,"Test str") 
            for i in range(10): 
                child_item = QTreeWidgetItem(item) 
                child_item.setCheckState(0,Qt.Checked) 
                child_item.setText(0,str(i)) 
                child_item.setText(1,"Test"+str(i)) 
        
if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    win = TreeWidget() 
    win.show() 
    sys.exit(app.exec_()) 

I want to disable only the checkstate column. child_item = QTreeWidgetItem() has no function of setDisabled itself. Also, child_item.setText(0,Qt.Unchecked) does not have a disabled function. I appreciate it

child_item.setDisabled(True)

The entire row disabled


Solution

  • Qt provides higher level item view classes to their basic model/view pairs. Those classes are intended for basic usage and interaction (both for the user and on the programming side).

    QTreeWidget items automatically sets a Qt.ItemIsUserCheckable flag on a whole row whenever setCheckState is explicitly called on an item.

    Unfortunately, this has a drawback: it automatically makes a checked item interactive for the check box, so the user can toggle it.

    Assuming that you only want to show a checked item for a single column, the solution is quite simple: unset the Qt.ItemIsUserCheckable flag for the item; this won't prevent the view to show the check box, but it will prevent the user to change its value:

    # ...
    for i in range(10): 
        child_item = QTreeWidgetItem(item) 
        child_item.setCheckState(0,Qt.Checked) 
        child_item.setFlags(child_item.flags() & ~Qt.ItemIsUserCheckable)
        # ...
    

    I strongly suggest you to carefully read (and patiently study and experiment with) the whole documentation about the Qt model/view programming in order to better understand how the whole system works.