PyQt的QButtonGroup示例

QButtonGroup是从QObject继承的,不是可视类,它主要是用于方便密钥管理的一组密钥。

默认情况下,QRadioButton与同一父类的所有单选框互斥,可以通过QButtonGroup对其进行分类。

 

QButtonGroup

下面的示例创建两个按钮组(QButtonGroup)。它将单选按钮添加到每个组(QRadioButton)。

from PyQt5.Qt import *
import sys    

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGrounp")
w.resize(300, 150)

cs1 = QRadioButton("Beginner",w)
cs1.move(130, 20)

cs2 = QRadioButton("Senior",w)
cs2.move(130, 40)

cs3 = QRadioButton("Expert",w)
cs3.move(130, 60)

cs4 = QRadioButton("Best",w)
cs4.move(130, 80)

cs_group = QButtonGroup(w)
cs_group.addButton(cs1)
cs_group.addButton(cs2)
cs_group.addButton(cs3)
cs_group.addButton(cs4)

drs1 = QRadioButton("Python",w)
drs1.move(20, 20)

drs2 = QRadioButton("Golang",w)
drs2.move(20, 40)

drs3 = QRadioButton("Java",w)
drs3.move(20, 60)

drs_group = QButtonGroup(w)
drs_group.addButton(drs1)
drs_group.addButton(drs2)
drs_group.addButton(drs3)

w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())

pyqt qbuttongroup示例

可以看出,按钮被分为两组。组是互斥的,并且一个组中的差异不会影响另一组。

QButtonGroup ID

QButtonGroup可以在添加按钮时快速设置按钮的ID(不指定将自动分配负值的ID),也可以通过button()和button()查看所有按钮以查看指定的按钮。

from PyQt5.Qt import *
import sys

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGrounp")
w.resize(300, 150)

cs1 = QRadioButton("Answer A",w)
cs1.move(20, 20)
cs1.setChecked(True)

cs2 = QRadioButton("Answer B",w)
cs2.move(20, 40)

cs_group = QButtonGroup(w)
cs_group.addButton(cs1, 1)      # ID 1
cs_group.addButton(cs2, 2)      # ID 2

print(cs_group.buttons())
print(cs_group.button(2))       # ID=2
print(cs_group.checkedButton()

w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())

qbuttongroup pyqt setid python

QButtonGroup setid

默认情况下,同一按钮组的单选框是互斥的,即,一次只能选择一个按钮。

在QRadioButton中,可以将单个按钮设置为不与其他按钮互斥,QButtonGroup具有类似的功能setExclusive()。

python pyqt qbuttongroup

以下程序说明了setExclusive()的用法。

from PyQt5.Qt import *
import sys

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGroup")
w.resize (300, 150)

#Create group 1 QRadioButton button
cs1 = QRadioButton ("Super Cup", w)
cs1.move(80, 20)

cs2 = QRadioButton ("Big Cup", w)
cs2.move(80, 40)

CS3 = QRadioButton ("Medium Cup", w)
cs3.move(80, 60)

cs4 = QRadioButton ("small cup", w)
cs4.move(80, 80)

#Create a key group and add keys
cs_group = QButtonGroup(w)
cs_group.addButton(cs1)
cs_group.addButton(cs2)
cs_group.addButton(cs3)
cs_group.addButton(cs4)

# Set cs_group to not be mutually exclusive
cs_group.setExclusive(False)

w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())

移除按钮

这里的remove按钮意味着将按钮移出QButtonGroup,而不是移除此控件,这是通过removeButton()完​​成的。

from PyQt5.Qt import *
import sys

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGroup")
w.resize (300, 150)

#Create group 1 QRadioButton button
cs1 = QRadioButton("1st",w)
cs1.move(20, 20)

#Setting the first radio box to be checked
cs1.setChecked(True)
cs2 = QRadioButton("2nd",w)
cs2.move(20, 40)

#Create a key group and add keys
cs_group = QButtonGroup(w)
cs_group.addButton(cs1, 1)
cs_group.addButton(cs2, 2)

#move cs2 key out of sc_group
cs_group.removeButton(cs2)

w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())

讯号

在buttonClicked的情况下,指定的信号为id。

from PyQt5.Qt import *

import sys

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGrounp")
w.resize (300, 150)

# Create 4 QRadioButton buttons
cs1 = QRadioButton ("Super Cup", w)
cs1.move(80, 20)

cs2 = QRadioButton ("Big Cup", w)
cs2.move(80, 40)

cs3 = QRadioButton ("Medium Cup", w)
cs3.move(80, 60)

cs4 = QRadioButton ("small cup", w)
cs4.move(80, 80)

# Create a key group and add keys
cs_group = QButtonGroup(w)
cs_group.addButton(cs1,1)
cs_group.addButton(cs2,2)
cs_group.addButton(cs3,3)
cs_group.addButton(cs4,4)

# Define the slot function and print the received parameters
def slot(object):
    print("Key was pressed, id is:", cs_group.id(object))

# connects the slot function and makes the argument of the band int type
cs_group.buttonClicked.connect(slot)
w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())

pyqt qbuttongroup ID

标签

发表评论