在 QVBoxLayout 中查找选中的 QRadioButton [英] Finding checked QRadioButton among many into a QVBoxLayout

查看:75
本文介绍了在 QVBoxLayout 中查找选中的 QRadioButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码动态创建了一组单选按钮:

I used the code below to dynamically create a group of radio buttons:

self.wPaymantType.qgbSomeSelectionGroup = QtGui.QGroupBox()
vbox = QtGui.QVBoxLayout()

for row in listOfChoices:
    radio = QtGui.QRadioButton(row)
    if bIsFirst:
        radio.setChecked(True)
        bIsFirst = False
    if len(row.name) > nMaxLen:
        nMaxLen = len(row.name)

    vbox.addWidget(radio)

self.wPaymantType.qgbSomeSelectionGroup.setLayout(vbox)

如何遍历所有单选按钮以找出选中的单选按钮?

How can I iterate through all radio buttons to find out which one is checked?

我尝试过这样的事情,但我没有从中得到任何好处:

I tried something like this, but I didn't get anything good from it:

qvbl = self.qgbSomeSelectionGroup.children()[0]

for i in range(0, qvbl.count()):
    child = qvbl.itemAt(i)
    radio = QtGui.QRadioButton(child.widget())
    if radio != None:
        if radio.isChecked():
            print "radio button num " + str(i) + " is checked"

推荐答案

我认为它不起作用的原因是你的

I believe the reason why it's not working is your

 radio = QtGui.QRadioButton(child.widget())

调用您检查复选框是否被选中的代码.我认为您要做的是将子对象类型转换为 QtGui.QRadioButton 并且在这种情况下不起作用.相反,您应该创建一个新的小部件.尝试将其更改为 smth.像这样:

call at the code where you're checking if your checkbox is checked. I think what you're trying to do is typecast the child object to QtGui.QRadioButton and it doesn't work in this case. Instead you should be creating a new widget. Try changing it to smth. like this:

    qvbl = self.qgbSomeSelectionGroup.layout()
    for i in range(0, qvbl.count()):
        widget = qvbl.itemAt(i).widget() 
        if (widget!=0) and (type(widget) is QtGui.QRadioButton):
            if widget.isChecked():
                print "radio button num " + str(i) + " is checked"

上面的代码应该遍历布局对象的子对象,检查它们的类型并打印radio button..."以防它是单选按钮并且它被选中

the code above should be iterating through child objects of the layout object, check their type and print "radio button..." in case it's radio buttong and it's checked

希望对您有所帮助,问候

hope this helps, regards

这篇关于在 QVBoxLayout 中查找选中的 QRadioButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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