0

单击按钮时,我想从 QTableView 获取活动的 QRadioButton 。但我不知道该怎么做。

假设我有一个 csv 文件my_csv包含

ANIMAL; AGE
DOG; 3
CAT; 5
COW; 5

这是我的示例代码:

import pandas as pd

from PySide2.QtCore import Qt
from PySide2.QtGui import QStandardItemModel, QStandardItem
from PySide2.QtWidgets import QApplication, QTableView, QRadioButton, QPushButton

my_button = QPushButton()
model = QStandardItemModel()
view = QTableView()
view.setModel(model)

df = pd.read_csv(my_csv, sep=';')
header = list(['']) + df.columns
model.setHorizontalHeaderLabels(header)

for index in df_interface.index:
    data = list()
    item = QStandardItem()
    data.append(item)
    items = [QStandardItem("{}".format(field)) for field in df.iloc[index]]
    [element.setTextAlignment(Qt.AlignVCenter | Qt.AlignHCenter) for element in items]
    data.extend(items)
    model.appendRow(data)
    view.setIndexWidget(self.model.index(index, 0), QRadioButton())

4

1 回答 1

1

我假设“活动”是指当前选中的单选按钮(如果有)。如果是这样,解决此问题的一种方法是使用QButtonGroup,如下所示:

# create button group
radio_btns = QButtonGroup()

for index in df_interface.index:
    ...
    # add button to the button group
    button = QRadioButton()
    radio_btns.addButton(button, index)
    view.setIndexWidget(self.model.index(index, 0), button)

完成后,您可以获得选中按钮的索引,如下所示:

index = radio_btns.checkedId()

(但请注意,-1如果当前没有选中按钮,这将返回)。

于 2019-08-19T10:56:18.273 回答