PDA

View Full Version : QThread Question



ionutdanila
4th January 2011, 19:06
Hello!

I am new to Qt, my question may seem simple, but I searched all over the Internet and didn't find the right answer.

I have a QThread class:



#include "thread.h"

Thread::Thread(QObject *parent)
: QThread(parent)
{
}

void Thread::run(string url)
{
ui->webView->setUrl(QUrl(url.c_str()));

connect(ui->webView, SIGNAL(loadFinished(bool)), SLOT(onLoadFinished(bool)));
connect(ui->webView, SIGNAL(loadProgress(int)), SLOT(onLoadProgress(int)));
//connect(pd, SIGNAL(canceled()), this, SLOT(cancel()));

exec();
}


Of course, this code doesn't work because I can't access ui from my main class.

So the question is: How can I access the widgets in my main class from this class?

tbscope
4th January 2011, 19:36
You can use events or signals and slots.

But be careful with defining slots. Creating slots in a QThread subclass and calling them doesn't always run them in the thread.
I think the best approach is to create a QObject subclass and move the QObject object to a thread instead of subclassing QThread directly.

karlkar
4th January 2011, 19:53
Another way is by giving the Thread class pointer to the widget with ui for example during construction:


Thread::Thread(QObject *parent, QWidget *window)
: QThread(parent),
pointerToWindowWithUi(window)
{
}

where pointerToWindowWithUi (xD) is thread class member.

tbscope
4th January 2011, 20:11
Another way is by giving the Thread class pointer to the widget with ui for example during construction:


Thread::Thread(QObject *parent, QWidget *window)
: QThread(parent),
pointerToWindowWithUi(window)
{
}

where pointerToWindowWithUi (xD) is thread class member.

That breaks the principle that every UI object should remain in one thread only.

wysota
4th January 2011, 20:26
That breaks the principle that every UI object should remain in one thread only.
Not in one thread, in one particular specific thread that is ran by the QApplication object.

ChrisW67
5th January 2011, 00:13
So the question is: How can I access the widgets in my main class from this class?

You can't, they are typically private for a reason. As others have said, you can use signals/slots to transfer pieces of information from one class to another, even across thread boundaries. For example:

The main GUI class exposes a slot that can be used to set the URL, and the two signals related to its private QWebview.
The Thread class exposes a signal that requests a URL change, and the two slots you already have.
When you create instance of the Thread class connect the slots to the signals. This happens outside the Thread class.

If you intend grabbing the content of the web view and processing it in the thread then you should probably rethink the design.

MarekR22
5th January 2011, 14:56
read this (http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/), it should help.
See also (http://www.qtcentre.org/threads/37546-QThread-clarification?p=172647#post172647).

ionutdanila
6th January 2011, 07:23
Thank you all for you answears!