使用 pyside 的 qtablewidget 中的单选按钮 [英] Radio buttons in qtablewidget using pyside

查看:58
本文介绍了使用 pyside 的 qtablewidget 中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有四列的 QTableWidget.第一列是文本字段,其余三列是单选按钮.目标是让一行中的所有单选按钮都是独占的.

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 exclusive.

代码片段如下:

#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

出于某种原因,一行中的单选按钮不是独占的.我们最多可以检查四个单选按钮,它们分布在整个表格中.

For some reason, the radio buttons in a row are not exclusive. We could check upto four radio buttons and they are distributed through out the table.

推荐答案

发生的事情是循环中创建的变量经常在循环被垃圾收集器终止时被销毁,避免这种情况的一种方法是传递一个QButtonGroup 的父级.

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_())

这篇关于使用 pyside 的 qtablewidget 中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆