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. How to pass a variable to a worker thread in PyQt?
Forum Updated to NodeBB v4.3 + New Features

How to pass a variable to a worker thread in PyQt?

Scheduled Pinned Locked Moved Solved Qt for Python
11 Posts 3 Posters 6.7k 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.
  • Q qtDevOps

    I want to access textList from Parent class inside Worker class. I tried passing values inside of process but it's not valid ?

    class Parent(BaseClass):
        def __init__(self) -> None:
            super().__init__()
    
        def getText(self, textList):
            self.process = Worker()
            self.process.start()
    
    
    class Worker(QThread):
    
        def run(self):
            for sentence in textList:
                 print(sentence) 
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @qtDevOps said in How to pass a variable to a worker thread in PyQt?:

    I tried passing values inside of process but it's not valid ?

    I don't see you passing anything to the thread...
    You could pass it to the constructor of your thread class.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    3
    • Q Offline
      Q Offline
      qtDevOps
      wrote on last edited by
      #3

      Thank you for responding. It got solved.

      I have another question. How can I access UI element of base class inside worker class?.

      I have PlaintextEdit inside base class which I can use inside parent class. But I want to use it inside worker class. How can I achieve it?

      jsulmJ 1 Reply Last reply
      0
      • Q qtDevOps

        Thank you for responding. It got solved.

        I have another question. How can I access UI element of base class inside worker class?.

        I have PlaintextEdit inside base class which I can use inside parent class. But I want to use it inside worker class. How can I achieve it?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @qtDevOps You should never ever access UI from other threads, only from main (GUI) thread.
        If you want to change something in the UI, then emit a signal from the thread and change the UI in the slot connected to this signal.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          qtDevOps
          wrote on last edited by
          #5

          I'm trying to get the currenttext of combobox inside of worker class. Any hints on how to do that?

          JonBJ jsulmJ 2 Replies Last reply
          0
          • Q qtDevOps

            I'm trying to get the currenttext of combobox inside of worker class. Any hints on how to do that?

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

            @qtDevOps
            You can't really. Or at least not safely and easily. Why don't you send it to worker when created?

            1 Reply Last reply
            0
            • Q qtDevOps

              I'm trying to get the currenttext of combobox inside of worker class. Any hints on how to do that?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @qtDevOps At which point in time do you need this information? If just at the beginning (when the thread starts), then pass it as parameter to the constructor (I already suggested that before).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              Q 1 Reply Last reply
              0
              • jsulmJ jsulm

                @qtDevOps At which point in time do you need this information? If just at the beginning (when the thread starts), then pass it as parameter to the constructor (I already suggested that before).

                Q Offline
                Q Offline
                qtDevOps
                wrote on last edited by
                #8

                This is the code I'm working on. I have options like MS David, MS Zira inside Combobox and I need to fetch the name so that I can use the specific voice for tts. How can I pass the self.ComboSpeechEngine.currentText() in constructor?

                from PyQt6.QtCore import QThread
                import pyttsx3
                
                from UI import Ui_MainWindow
                textList = []
                
                nativeVoices = {
                    "MS David": "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0",
                    "MS Zira": "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0"
                }
                
                
                class NativeSpeechEngine(Ui_MainWindow):
                    def __init__(self) -> None:
                        super().__init__()
                
                    def getTextList(updatedList):
                        global textList
                
                        textList = updatedList
                
                    def nativeSpeech(self):
                        self.worker = Speak()
                        self.worker.start()
                
                
                class Speak(QThread):
                
                    def run(self):
                        engine = pyttsx3.init()
                        engine.setProperty(
                            "voice", nativeVoices[self.ComboSpeechEngine.currentText()])
                
                        for sentence in textList:
                            engine.say(sentence)
                            engine.runAndWait()
                
                jsulmJ 1 Reply Last reply
                0
                • Q qtDevOps

                  This is the code I'm working on. I have options like MS David, MS Zira inside Combobox and I need to fetch the name so that I can use the specific voice for tts. How can I pass the self.ComboSpeechEngine.currentText() in constructor?

                  from PyQt6.QtCore import QThread
                  import pyttsx3
                  
                  from UI import Ui_MainWindow
                  textList = []
                  
                  nativeVoices = {
                      "MS David": "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0",
                      "MS Zira": "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0"
                  }
                  
                  
                  class NativeSpeechEngine(Ui_MainWindow):
                      def __init__(self) -> None:
                          super().__init__()
                  
                      def getTextList(updatedList):
                          global textList
                  
                          textList = updatedList
                  
                      def nativeSpeech(self):
                          self.worker = Speak()
                          self.worker.start()
                  
                  
                  class Speak(QThread):
                  
                      def run(self):
                          engine = pyttsx3.init()
                          engine.setProperty(
                              "voice", nativeVoices[self.ComboSpeechEngine.currentText()])
                  
                          for sentence in textList:
                              engine.say(sentence)
                              engine.runAndWait()
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @qtDevOps said in How to pass a variable to a worker thread in PyQt?:

                  How can I pass the self.ComboSpeechEngine.currentText() in constructor?

                  This is really basic stuff:

                  class Speak(QThread):
                      def __init__(self, text):
                          ...
                          self.__text = text
                      def run(self):
                          ...
                  ...
                  def nativeSpeech(self):
                          self.worker = Speak(self.ComboSpeechEngine.currentText())
                          self.worker.start()
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Q 1 Reply Last reply
                  3
                  • jsulmJ jsulm

                    @qtDevOps said in How to pass a variable to a worker thread in PyQt?:

                    How can I pass the self.ComboSpeechEngine.currentText() in constructor?

                    This is really basic stuff:

                    class Speak(QThread):
                        def __init__(self, text):
                            ...
                            self.__text = text
                        def run(self):
                            ...
                    ...
                    def nativeSpeech(self):
                            self.worker = Speak(self.ComboSpeechEngine.currentText())
                            self.worker.start()
                    
                    Q Offline
                    Q Offline
                    qtDevOps
                    wrote on last edited by
                    #10

                    @jsulm Thanks, With few modifications it worked. I'm a QtNoob but no shame in learning :).

                        def nativeSpeech(self):
                            self.worker = Speak(nativeVoices[self.ComboSpeechEngine.currentText()])
                            self.worker.start()
                    
                    
                    class Speak(QThread):
                    
                        def __init__(self, speechID):
                            super().__init__()
                            self.speechID = speechID
                    
                        def run(self):
                            engine = pyttsx3.init()
                            engine.setProperty(
                                "voice", self.speechID)
                    
                            for sentence in textList:
                                engine.say(sentence)
                                engine.runAndWait()
                    
                    jsulmJ 1 Reply Last reply
                    1
                    • Q qtDevOps

                      @jsulm Thanks, With few modifications it worked. I'm a QtNoob but no shame in learning :).

                          def nativeSpeech(self):
                              self.worker = Speak(nativeVoices[self.ComboSpeechEngine.currentText()])
                              self.worker.start()
                      
                      
                      class Speak(QThread):
                      
                          def __init__(self, speechID):
                              super().__init__()
                              self.speechID = speechID
                      
                          def run(self):
                              engine = pyttsx3.init()
                              engine.setProperty(
                                  "voice", self.speechID)
                      
                              for sentence in textList:
                                  engine.say(sentence)
                                  engine.runAndWait()
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @qtDevOps said in How to pass a variable to a worker thread in PyQt?:

                      but no shame in learning

                      Learning is always good :-)
                      Sorry if I was somewhat harsh, this happens sometimes when people ask very basic questions not related to Qt (I usually assume that somebody using Qt knows the programming language basics).

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

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