如何以编程方式更改 Table 单元格中单选按钮组的单选按钮?
How change radio button of Radio Button Group in cell of Table programatically?
我想以编程方式更改单元格 table 中单选按钮组中的 selected 单选按钮。
我尝试在更改 piece 对象中的状态后重新创建 table,但如果我有很多记录,它会变慢。
我有下一个代码:
import sys
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QHBoxLayout, QWidget, QButtonGroup, \
QRadioButton, QVBoxLayout, QPushButton, QApplication
class Piece:
def __init__(self, init, name, status):
self.init = init
self.name = name
self.status = status
class Table(QWidget):
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.debug_data()
self.fila = 0
self.init_ui()
def debug_data(self):
print("okay")
self.pieces: [Piece] = []
self.pieces.append(Piece('2021-01-11 13:52:00', 'Router 234', 1))
self.pieces.append(Piece('2021-01-11 13:55:00', 'Router AB', 0))
self.pieces.append(Piece('2021-01-11 14:00:00', 'Router A234E', 1))
self.pieces.append(Piece('2021-01-11 14:01:00', 'Router Sufle', 0,))
def _create_buttons(self):
self.buttons_layout = QHBoxLayout()
self.btn_ok_all = QPushButton("All Manual")
self.btn_no_ok_all = QPushButton("All Automatic")
self.btn_no_validate_all = QPushButton("All Hybrid")
self.buttons_layout.addWidget(self.btn_ok_all)
self.buttons_layout.addWidget(self.btn_no_ok_all)
self.buttons_layout.addWidget(self.btn_no_validate_all)
self.layout.addLayout(self.buttons_layout)
def init_ui(self):
self.createTable()
self.layout = QVBoxLayout()
self._create_buttons()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
self.show()
def createTable(self):
self.tableWidget = QTableWidget()
self.tableWidget.setColumnCount(3)
self.tableWidget.setHorizontalHeaderLabels(
["Init Time", "Name", "Type"])
self.process_rows()
def process_rows(self):
for piece in self.pieces:
self.add_row(piece)
def add_row(self, piece):
self.tableWidget.insertRow(self.fila)
self.tableWidget.setItem(self.fila, 0, QTableWidgetItem(piece.init))
self.tableWidget.setItem(self.fila, 1, QTableWidgetItem(piece.name))
self.tableWidget.setCellWidget(self.fila, 2, self.create_group_radio_button(piece))
self.fila = self.fila + 1
def create_group_radio_button(self, piece):
layout = QHBoxLayout()
widget = QWidget(self)
widget.setLayout(layout)
number_group = QButtonGroup(widget)
manual = QRadioButton("Manual")
number_group.addButton(manual)
automatic = QRadioButton("Automatic")
number_group.addButton(automatic)
hybrid = QRadioButton("Hybrid")
number_group.addButton(hybrid)
layout.addWidget(manual)
layout.addWidget(automatic)
layout.addWidget(hybrid)
if piece.status == 0:
hybrid.setChecked(True)
elif piece.status == 1:
manual.setChecked(True)
else:
automatic.setChecked(True)
return widget
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Table()
sys.exit(app.exec_())
以编程方式更改其中之一的方法是什么?
如何访问单元格小部件和 select 更改它的按钮?
在这种情况下,创建 class 可能是最好的方法:它改进了对象结构并允许创建更容易访问它的函数。
class PieceSelector(QWidget):
def __init__(self, piece):
super().__init__()
self.piece = piece
layout = QHBoxLayout(self)
self.number_group = QButtonGroup(self)
manual = QRadioButton("Manual", 1)
self.number_group.addButton(manual)
automatic = QRadioButton("Automatic", 2)
self.number_group.addButton(automatic)
hybrid = QRadioButton("Hybrid", 0)
self.number_group.addButton(hybrid)
layout.addWidget(manual)
layout.addWidget(automatic)
layout.addWidget(hybrid)
self.number_group.button(piece.status).setChecked(True)
def get_status(self):
return self.number_group.checkedId()
def set_status(self, status):
self.number_group.button(status).setChecked(True)
然后您只需通过创建实例来添加小部件,然后使用 cellWidget()
:
取回它
def add_row(self, piece):
row = self.tableWidget.rowCount()
self.tableWidget.insertRow(row)
self.tableWidget.setItem(row, 0, QTableWidgetItem(piece.init))
self.tableWidget.setItem(row, 1, QTableWidgetItem(piece.name))
self.tableWidget.setCellWidget(row, 2, PieceSelector(piece))
def set_status_for_row(self, row, status):
self.tableWidget.cellWidget(row, 2).set_status(status)
我想以编程方式更改单元格 table 中单选按钮组中的 selected 单选按钮。 我尝试在更改 piece 对象中的状态后重新创建 table,但如果我有很多记录,它会变慢。
我有下一个代码:
import sys
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QHBoxLayout, QWidget, QButtonGroup, \
QRadioButton, QVBoxLayout, QPushButton, QApplication
class Piece:
def __init__(self, init, name, status):
self.init = init
self.name = name
self.status = status
class Table(QWidget):
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.debug_data()
self.fila = 0
self.init_ui()
def debug_data(self):
print("okay")
self.pieces: [Piece] = []
self.pieces.append(Piece('2021-01-11 13:52:00', 'Router 234', 1))
self.pieces.append(Piece('2021-01-11 13:55:00', 'Router AB', 0))
self.pieces.append(Piece('2021-01-11 14:00:00', 'Router A234E', 1))
self.pieces.append(Piece('2021-01-11 14:01:00', 'Router Sufle', 0,))
def _create_buttons(self):
self.buttons_layout = QHBoxLayout()
self.btn_ok_all = QPushButton("All Manual")
self.btn_no_ok_all = QPushButton("All Automatic")
self.btn_no_validate_all = QPushButton("All Hybrid")
self.buttons_layout.addWidget(self.btn_ok_all)
self.buttons_layout.addWidget(self.btn_no_ok_all)
self.buttons_layout.addWidget(self.btn_no_validate_all)
self.layout.addLayout(self.buttons_layout)
def init_ui(self):
self.createTable()
self.layout = QVBoxLayout()
self._create_buttons()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
self.show()
def createTable(self):
self.tableWidget = QTableWidget()
self.tableWidget.setColumnCount(3)
self.tableWidget.setHorizontalHeaderLabels(
["Init Time", "Name", "Type"])
self.process_rows()
def process_rows(self):
for piece in self.pieces:
self.add_row(piece)
def add_row(self, piece):
self.tableWidget.insertRow(self.fila)
self.tableWidget.setItem(self.fila, 0, QTableWidgetItem(piece.init))
self.tableWidget.setItem(self.fila, 1, QTableWidgetItem(piece.name))
self.tableWidget.setCellWidget(self.fila, 2, self.create_group_radio_button(piece))
self.fila = self.fila + 1
def create_group_radio_button(self, piece):
layout = QHBoxLayout()
widget = QWidget(self)
widget.setLayout(layout)
number_group = QButtonGroup(widget)
manual = QRadioButton("Manual")
number_group.addButton(manual)
automatic = QRadioButton("Automatic")
number_group.addButton(automatic)
hybrid = QRadioButton("Hybrid")
number_group.addButton(hybrid)
layout.addWidget(manual)
layout.addWidget(automatic)
layout.addWidget(hybrid)
if piece.status == 0:
hybrid.setChecked(True)
elif piece.status == 1:
manual.setChecked(True)
else:
automatic.setChecked(True)
return widget
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Table()
sys.exit(app.exec_())
以编程方式更改其中之一的方法是什么? 如何访问单元格小部件和 select 更改它的按钮?
在这种情况下,创建 class 可能是最好的方法:它改进了对象结构并允许创建更容易访问它的函数。
class PieceSelector(QWidget):
def __init__(self, piece):
super().__init__()
self.piece = piece
layout = QHBoxLayout(self)
self.number_group = QButtonGroup(self)
manual = QRadioButton("Manual", 1)
self.number_group.addButton(manual)
automatic = QRadioButton("Automatic", 2)
self.number_group.addButton(automatic)
hybrid = QRadioButton("Hybrid", 0)
self.number_group.addButton(hybrid)
layout.addWidget(manual)
layout.addWidget(automatic)
layout.addWidget(hybrid)
self.number_group.button(piece.status).setChecked(True)
def get_status(self):
return self.number_group.checkedId()
def set_status(self, status):
self.number_group.button(status).setChecked(True)
然后您只需通过创建实例来添加小部件,然后使用 cellWidget()
:
def add_row(self, piece):
row = self.tableWidget.rowCount()
self.tableWidget.insertRow(row)
self.tableWidget.setItem(row, 0, QTableWidgetItem(piece.init))
self.tableWidget.setItem(row, 1, QTableWidgetItem(piece.name))
self.tableWidget.setCellWidget(row, 2, PieceSelector(piece))
def set_status_for_row(self, row, status):
self.tableWidget.cellWidget(row, 2).set_status(status)