I want to get the active QRadioButton from the QTableView when clicking a button. But I don't know how to do it.
Let's say I have a csv file my_csv that contains
ANIMAL; AGE
DOG; 3
CAT; 5
COW; 5
This is my sample code:
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())
I assume by "active", you mean the currently checked radio-button (if any). If so, one way to solve this is to use a QButtonGroup, like this:
# 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)
With that done, you can get the index of the checked button like this:
index = radio_btns.checkedId()
(But note that this will return -1
if there is currently no checked button).