To this question I am referring to the answer from @Andy PyQt Tree Widget, adding check boxes for dynamic removal
There @Andy shows how to add CheckBox
into the QTreeWidget
, which works perfect.
I would like ask here, how to add RadioButton
into QTreeWidget
? ----And, which is more difficult for me, how to make only one item selectable, although they are in different groups
?
I rewrite the code from @Andy into PyQt5:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
def main():
app = QApplication (sys.argv)
tree = QTreeWidget ()
headerItem = QTreeWidgetItem()
item = QTreeWidgetItem()
for i in range(3):
parent = QTreeWidgetItem(tree)
parent.setText(0, "Parent {}".format(i))
parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
for x in range(5):
child = QTreeWidgetItem(parent)
child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
child.setText(0, "Child {}".format(x))
child.setCheckState(0, Qt.Unchecked)
tree.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
The running result of the code above:
UPDATE: The desired result should be like as followings...
Any help will be highly appreciated! Thanks!