Skip to content Skip to sidebar Skip to footer

Radio Buttons In Qtablewidget Using Pyside

I have created a QTableWidget having four columns. The first column is a text field and the remaining three are radio buttons. Objective is to make all radio buttons in a row exclu

Solution 1:

What is happening is that the variables created within a loop are often destroyed when the loop is terminated by the garbage collector, one way to avoid this is to pass a parent to QButtonGroup.

import sys

from PySide.QtGui import QApplication, QTableWidget, QTableWidgetItem, \
    QButtonGroup, QRadioButton

app = QApplication(sys.argv)

searchView = QTableWidget(0, 4)
colsNames = ['A', 'B', 'C']
searchView.setHorizontalHeaderLabels(['DIR'] + colsNames)
dirNames = {'A': ['/tmp', '/tmp/dir1'], 'B': ['/tmp/dir2'],
            'C': ['/tmp/dir3']}

rowCount = sum(len(v) for (name, v) in dirNames.items())
searchView.setRowCount(rowCount)

index = 0

for letter, paths in dirNames.items():
    for path in paths:
        it = QTableWidgetItem(path)
        searchView.setItem(index, 0, it)
        group = QButtonGroup(searchView)
        for i, name in enumerate(colsNames):
            button = QRadioButton()
            group.addButton(button)
            searchView.setCellWidget(index, i + 1, button)
            if name == letter:
                button.setChecked(True)
        index += 1

searchView.show()
sys.exit(app.exec_())

Post a Comment for "Radio Buttons In Qtablewidget Using Pyside"