Как установить взаимоисключающий QRadioButton в QTableWidget для каждой строки - PullRequest
0 голосов
/ 07 марта 2019

Если я добавлю радиокнопку таким образом, все радиокнопки будут взаимоисключающими, поэтому есть идеи, как установить для каждой строки?

...
for y in range(2):
    for x in range(3):
        checkbox = QRadioButton()
        table.setCellWidget(y, x, checkbox)
...

enter image description here

1 Ответ

1 голос
/ 07 марта 2019

Класс 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())

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...