对于这个问题,我指的是@Andy PyQt Tree Widget, adding check boxes for dynamic removal
的回答 @Andy展示了如何将CheckBox
添加到QTreeWidget
中,这非常有效。
我想问一下,如何将RadioButton
添加到QTreeWidget
? ----而且,对我来说,如果只选择一个项目,虽然它们位于不同的groups
,但这对我来说更难?
我将@Andy中的代码重写为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()
上面代码的运行结果:
任何帮助将受到高度赞赏!谢谢!