0

lots of examples show how to send data from a Worker to the main Application

Im looking for a way to send data into a worker store it there and receave it back

What I want to acomplish

open the mainwindow start a thread with a worker

emit data to the worker

receive the data from the worker to a new window

so far I have emit a signal from the main window

which contains the input from self.input_widget = qtw.QLineEdit()

data = self.input_widget.text()
self.emit_data_signal.emit(data)

and send it to the slot in the worker

@qtc.pyqtSlot()
def workerslot(self,data):
    mainwindow = MainWindow()
    mainwindow.emit_data_signal.connect(lambda: print(data) )

But I get no print output

What Im getting wrong here ?

full code

import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg


class Worker(qtc.QObject):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

    @qtc.pyqtSlot()
    def workerslot(self,data):
        mainwindow = MainWindow()
        mainwindow.emit_data_signal.connect(lambda: print(data) )



class MainWindow(qtw.QWidget):
    emit_data_signal = qtc.pyqtSignal(str)


    def __init__(self):
        super().__init__()
        # your code will go here


        self.send_data_button = qtw.QPushButton("send data")
        layout = qtw.QHBoxLayout()
        self.input_widget = qtw.QLineEdit()
        layout.addWidget(self.send_data_button)
        layout.addWidget(self.input_widget)

        # your code ends here
        self.setLayout(layout)
        self.show()

        # ---------------function-------------------------------------#
        self.send_data_button.clicked.connect(self.senddata)
        # self.
        # ---------------thread---------------------------------------#

        # start thread

        # create objects
        self.worker = Worker()    # 1 - create a  worker and
        self.thread = qtc.QThread()                 # 1 -- thread  instance

        # 2 -- to worker.stop method
        self.worker.moveToThread(self.thread)   # 3Move the Worker object to the Thread object

        self.thread.start() # finally thread starts


    def senddata(self):
        data = self.input_widget.text()
        self.emit_data_signal.emit(data)



if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

0 Answers0