I am new to PySide and I'm trying to emit a signal and receive it from another class.
I have used emit on the object of the MySignal class and signal is being emitted from the MyRadioButton class. Both emit and connect are returning True but as I want, the update method is not being called
This is the basic structure of files and code.
# MySignals.py
from PySide.QtCore import QObject, Signal
class MyCheckedSignal(QObject):
signal = Signal(str)
# MyRadioButton.py
class MyRadioButton(QWidget, QObject):
def __init__(self, value=None, label=None):
QWidget.__init__(self)
self.__value = value
self.__checked = False
self.checkSignal = MySignals.MyCheckedSignal()
def toggleCheck(self):
if self.__checked == False:
self.__checked = True
self.checkSignal.emit(SIGNAL(self.__value))
else:
self.__checked = False
# MyRadioGroup.py
class MyRadioGroup(QObject):
def __init__(self, radioes=None):
QObject.__init__(self)
for radio in radioes:
# radio is of type MyRadio
# self.connect(radio, SIGNAL("checked()"), self.update)
radio.checkSignal.signal.connect(self.update)
# self.connect(self.update)
def update(self, value):
print("Checked", value)
Foreword: Your choice of base class seems strange: your MyRadioButton
should probably inherit from QRadioButton and your MyRadioGroup
would benefit inheriting from QGroupBox , which is a proper widget, and will therefore integrate well with the rest of the application. Look at the description inside for a concrete example with layouts.
To your specific question, and without using the existing QRadioButton
, you don't need to create your special class for the signal. Simply declare your MyRadioButton
as follow:
class MyRadioButton(QPushButton):
checkSignal = QtCore.Signal(str)
def __init__(self, value=None, label=None):
QPushButton.__init__(self)
self.__value = value
self.__checked = False
self.setCheckable(True) # This will 'hold' the button once clicked
self.clicked.connect(self.toggleCheck) # Signal emitted on click
def toggleCheck(self):
if self.__checked == False:
self.__checked = True
self.checkSignal.emit(self.__value)
else:
self.__checked = False
Notice that for your toggleCheck
method to be activated, you have to connect the signal from QAbstractButton
: clicked
, to the method. Your RadioGroup then becomes simply (inheriting from QWidget is a good idea if you want to display it in some way):
class MyRadioGroup(QWidget):
def __init__(self, radioes=None):
QWidget.__init__(self)
self.radioes = radioes # good idea to store a reference to it
for radio in radioes:
radio.checkSignal.connect(self.update)
def update(self, value):
print("Checked", value)
I will paste below the entire file for you to test. On my setup ( Python 2.7.8
, PySide 1.2.2
), this work fine.
As an aside: I am confused by your line self.checkSignal.emit(SIGNAL(self.__value))
. What is this SIGNAL
? Maybe I missed something from your question.
full example file:
import sys
from PySide import QtGui
from PySide import QtCore
class MyRadioButton(QtGui.QPushButton):
checkSignal = QtCore.Signal(str)
def __init__(self, text, value=None, label=None):
QtGui.QPushButton.__init__(self, text)
self.__value = value
self.__checked = False
self.setCheckable(True)
self.clicked.connect(self.toggleCheck)
def toggleCheck(self):
if self.__checked == False:
self.__checked = True
self.checkSignal.emit(self.__value)
else:
self.__checked = False
class MyRadioGroup(QtGui.QWidget):
def __init__(self, radios=None):
QtGui.QWidget.__init__(self)
self.radios = radios
for radio in self.radios:
radio.checkSignal.connect(self.update)
def update(self, value):
print("Checked", value)
app = QtGui.QApplication(sys.argv)
radio1 = MyRadioButton('Hello toto', value='toto')
radio2 = MyRadioButton('Hello titi', value='titi')
group = MyRadioGroup(radios=[radio1, radio2])
vbox = QtGui.QVBoxLayout()
vbox.addWidget(radio1)
vbox.addWidget(radio2)
vbox.addStretch(1)
group.setLayout(vbox)
group.resize(250, 150)
group.setWindowTitle('Signals')
group.show()
sys.exit(app.exec_())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.