I have a QTreeWidget and each QTreeWidgetItem has 3 checkboxes.I would like to do two things but I can't figure how to do them.
- The user should be able to select only one check box at time for each QTreeWidgetItem.So the selection of a given checkbox of a QTreeWidgetItem should deselect the other checkboxes of that QTreeWidgetItem.
- The selection/deselection of a given parent QTreeWidgetItem checkbox should select/deselect all its child checkboxes in the same column. Below is my code:
Any help would be highly appreciated
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.Qt import Qt
import sys
def main():
app = QtWidgets.QApplication(sys.argv)
tree = QtWidgets.QTreeWidget()
tree.resize(500, 200)
headerItem = QtWidgets.QTreeWidgetItem()
item = QtWidgets.QTreeWidgetItem()
tree .setColumnCount(4)
tree .setHeaderLabels(["pluto", "X", "Y", "Z", ""])
for i in range(3):
parent = QtWidgets.QTreeWidgetItem(tree)
parent.setText(0, "Parent {}".format(i))
parent.setCheckState(1, Qt.Unchecked)
parent.setCheckState(2, Qt.Unchecked)
parent.setCheckState(3, Qt.Unchecked)
#parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
for x in range(5):
child = QtWidgets.QTreeWidgetItem(parent)
child.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
child.setText(0, "Child {}".format(x))
child.setCheckState(1, Qt.Unchecked)
child.setCheckState(2, Qt.Unchecked)
child.setCheckState(3, Qt.Unchecked)
tree.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I have solved my own problem by using itemClicked.Here is my solution incase someone is interested.