How to Communicate Between Threads and Pyside Qt?

Asked

Viewed 226 times

1

The graphical user interface window created in Pyside is normally executed following the normal program flow. The actual python script of the program runs in a Thread in parallel. But this thread script needs to send the logs to Qplaintextedit from the graphical user interface created in Pyside. But it does not accept receiving threads commands, which causes window crash. I would like to know how to carry out this communication in a simple way?

  • What I meant by "she doesn’t accept receiving threads commands"?

  • The message is classic. I believe that if you had already used Pyside you would certainly be familiar with this message. The dialog only accepts receiving commands on treads started with Qthread. But I don’t want to use Qthread.

  • If only accepted from QThread and you accurate send the message, which makes you want not to use it?

  • Thread sends signal to Qthread and Qtread communicates with the window. But I don’t know anything about these Qt signals.

  • You can use Python’s own mechanism, like this here, but my first option would be to use Qt mechanisms (Signal and Qthread).

1 answer

0


I found out how:

        # Classe de Sinais.
        class Sinais(QtCore.QObject):
            # Elementos.
            elemento1 = QtCore.Signal()
            elemento2 = QtCore.Signal()

            def __init__(self):
                QtCore.QObject.__init__(self)
        sinal = Sinais() # Instância da Classe Sinais.



        def a():
            print('Bom dia')
        def b():
            print('Boa Noite')

        sinal.elemento1.connect(a)
        sinal.elemento2.connect(b)



        def c():
            while True:
                time.sleep(1)
                sinal.elemento1.emit()

        tarefa_c = threading.Thread(target=c)
        tarefa_c.daemon = True
        tarefa_c.start()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.