E
E
eellazy2020-03-21 20:10:01
Python
eellazy, 2020-03-21 20:10:01

How to transmit a signal from a third-party module?

Hello. There is a program that is knocked down into one file. Now I want to scatter this whole thing into modules.
How do I receive a signal in pyqt from another module?

Right now I'm signaling like this:

self.parserBazos.newTextSignalBazos.connect(self.addNewItemBazos)


Main file
config_dir = 'modules/sk_sk/bazos/config/config.ini'
open_file_number_dir = 'modules/sk_sk/bazos/database/database_number.txt'
open_file_csv_dir = 'modules/sk_sk/bazos/database/database.csv'

class ParserBazos(QtCore.QObject):
    running = False
    newTextSignalBazos = QtCore.pyqtSignal(str)
    def run(self):     
        bazos.main(config_dir, open_file_number_dir, open_file_csv_dir)

class MyWindow(QtWidgets.QMainWindow):

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

        self.ui.parserButton_1.clicked.connect(self.threadBazos)  

    def threadBazos(self):
        self.thread_1 = QtCore.QThread()
        self.parserBazos = ParserBazos()
        self.parserBazos.moveToThread(self.thread_1)
        self.parserBazos.newTextSignalBazos.connect(self.addNewItemBazos)
        self.thread_1.started.connect(self.parserBazos.run)
        self.thread_1.start()

    @QtCore.pyqtSlot(str)
    def addNewItemBazos(self, string):
        self.ui.parserList_1.addItem(string)


And here is another file from which, instead of print, you need to send a signal and display it in the program window
def print_item(data):
    data_item = data['phone'], data['country_code'], data['name'], data['title'], data['location'], data['link_page'], data['login'], data['link_login']
print(data_item)
    return data_item

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2020-03-21
@eellazy

ParserBazos to a separate file, import it, then everything is the same

#####
from <file_with_ParserBazos> import ParserBazos 
#####
config_dir = 'modules/sk_sk/bazos/config/config.ini'
open_file_number_dir = 'modules/sk_sk/bazos/database/database_number.txt'
open_file_csv_dir = 'modules/sk_sk/bazos/database/database.csv'

class MyWindow(QtWidgets.QMainWindow):

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

        self.ui.parserButton_1.clicked.connect(self.threadBazos)  

    def threadBazos(self):
        self.thread_1 = QtCore.QThread()
        self.parserBazos = ParserBazos()
        self.parserBazos.moveToThread(self.thread_1)
        self.parserBazos.newTextSignalBazos.connect(self.addNewItemBazos)
        self.thread_1.started.connect(self.parserBazos.run)
        self.thread_1.start()

    @QtCore.pyqtSlot(str)
    def addNewItemBazos(self, string):
        self.ui.parserList_1.addItem(string)

PS Get in the habit of defining all class-level variables in __init__
def __init__(self, parent=None):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.parserBazos = ParserBazos() # <-
        self.thread_1 = QtCore.QThread() # <-

        self.ui.parserButton_1.clicked.connect(self.threadBazos)

while in threadBazos(self): ParserBazos instantiation and QtCore.QThread removed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question