PDA

View Full Version : [PyQt] send and emit an customer class....



cascoin
6th June 2011, 12:53
Hello everybody,

I do have a problem with the signal/slot stuff.....



class Test(QObject):
def __init__(self, parent=None):
QObject.__init__(self, parent)
self.connect(self, SIGNAL('send(int)'), self, SLOT('receiver(int)'))

def send(self, value):
self.emit(SIGNAL('send(int)'), value)

@pyqtSignature('receiver(int)')
def receiver(self, value):
print value


def main():
test = Test()
test.send(12345)


app = QtGui.QApplication(sys.argv)
Start = main()
sys.exit(app.exec_())


Here you can see my very simple code... I have a class called Test.... I want that this class emits a signal and the same class receives this signal.
As the code is written above everything is fine....

Now I want to emit not a integer variable. I want to emit and receive a customer class... I simply changed int everywhere to my classes name and tried to run this code...
Here is how I did it....my customer class and the new code...



from PyQt4.QtCore import *
from PyQt4 import QtGui
import sys

class Student:
def __init__(self, name, Studium, Nr):
self.name = name
self.Fach = Studium
self.Matrikel = Nr



class Test(QObject):
def __init__(self, parent=None):
QObject.__init__(self, parent)
self.connect(self, SIGNAL('send(Student)'), self, SLOT('receiver(Student)'))

def send(self, value):
self.emit(SIGNAL('send(Student)'), value)

@pyqtSignature('receiver(Student)')
def receiver(self, value):
print value


def main():
#Andi = Student("Andi", "MB", 23523)
test = Test()
test.send(Student('Andi', 'MB',12345))


app = QtGui.QApplication(sys.argv)
Start = main()
sys.exit(app.exec_())


The failure message is:
Traceback (most recent call last):
File "C:\Dokumente und Einstellungen\faankesk\Desktop\test\test.py", line 13, in <module>
class Test(QObject):
File "C:\Dokumente und Einstellungen\faankesk\Desktop\test\test.py", line 21, in Test
@pyqtSignature('receiver(Student)')
TypeError: C++ type 'Student' is not supported as a pyqtSlot signature argument type
#---------------------------------------------

Does anybody know why this doesn't work? I don't see the problem....
I hope you understood my problem....
Thank you for helping

Cascoin

Added after 27 minutes:

by the way I use Qt 4.7.1

dbzhang800
6th June 2011, 17:07
Please replace "Student" with "PyQt_PyObject" in signals and slots.
You can refer to: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/old_style_signals_slots.html

You'd better replace this old-fashioned signals and slots usage with the new .
Please refer to: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/new_style_signals_slots.html

cascoin
15th June 2011, 15:11
Hi,
thank you for your answer...
I read the documentation about "New Signal and Slot"-stuff about hundret times...
But I don't understand how to change my example... :(

I just want to replace my signal. I DONT want to send just an "int"-variable...
I want to send a customer-data... Like the student-data-typ in my example...

Can anybody please give me a hint how to start? I tried it very often but it doesn't work...

thanks a lot,

Cascoin

cascoin
17th June 2011, 14:36
please help me if you can!:):):)

Rachol
17th June 2011, 14:51
self.connect(self, SIGNAL('send(PyQt_PyObject)'), self, SLOT('receiver(PyQt_PyObject)'))

walkerning
25th July 2013, 18:25
If I have understood your problem correctly. You could try short-circuit signal to pass your custom python object as your signal paramter. emiting as: self.emit(SIGNAL("send"), value) , and connect as self.connect(self, SIGNAL("send"), self.receiver)