⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)


  • 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:

    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?

  • 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()).

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

  • @AntonZelenin Well done mate : )

Log in to reply