可以完成的任務,如果你暫時停用排他性屬性,你的情況是創建一個保存最後一次按下按鈕,一個變量,如果再次按下它檢查,如果發生這種情況,我們通過取消選中該按鈕並再次激活排他性屬性來停用該屬性。
[...]
self.group.setExclusive(True)
self.btnLastChecked = None
group.buttonClicked.connect(self.onButtonClicked)
def onButtonClicked(self, btn):
if not self.btnLastChecked:
self.btnLastChecked = btn
else:
if self.btnLastChecked == btn:
self.sender().setExclusive(False)
btn.setCheckState(QtCore.Qt.Unchecked)
self.sender().setExclusive(True)
self.btnLastChecked = None
else:
self.btnLastChecked = btn
實施例:
import sys
from PyQt4 import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout=QtGui.QHBoxLayout(self)
self.group=QtGui.QButtonGroup(self)
for i in range(10):
btn = QtGui.QCheckBox(str(i))
self.group.addButton(btn, i)
layout.addWidget(btn)
self.group.setExclusive(True)
self.btnLastChecked = None
self.group.buttonClicked.connect(self.onButtonClicked)
def onButtonClicked(self, btn):
if not self.btnLastChecked:
self.btnLastChecked = btn
else:
if self.btnLastChecked == btn:
self.sender().setExclusive(False)
btn.setCheckState(QtCore.Qt.Unchecked)
self.sender().setExclusive(True)
self.btnLastChecked = None
else:
self.btnLastChecked = btn
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
感謝。是工作! –