如果我以此方式添加单选按钮,则所有单选按钮都是互斥的 所以知道如何设置每行吗?
...
for y in range(2):
for x in range(3):
checkbox = QRadioButton()
table.setCellWidget(y, x, checkbox)
...
答案 0 :(得分:1)
QButtonGroup类提供了一个容器,用于组织按钮小部件的组。
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class TableExample(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.table = QTableWidget()
self.table.setRowCount(3)
self.table.setColumnCount(3)
for y in range(3):
button_group = QButtonGroup(self) # <---
button_group.setExclusive(True) # <---
for x in range(3):
checkbox = QRadioButton()
button_group.addButton(checkbox) # <---
self.table.setCellWidget(y, x, checkbox)
layout = QVBoxLayout(self)
layout.addWidget(self.table)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = TableExample()
w.setWindowTitle('qradiobutton-in-qtablewidget')
w.setWindowIcon(QIcon('im.png'))
w.resize(400, 150)
w.show()
sys.exit(app.exec())