繁体   English   中英

使用pyside的qtablewidget中的单选按钮

[英]Radio buttons in qtablewidget using pyside

我创建了一个具有四列的QTableWidget。 第一列是一个文本字段,其余三列是单选按钮。 目的是使一行中的所有单选按钮互斥。

代码片段如下:

#Create a table having one row and four columns
searchView = QTableWidget(1,4)
#Input data to create table
dirNames = {'A':['/tmp', '/tmp/dir1'], 'B':['/tmp/dir2'], 'C':['/tmp/dir3']}
#calculate the number of rows needed in table
rowCount = len(dirNames["A"]) + len(dirNames["B"]) + len(dirNames["C"])
searchView.setRowCount(rowCount)
#Set the horizontal header names
searchView.setHorizontalHeaderLabels(["DIR", "A", "B", "C"])
index = 0 
for action in dirNames:
    for paths in dirNames[action]:
        #Create QTableWidgetItem for directory name
        item = QTableWidgetItem(paths)
        searchView.setItem(index, 0, item)
        #Create three radio buttons
        buttonA = QRadioButton()
        buttonB = QRadioButton()
        buttonC = QRadioButton()
        #Check the radio button based on action
        if action == 'A':
            buttonA.setChecked(True)
        elif action == 'B':
            buttonB.setChecked(True)
        else:
            buttonC.setCheched(True)
        #Add radio button to corresponding table item
        searchView.setCellWidget(index, 1, buttonA)
        searchView.setCellWidget(index, 2, buttonB)
        searchView.setCellWidget(index, 3, buttonC)
        #Since setCellWidget transfers the ownership of all radio buttons to qtablewidget Now all radio buttons in table are exclusive
        #So create a buttongroup and add all radio buttons in a row to it so that only radio buttons in a row are exclusive
        buttonGroup = QButtonGroup()
        buttonGroup.addButton(searchView.cellWidget(index, 1)) 
        buttonGroup.addButton(searchView.cellWidget(index, 2)) 
        buttonGroup.addButton(searchView.cellWidget(index, 3)) 
        index += 1

由于某些原因,单选按钮不是排他的。 我们最多可以检查四个单选按钮,它们分布在整个表中。

发生的事情是,当循环被垃圾回收器终止时,通常会破坏在循环中创建的变量,一种避免这种情况的方法是将父QButtonGroup传递给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_())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM