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. [solved]QThread
QtWS25 Last Chance

[solved]QThread

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 909 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.
  • A Offline
    A Offline
    AntonZelenin
    wrote on last edited by AntonZelenin
    #1

    I need to get the message out of another thread few seconds after program started. I created a QThread *anotherThread = new QThread; in a main function and moved an object there:
    logMind->moveToThread(&anotherThread);
    anotherThread->start();
    logMind->greeting();

    Does greeting() function works in the another thread or not? Probably no, because there is a function Sleep(2000) inside and I thought that at-first main window will be shown, and after 2 seconds message will be displayed. But program just waits two seconds and then shows a main window with a message. What am I doing wrong?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      code_fodder
      wrote on last edited by code_fodder
      #2

      This is a very bad idea. Even though you "can" call logMind->greeting(); you should never do this once it is in a different thread. Any variables / data that is initialised will be in the wrong thread - for example if greeting() function creates a new QString object it will actually exist in the main thread and not the "anotherThread".

      Basically once you have moved an object to a different thread, you have to "forget" about it in the current thread. The main way to talk to it is via slots / signals.

      So you need make a slot in logMind and a signal in your calling object, connect the two and then emit a signal to logMain (which may then call greeting()).

      A 1 Reply Last reply
      2
      • C code_fodder

        This is a very bad idea. Even though you "can" call logMind->greeting(); you should never do this once it is in a different thread. Any variables / data that is initialised will be in the wrong thread - for example if greeting() function creates a new QString object it will actually exist in the main thread and not the "anotherThread".

        Basically once you have moved an object to a different thread, you have to "forget" about it in the current thread. The main way to talk to it is via slots / signals.

        So you need make a slot in logMind and a signal in your calling object, connect the two and then emit a signal to logMain (which may then call greeting()).

        A Offline
        A Offline
        AntonZelenin
        wrote on last edited by AntonZelenin
        #3

        @code_fodder
        Hooray, it works) Thanks a lot=)

        C 1 Reply Last reply
        1
        • A AntonZelenin

          @code_fodder
          Hooray, it works) Thanks a lot=)

          C Offline
          C Offline
          code_fodder
          wrote on last edited by
          #4

          @AntonZelenin Well done mate : )

          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