Python PyQt[QTreeWidget]:如何为项目添加Radiobutton?
关于这个问题,我指的是@Andy的回答 在这里@Andy展示了如何将Python PyQt[QTreeWidget]:如何为项目添加Radiobutton?,python,pyqt,pyqt5,qtreewidget,qradiobutton,Python,Pyqt,Pyqt5,Qtreewidget,Qradiobutton,关于这个问题,我指的是@Andy的回答 在这里@Andy展示了如何将复选框添加到QTreeWidget中,这非常好用 我想在这里问一下,如何将RadioButton添加到QTreeWidget----而且,对我来说更困难的是,如何只选择一个项目,尽管它们属于不同的组 我将@Andy的代码重写为PyQt5: from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import sys d
复选框添加到QTreeWidget
中,这非常好用
我想在这里问一下,如何将RadioButton
添加到QTreeWidget
----而且,对我来说更困难的是,如何只选择一个项目,尽管它们属于不同的组
我将@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()
以上代码的运行结果:
更新:预期结果如下所示。。。
任何帮助都将不胜感激!谢谢 如果您想建立一个QRadioButton
,并且它在一个组中是独占的,一个可能的解决方案是实现一个委托,在此委托中,我们将覆盖绘制QRadioButton
的paint
方法和捕获单击事件并根据具体情况更改其他项目状态的editorEvent
方法
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Delegate(QStyledItemDelegate):
def paint(self, painter, option, index):
if not index.parent().isValid():
QStyledItemDelegate.paint(self, painter, option, index)
else:
widget = option.widget
style = widget.style() if widget else QApplication.style()
opt = QStyleOptionButton()
opt.rect = option.rect
opt.text = index.data()
opt.state |= QStyle.State_On if index.data(Qt.CheckStateRole) else QStyle.State_Off
style.drawControl(QStyle.CE_RadioButton, opt, painter, widget)
def editorEvent(self, event, model, option, index):
value = QStyledItemDelegate.editorEvent(self, event, model, option, index)
if value:
if event.type() == QEvent.MouseButtonRelease:
if index.data(Qt.CheckStateRole) == Qt.Checked:
parent = index.parent()
for i in range(model.rowCount(parent)):
if i != index.row():
ix = parent.child(i, 0)
model.setData(ix, Qt.Unchecked, Qt.CheckStateRole)
return value
def main():
app = QApplication(sys.argv)
tree = QTreeWidget()
tree.setItemDelegate(Delegate())
for i in range(3):
parent = QTreeWidgetItem(tree)
parent.setText(0, "Parent {}".format(i))
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.expandAll()
tree.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
您可以解释它的含义:如何只选择一个项目,尽管它们在不同的组中?什么是一个组?另一个组中的项目是什么意思?您的意思是说每个组都与parent0、parent1等关联,并且相同的项目都是具有相同名称的项目吗?您是否希望家长也有QRadioButton?所有家长都将有相同的孩子?@eyllanesc感谢您的回答。我在上面添加了一张图片来解释我的问题。请看一看。组的名称并不重要。总之,我的问题是:1如何添加Radiobutton,2如何使Radiobutton“独一无二”。非常感谢!它还有助于添加对取消选中状态的检查,以便在选中同一项时,单选按钮不会消失。`if index.data(QtCore.Qt.CheckStateRole)==QtCore.Qt.Unchecked:parent=index.parent()表示范围内的i(model.rowCount(parent)):if i==index.row():ix=parent.child(i,0)model.setData(ix,QtCore.Qt.Checked,QtCore.Qt.CheckStateRole)`