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. Odd issue, thread worker & timmer not running properly
Forum Updated to NodeBB v4.3 + New Features

Odd issue, thread worker & timmer not running properly

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyside2python
10 Posts 4 Posters 1.1k Views 2 Watching
  • 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    I've made this test >

    from PySide6.QtCore import QObject, Signal, QTimer, QThread
    import sys
    
    from PySide6.QtWidgets import QWidget, QApplication
    
    
    class myWorker(QObject):
        triggerSignal = Signal()
    
        def __init__(self, threadName="newThread", threadInterval=1000):
            QObject.__init__(self)
            self.trigger = QTimer(self)
            # self.trigger.timeout.connect(self.triggerSignal)
            self.trigger.timeout.connect(self.ehh)
            self.trigger.setInterval(threadInterval)
            self.threadWorker = QThread()
            self.threadWorker.setObjectName(threadName)
            self.threadWorker.started.connect(self.startTimerAction)
            self.moveToThread(self.threadWorker)
    
        def ehh(self):
            print("gsdgsdf")
    
        def startTimerAction(self):
            print("5325235")
            self.trigger.start()
            print(self.trigger.isActive(), self.trigger.isSingleShot(), self.trigger.interval(), self.threadWorker.objectName(),QThread.currentThread(),self.thread())
    
        def deleteWorker(self):
            self.trigger.stop()
            self.trigger = None
            self.threadWorker.quit()
            self.threadWorker.wait()
            self.threadWorker = None
            self.deleteLater()
            # self.deleteLater()
            # self.threadWorker.deleteLater()
            # self.threadWorker = None
    
        def startWorker(self, time=-1):
            if time != -1:
                self.trigger.setInterval(time)
            print("Starting worker?")
            self.threadWorker.start()
    
    a = QApplication()
    
    d = myWorker()
    d.startWorker()
    w = QWidget()
    w.show()
    
    sys.exit(a.exec_())
    

    Which I'm using to create a Timmer that runs in other thread that calls a function. It allowes me to not have while loop, but a timmed function call latter in my app.
    Strange thing, the exact same code in c++ works just fine, but in pyside it does not...
    Any hints?

    TIA

    kshegunovK 1 Reply Last reply
    0
    • D Dariusz

      Hey

      I've made this test >

      from PySide6.QtCore import QObject, Signal, QTimer, QThread
      import sys
      
      from PySide6.QtWidgets import QWidget, QApplication
      
      
      class myWorker(QObject):
          triggerSignal = Signal()
      
          def __init__(self, threadName="newThread", threadInterval=1000):
              QObject.__init__(self)
              self.trigger = QTimer(self)
              # self.trigger.timeout.connect(self.triggerSignal)
              self.trigger.timeout.connect(self.ehh)
              self.trigger.setInterval(threadInterval)
              self.threadWorker = QThread()
              self.threadWorker.setObjectName(threadName)
              self.threadWorker.started.connect(self.startTimerAction)
              self.moveToThread(self.threadWorker)
      
          def ehh(self):
              print("gsdgsdf")
      
          def startTimerAction(self):
              print("5325235")
              self.trigger.start()
              print(self.trigger.isActive(), self.trigger.isSingleShot(), self.trigger.interval(), self.threadWorker.objectName(),QThread.currentThread(),self.thread())
      
          def deleteWorker(self):
              self.trigger.stop()
              self.trigger = None
              self.threadWorker.quit()
              self.threadWorker.wait()
              self.threadWorker = None
              self.deleteLater()
              # self.deleteLater()
              # self.threadWorker.deleteLater()
              # self.threadWorker = None
      
          def startWorker(self, time=-1):
              if time != -1:
                  self.trigger.setInterval(time)
              print("Starting worker?")
              self.threadWorker.start()
      
      a = QApplication()
      
      d = myWorker()
      d.startWorker()
      w = QWidget()
      w.show()
      
      sys.exit(a.exec_())
      

      Which I'm using to create a Timmer that runs in other thread that calls a function. It allowes me to not have while loop, but a timmed function call latter in my app.
      Strange thing, the exact same code in c++ works just fine, but in pyside it does not...
      Any hints?

      TIA

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @Dariusz said in Odd issue, thread worker & timmer not running properly:

      Any hints?

      You must say what exactly isn't running properly. From what I can tell the code is semantically correct (even though keeping the thread instance inside the worker object is very dubious design).

      Read and abide by the Qt Code of Conduct

      D 1 Reply Last reply
      0
      • kshegunovK kshegunov

        @Dariusz said in Odd issue, thread worker & timmer not running properly:

        Any hints?

        You must say what exactly isn't running properly. From what I can tell the code is semantically correct (even though keeping the thread instance inside the worker object is very dubious design).

        D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        @kshegunov said in Odd issue, thread worker & timmer not running properly:

        @Dariusz said in Odd issue, thread worker & timmer not running properly:

        Any hints?

        You must say what exactly isn't running properly. From what I can tell the code is semantically correct (even though keeping the thread instance inside the worker object is very dubious design).

        Once running the app there should be print("gsdgsdf") every 1000 but it does not happen. The timmer does not expire/call a function from the thread.

        kshegunovK 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          No warning on the console ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            No warning on the console ?

            D Offline
            D Offline
            Dariusz
            wrote on last edited by
            #5

            @SGaist said in Odd issue, thread worker & timmer not running properly:

            Hi,

            No warning on the console ?

            Nope, I've no idea why would it not run, the timer does not trigger the function when its running in other thread.

            jsulmJ 1 Reply Last reply
            0
            • D Dariusz

              @SGaist said in Odd issue, thread worker & timmer not running properly:

              Hi,

              No warning on the console ?

              Nope, I've no idea why would it not run, the timer does not trigger the function when its running in other thread.

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

              @Dariusz Your QTimer is not created in the new thread. You should create it in startTimerAction to make sure it is created in the new thread.

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

              D 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Dariusz Your QTimer is not created in the new thread. You should create it in startTimerAction to make sure it is created in the new thread.

                D Offline
                D Offline
                Dariusz
                wrote on last edited by
                #7

                @jsulm said in Odd issue, thread worker & timmer not running properly:

                @Dariusz Your QTimer is not created in the new thread. You should create it in startTimerAction to make sure it is created in the new thread.

                Does it not get moved to new thread when its parent object gets moved to a new thread?

                TIA

                jsulmJ 1 Reply Last reply
                0
                • D Dariusz

                  @jsulm said in Odd issue, thread worker & timmer not running properly:

                  @Dariusz Your QTimer is not created in the new thread. You should create it in startTimerAction to make sure it is created in the new thread.

                  Does it not get moved to new thread when its parent object gets moved to a new thread?

                  TIA

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

                  @Dariusz Ah, you're right, you set parent.

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

                  1 Reply Last reply
                  0
                  • D Dariusz

                    @kshegunov said in Odd issue, thread worker & timmer not running properly:

                    @Dariusz said in Odd issue, thread worker & timmer not running properly:

                    Any hints?

                    You must say what exactly isn't running properly. From what I can tell the code is semantically correct (even though keeping the thread instance inside the worker object is very dubious design).

                    Once running the app there should be print("gsdgsdf") every 1000 but it does not happen. The timmer does not expire/call a function from the thread.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #9

                    Does the timer start at all? Does the thread start at all?

                    Read and abide by the Qt Code of Conduct

                    D 1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      Does the timer start at all? Does the thread start at all?

                      D Offline
                      D Offline
                      Dariusz
                      wrote on last edited by
                      #10

                      @kshegunov said in Odd issue, thread worker & timmer not running properly:

                      Does the timer start at all? Does the thread start at all?

                      the timer.start() gets called. But after that silence... Maybe everything gets deleted somewhere?

                      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