What is the problem with this QThread class?
-
[quote]Is it possible for the main thread to process events while running some other codes?If not then how it is possible to get updated data while running.please tell me how to design it?please go through the previous reply, i ve edited my requirement in previous post.
may be I need to implement mutex in helper thread and access data from GUI thread,and wait for specific value and then proceed.[/quote]I don't see how this should be a problem ???
Example:
@void MyMainWindow::init()
{
QStringList filesToBeCopied = generateFileList();m_thread = new MyFileCopyThread(filesToBeCopied); connect(m_thread, SIGNAL(copyStarted(QString)), this, SLOT(copyStarted(QString))); connect(m_thread, SIGNAL(copyCompleted(QString)), this, SLOT(copyCompleted(QString))); m_thread.start(); doOtherStuffHereThatDoesNotNeedTheThreadResult();
}
//SLOT
void MyMainWindow::copyStarted(QString name)
{
//Just an example. You can do whatever needs to be done!
myStatusLabel->setText(tr("Now Copying: %1").arg(name));
}//SLOT
void MyMainWindow::copyCompleted(QString name)
{
//Just an example. You can do whatever needs to be done!
myStatusLabel->setText(tr("Copy complete: %1").arg(name));
}@NOTE: After init() function exists, your GUI thread will simply keep on processing events until a Signal form the worker thread arrives – in which case the Slot will be invoked for further processing.
No processEvents() required anywhere…
-
Sir tell me one thing. is there any problem if i access the HelperThread object data through my main thread with ReadWritemutex in helper thread function. Is there any problem here?
[quote author="MuldeR" date="1384094383"][quote]Is it possible for the main thread to process events while running some other codes?If not then how it is possible to get updated data while running.please tell me how to design it?please go through the previous reply, i ve edited my requirement in previous post.
may be I need to implement mutex in helper thread and access data from GUI thread,and wait for specific value and then proceed.[/quote]I don't see how this should be a problem ???
Example:
@void MyMainWindow::init()
{
QStringList filesToBeCopied = generateFileList();m_thread = new MyFileCopyThread(filesToBeCopied); connect(m_thread, SIGNAL(copyStarted(QString)), this, SLOT(copyStarted(QString))); connect(m_thread, SIGNAL(copyCompleted(QString)), this, SLOT(copyCompleted(QString))); m_thread.start(); doOtherStuffHereThatDoesNotNeedTheThreadResult();
}
//SLOT
void MyMainWindow::copyStarted(QString name)
{
//Just an example. You can do whatever needs to be done!
MyStatusLabel->setText(tr("Now Copying: %1").arg(name));
}//SLOT
void MyMainWindow::copyCompleted(QString name)
{
//Just an example. You can do whatever needs to be done!
MyStatusLabel->setText(tr("Copy complete: %1").arg(name));
}@NOTE: After init() function exists, your GUI thread will simply keep on processing events until a Signal form the worker thread arrives – in which case the Slot will be invoked for further processing.
No processEvents() required anywhere…[/quote]
-
[quote author="blue_sky" date="1384095586"]Sir tell me one thing. is there any problem if i access the HelperThread object data through my main thread with ReadWritemutex in helper thread function. Is there any problem here?[/quote]
Not quite sure what you mean?
But you can add a getter and setter function to your Worker thread like this:
@Foo MyWorkerThread::getData(void) const
{
QReadLocker readLock(&m_readWriteLock);
//real assignment operator below, NOT pointer assignment!
Foo data = m_data;
return data;
}MyWorkerThread::setData(const Foo &data)
{
QWriteLocker writeLock(&m_readWriteLock);
//real assignment operator below, NOT pointer assignment!
m_data = data;
}@Now make sure that all access to m_data is done via getData() and setData(). This applies to the GUI thread and the WorkerThread itself!
BUT: If communication between GUI thread and WorkerThread is done solely with Signals & Slots and queued connections, there is no need for such getter/setter functions. Just pass data "by value" in the Signal.