Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Adding QThread with button dynamically as Table entries changes
Forum Updated to NodeBB v4.3 + New Features

Adding QThread with button dynamically as Table entries changes

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for python
4 Posts 2 Posters 511 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haseeb
    wrote on 13 Oct 2021, 06:29 last edited by
    #1

    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_())
    
    
    J 1 Reply Last reply 13 Oct 2021, 07:41
    0
    • H haseeb
      13 Oct 2021, 06:29

      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_())
      
      
      J Online
      J Online
      JonB
      wrote on 13 Oct 2021, 07:41 last edited by JonB
      #2

      @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?

      H 1 Reply Last reply 13 Oct 2021, 08:11
      0
      • J JonB
        13 Oct 2021, 07:41

        @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?

        H Offline
        H Offline
        haseeb
        wrote on 13 Oct 2021, 08:11 last edited by
        #3

        @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

        J 1 Reply Last reply 13 Oct 2021, 08:14
        0
        • H haseeb
          13 Oct 2021, 08:11

          @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

          J Online
          J Online
          JonB
          wrote on 13 Oct 2021, 08:14 last edited by JonB
          #4

          @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.

          1 Reply Last reply
          1

          1/4

          13 Oct 2021, 06:29

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved