Polo D. Vargas
Polo D. Vargas

Reputation: 1669

Error sending signals from thread

Hello need some help I am trying to show a Window pop-up when I receive a certain type of information sent by the server. The problem is that the method of receiving the data from the server is a Thread so I have problems modifying the MainWindow from a thread, that's why I tried to send a signal from the thread to another method in the class User (see my code below), so i don't modify the window from a thread. I get the following error:

TypeError: User cannot be converted to PyQt4.QtCore.QObject in this context

I don't know if this is the right way of solving the problem, so if there is a better way i would appreciate your help, here goes the code:

class User:
    signal1 = QtCore.pyqtSignal()

    def __init__(self, usuario):
        self.usuario = usuario
        self.host = '127.0.0.1'
        self.port = 3490
        self.user = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Socket cliente
        try:                
            self.thread_wait_server = threading.Thread(target=self.wait_server)
            self.thread_wait_server.start()               

        except socket.error:
            print("No fue posible realizar la conexión")
            sys.exit()
        self.initUI()

    def initUI(self):
        app = QtGui.QApplication(sys.argv)
        self.login = interfaz.Loggin()
        self.login.show()
        self.signal1.connect(self.showMain)
        self.login.crear.trigger.connect(self.signal)
        self.login.trigger1.connect(self.signal2)
        app.exec_()

    def signal(self):
        info = self.login.crear.userPassword()
        info1 = pickle.dumps(info)
        self.user.sendall(info1)

    def signal2(self):
        info2 = self.login.comprobar()
        info3 = pickle.dumps(info2)
        self.user.sendall(info3)
        print('sendall')

    def wait_server(self):
        self.conectar()

    def conectar(self):
        self.user.connect((self.host, self.port))
        self.listener = threading.Thread(target=self.listen, args=())
        self.listener.Daemon = True
        self.listener.start()

    def listen(self):
        while True:

            data = self.user.recv(1024)
            print('received')
            print(data)
            if data:
                info = pickle.loads(data)
                if info == 'usuario_valido':
                    self.signal1.emit()
                    print('mainshow')
                    #self.login.exit()

    def showMain(self):
        self.login.main.show()

don't mind the functions signal they are just sending things to the server the problem is with the method listen when the server sends back the information my user should receive and do something in this case open a window. ( My widgets are Loggin() and the one i want to show is set as an attribute of the class Loggin() and its called main.

Thanks

Upvotes: 1

Views: 1323

Answers (1)

three_pineapples
three_pineapples

Reputation: 11849

The class User must inherit from QObject if you want it to contain a signal. So the first line of the class definition should read class User(QtCore.QObject):.

Upvotes: 4

Related Questions