Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PyQT, how to use QThread to stop application from proceeding?
Forum Updated to NodeBB v4.3 + New Features

PyQT, how to use QThread to stop application from proceeding?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 3.0k 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.
  • F Offline
    F Offline
    freemanl144
    wrote on last edited by
    #1

    I have an application that just basically runs through a set of functions. Within those functions there is a popup window that allows the user to cancel out. I want this to stop everything that is happening. I was trying to use QThreading to do this. I start the thread with a button click using

    self.thread = QThread()
    self.ui.pb_start.clicked.connect(lambda: self.thread.started.connect(self.start_button_click()))
    

    The function start_button_click is just a function that makes other function calls. Something along these lines

    def start_button_click(self):
            self.get_input_vals_from_gui()
            self.write_to_log_box("Starting")
            self.get_ranges_for_inputs()
            self.get_values_from_gui()
            self.power_meas() # <-- popup window is offered within here 
            self.download_to_ref_file()
    

    If the user hits 'stop' on the popup window. A function gets called that just has

    def stop_program(self):
        self.thread.terminate()
        # self.thread.stop() <- also have tried this...to no avail
    

    This function gets called, but does not stop the thread.

    JonBJ 1 Reply Last reply
    0
    • F freemanl144

      I have an application that just basically runs through a set of functions. Within those functions there is a popup window that allows the user to cancel out. I want this to stop everything that is happening. I was trying to use QThreading to do this. I start the thread with a button click using

      self.thread = QThread()
      self.ui.pb_start.clicked.connect(lambda: self.thread.started.connect(self.start_button_click()))
      

      The function start_button_click is just a function that makes other function calls. Something along these lines

      def start_button_click(self):
              self.get_input_vals_from_gui()
              self.write_to_log_box("Starting")
              self.get_ranges_for_inputs()
              self.get_values_from_gui()
              self.power_meas() # <-- popup window is offered within here 
              self.download_to_ref_file()
      

      If the user hits 'stop' on the popup window. A function gets called that just has

      def stop_program(self):
          self.thread.terminate()
          # self.thread.stop() <- also have tried this...to no avail
      

      This function gets called, but does not stop the thread.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @freemanl144
      You cannot do anything to or with the UI in anything other than the main thread. As a newcomer to Qt you can be sure you will want to avoid threads at all costs.

      F 1 Reply Last reply
      1
      • JonBJ JonB

        @freemanl144
        You cannot do anything to or with the UI in anything other than the main thread. As a newcomer to Qt you can be sure you will want to avoid threads at all costs.

        F Offline
        F Offline
        freemanl144
        wrote on last edited by
        #3

        @JonB Okay so then how should I go about stopping my processes? If the user hits the 'quit' I want to be able to stop everything that is running but leave the application up. I would have assumed that by having the actual functions running in a separate thread that would be accomplished.

        JonBJ 1 Reply Last reply
        0
        • F freemanl144

          @JonB Okay so then how should I go about stopping my processes? If the user hits the 'quit' I want to be able to stop everything that is running but leave the application up. I would have assumed that by having the actual functions running in a separate thread that would be accomplished.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @freemanl144
          If you really have long running, non-UI processing to do go ahead and use threads and don't get it wrong. Doesn't alter the fact that threads cannot address the UI. Use signals from your threads to inform the UI thread of something for it to act on. Use a signal from your main thread to secondary threads if you want to tell them to exit. If the UI needs to allow the user to interrupt, put up a dialog and send a signal to the sub-threads (preferably) or terminate them (not so preferably).

          1 Reply Last reply
          1

          • Login

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