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. AttributeError: 'testclass' object has no attribute 'moveToThread'
Forum Updated to NodeBB v4.3 + New Features

AttributeError: 'testclass' object has no attribute 'moveToThread'

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 4 Posters 1.1k Views 1 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.
  • I Offline
    I Offline
    Igor86
    wrote on last edited by Igor86
    #1

    Hello!

    Ive been trying to run a class on a different thread, but its thowing an error and I cannot figure out why.

    The error I get: AttributeError: 'testclass' object has no attribute 'moveToThread'
    relevant code is below.

    My setup:

    main.py contains the code posted below,
    TelegramClass.py contains the code I want to run on a new thread.

    in the main.py file i have this code:

    import TelegramClass
    ...
    class Ui_MainWindow(object):
            ...
            self.TelegramThread()
    
     def TelegramThread(self):
            self.telegramThread = QThread()
            self.telegramWorker = TelegramClass.testclass()
            self.telegramWorker.moveToThread(self.telegramThread)
            self.telegramThread.start()
    

    in my TelegramClass.py:

    class testclass():
     def __init__(self):
            self.run()
     def run(self):
            print("ok")
    

    what am I doing wrong?
    Any help appreciated.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      testclass needs to inherit from QObject.

      (Z(:^

      1 Reply Last reply
      2
      • I Offline
        I Offline
        Igor86
        wrote on last edited by
        #3

        Thank you,

        i changed it to this, and now it works:

        class testclass(QObject):
            def __init__(self):
                QObject.__init__(self)
                self.run()
        
            def run(self):
                print("ok")
        
        

        but now I am facing a new problem when I try to connect a signal:

        if I add this before i call the start():

              self.telegramThread = QThread()
              self.telegramWorker = TelegramClass.testclass()                
              self.telegramWorker.moveToThread(self.telegramThread)
              self.tgSignal.connect(self.telegramWorker.run)       
              self.telegramThread.start()
        

        I get the error message:

        TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context

        jsulmJ 1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          No idea, sorry.

          (Z(:^

          1 Reply Last reply
          0
          • I Igor86

            Thank you,

            i changed it to this, and now it works:

            class testclass(QObject):
                def __init__(self):
                    QObject.__init__(self)
                    self.run()
            
                def run(self):
                    print("ok")
            
            

            but now I am facing a new problem when I try to connect a signal:

            if I add this before i call the start():

                  self.telegramThread = QThread()
                  self.telegramWorker = TelegramClass.testclass()                
                  self.telegramWorker.moveToThread(self.telegramThread)
                  self.tgSignal.connect(self.telegramWorker.run)       
                  self.telegramThread.start()
            

            I get the error message:

            TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context

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

            @Igor86 said in AttributeError: 'testclass' object has no attribute 'moveToThread':

            TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context

            What line of code causes this?

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

            I 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Igor86 said in AttributeError: 'testclass' object has no attribute 'moveToThread':

              TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context

              What line of code causes this?

              I Offline
              I Offline
              Igor86
              wrote on last edited by
              #6

              @jsulm said in AttributeError: 'testclass' object has no attribute 'moveToThread':

              @Igor86 said in AttributeError: 'testclass' object has no attribute 'moveToThread':

              TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context

              What line of code causes this?

              self.tgSignal.connect(self.telegramWorker.run)

              this one

              JonBJ 1 Reply Last reply
              0
              • I Igor86

                @jsulm said in AttributeError: 'testclass' object has no attribute 'moveToThread':

                @Igor86 said in AttributeError: 'testclass' object has no attribute 'moveToThread':

                TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context

                What line of code causes this?

                self.tgSignal.connect(self.telegramWorker.run)

                this one

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

                @Igor86

                class Ui_MainWindow(object):

                self.tgSignal.connect(self.telegramWorker.run)
                TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context

                If self is Ui_MainWindow that too will need to inherit QObject, you have it only from object? Everything which emits a signal, and probably in practice anything which has a slot, needs to derive from QObject.

                I 1 Reply Last reply
                2
                • JonBJ JonB

                  @Igor86

                  class Ui_MainWindow(object):

                  self.tgSignal.connect(self.telegramWorker.run)
                  TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context

                  If self is Ui_MainWindow that too will need to inherit QObject, you have it only from object? Everything which emits a signal, and probably in practice anything which has a slot, needs to derive from QObject.

                  I Offline
                  I Offline
                  Igor86
                  wrote on last edited by
                  #8

                  @JonB thank you, nwo it works!

                  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