Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[Solved]Transfer parameter between QThread
I had a problem.I wrote my code like following, how do I transfer parameter into this thread, when it start?? Thanks in advance.
You have to setup your thread before starting it. If you need to change things at run time, you need setters.
But before that, are you sure you need a QThread derivative ? Wouldn't a worker object be better ?
Jeroentjehome last edited by
Hmm, the QThread object itself is only a wrapper for what SGalst points out. You need a worker object in the thread to be run. Or you need to derive from QThread (but this is discouraged). So have a look at this post:
Then to pass data from one thread to another use signal/slots or mutex protected variables with setter/getter functions.
There's also "this one":http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html
This really depends on the use case. Most of the time the worker object is the right solution, but not always.
Also, depending on the job, QtConcurrent suits even better.
Thanks all your suggestions, Actually, I wrote a program is GUI, and whenever user clicked the button, something would be done and displayed to the QTextEdit of GUI. But If I don't use thread, the GUI would be locked because the thread of GUI is busy.It couldn't respond to user.So should I put work item into worker, then use singnal & slot pass information to GUI that could display to user??
Yes but be careful when you start using multithreading.
Please read the examples and the documentation from Qt's various threading classes (and if you're nor familiar with the thread subject, a good book about them is really a good idea). And only then start designing your application using threads.
Thanks very much for your help : )