How to update main windows from running thread , the right way
-
GetValFromConfig() is from singleton object that holds the app config does it matter ?
directconnection is there because i saw recommendations in the net to stick it there -
Thanks Andre for your very informative answer but i must admit , you confused me here ,
can you direct me to link that shows me some code , how it suppose to look ?
Thanks -
[quote author="umen242" date="1325148899"]Thanks Andre for your very informative answer but i must admit , you confused me here ,
can you direct me to link that shows me some code , how it suppose to look ?
Thanks[/quote]Yeah, sorry, it is a bit confusing.
"This wiki article":http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects has a lot of detail on this topic. The code below is lifted from this article:@
class Worker : public QObject
{
Q_OBJECTpublic slots:
void doWork() {
/* ... */
}
};/* ... */
QThread *thread = new QThread;
Worker *worker = new Worker;
connect(obj, SIGNAL(workReady()), worker, SLOT(doWork()));
worker->moveToThread(thread);
thread->start();
@ -
Thanks again, im reading the wiki you posted , after implementing this method of thread creation
do you think i can still use the pause timer i used in the original thread ? -
Of course you can! Just create your timer in the worker class itself, and replace the doWork() slot with something that starts that timer.
-
i finished to read it , one small thing i didn't understand or didn't noticed what is the "obj" in the :
@connect(obj, SIGNAL(workReady()), worker, SLOT(doWork()));@
you have idea? -
It is an object living in the main thread that signals that there is work to be done for the thread.
-
Hmm in my case is , i need the thread start when some object is initialized , so i guess i can remove it
but when i think about it , what will trigger the DoWork() method ?
for example:
i have this object and method ApiManager->Init() that inside the Init() i want to start the thread to work so this is what is did :
@//Singeltone object
ApiManager::ApiManager()
{
m_Thread = new QThread();
m_Worker = new Worker() // is the same as in the wiki example
m_Worker->moveToThread(m_Thread);
}ApiManager::Init()
{
m_Thread->start();
}@what will trigger me the DoWork here ? or any other method inside the thread ?
i dont need here another mechanize to trigger the signal inside the worker . it needs to start when the thread start. -
So... why don't you connect the thread's started() signal with the doWork() slot? That way, your worker will start to do it's job as soon as the thread starts.
However, note that this setup is most useful for situations where the worker can get more than one request to do some piece of work. If you 'just' need a one-of operation to run in a thread, then you probably are better off using a QRunnable or subclassing QThread and reimplementing the run() method (without using slots on the subclassed QThread, of course).
-
ok so this is what was my initial setup to start the thread i subclass he thread and invoke the QThread start() and the start trigger the QThread::run()
and inside the run i have the code that i show above that cause the problem .
so what do you think is the best solution here ? maybe the timer code is wrong after all .like this :
@ //Singeltone object
ApiManager::ApiManager()
{
// im not using new QThead herem_Thread.start() }
@
-
Some update ,
after implementing the thread as worker object , i eliminated the delay in my gui , now its just works
smooth.
thanks andre !