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. How to move QTimer from GUI thread?
Forum Updated to NodeBB v4.3 + New Features

How to move QTimer from GUI thread?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 518 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.
  • B Offline
    B Offline
    BD9a
    wrote on last edited by
    #1

    Hey, how can I move QTimer from GUI thread? When I'm printing currentThreadId() from run(), I got diffrent threadId than calling currentThreadId() from main(). But when printing from loop() I got the same threadId like from main().
    My current code: https://github.com/fire7d/TestProject

    1 Reply Last reply
    0
    • B BD9a

      @Christian-Ehrlicher In this code it's not needed, but I want to learn on this one instead of messing in other projects

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @BD9a
      Hi
      Docs says
      "It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots and invoked methods will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; "

      1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        As always - please read the QThread documentation and don't use threads when you did not understood it.
        Your Printer object (= QThread object) lives in the main thread. Only stuff inside run() is in the newley created thread. Therefore your slot is executed in the main thread.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        3
        • B Offline
          B Offline
          BD9a
          wrote on last edited by
          #3

          So I have to add moveToThread(this); in constructor? Everything else is fine?

          Christian EhrlicherC 1 Reply Last reply
          0
          • B BD9a

            So I have to add moveToThread(this); in constructor? Everything else is fine?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @BD9a said in How to move QTimer from GUI thread?:

            So I have to add moveToThread(this); in constructor? Everything else is fine?

            No, you should follow the examples in the documentation. Or remove the threading completely - it's not needed in your code at all.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            B 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @BD9a said in How to move QTimer from GUI thread?:

              So I have to add moveToThread(this); in constructor? Everything else is fine?

              No, you should follow the examples in the documentation. Or remove the threading completely - it's not needed in your code at all.

              B Offline
              B Offline
              BD9a
              wrote on last edited by
              #5

              @Christian-Ehrlicher In this code it's not needed, but I want to learn on this one instead of messing in other projects

              mrjjM 1 Reply Last reply
              0
              • B BD9a

                @Christian-Ehrlicher In this code it's not needed, but I want to learn on this one instead of messing in other projects

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @BD9a
                Hi
                Docs says
                "It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots and invoked methods will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; "

                1 Reply Last reply
                3

                • Login

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