Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adding QThread with button dynamically as Table entries changes



  • I am working on a test project where I am adding entries/rows in qWidgetTable on the right side and the number of buttons with player name changes accordingly.

    Every button should be bonded to a QThread worker. The worker is just grabbing the coin price from the futbin.com website (that can be any but for this testing, I went with this one)

    But as I clicked on any log-in button it creates a QThread and runs the worker, but if we click on another button it doesn't do anything but wait for the previous thread to complete.

    I am stuck at this point.

    alt text

    class MarketWorker(QObject):
        finished = pyqtSignal()
    
        @pyqtSlot()
        def _check_price(self):
            check_price('angel correa', player_type= 'rare') # angel correa if any button clicked
            self.finished.emit()
    
    class MainApp(QtWidgets.QMainWindow):
        login_requested = pyqtSignal()
    
        def __init__(self, parent=None):
            super(MainApp, self).__init__(parent=parent)
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
    
            # creating buttons
            for num in range(self.ui.tableWidget.rowCount()):
                _margin = 32 * num + 10
                self.btn_login = QtWidgets.QPushButton(self.ui.centralwidget)
                self.btn_login.setGeometry(QtCore.QRect(50, 70 + _margin , 261, 31))
                self.btn_login.setObjectName(f"btn_login_{num}")
                _name = self.ui.tableWidget.item(num, 0).text()
                self.btn_login.setText(_name.upper())
    
                self.btn_login.clicked.connect(self.get_info)
    
            # creating worker threads
            self.worker = MarketWorker()
            self.thread = QThread() # creating thread
            self.worker.moveToThread(self.thread) # binding worker with thread
            self.login_requested.connect(self.worker._check_price) # check for pricing on futbin
            self.login_requested.connect(self.thread.start) # starts the thread
            self.worker.finished.connect(lambda: print("done")) # when finished
            self.worker.finished.connect(self.thread.quit) 
            self.worker.finished.connect(self.worker.deleteLater)
            self.worker.finished.connect(self.thread.deleteLater)
    
    
        def get_info(self):
            self.login_requested.emit()
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        w = MainApp()
        w.show()
        sys.exit(app.exec_())
    
    


  • @haseeb said in Adding QThread with button dynamically as Table entries changes:

        self.login_requested.connect(self.worker._check_price) # check for pricing on futbin
        self.login_requested.connect(self.thread.start) # starts the thread
    

    I am not an expert in threads --- and also I don't know what your _check_price() check_price() does --- but this will run self.worker._check_price() before self.thread.start(). Is that a problem?



  • @JonB Thank you for writing.

    Nah that's not the problem. You are just connecting the functions together asynchronously, so the order doesn't matter in threads



  • @haseeb
    These are slots attached to the same object-signal. They will be executed (when the signal is emitted) in the order in which you did the connect()s. I thought you want to start the thread first and then get it to execute self.worker._check_price(), but maybe I misunderstand. If you say it's OK to first call self.worker._check_price() and then call self.thread.start() that's fine, I had the impression you need to start the thread before asking the worker to check the price, but I defer to your knowledge of the code.


Log in to reply