[SOLVED] execute a slot with parameters inside a thread
-
wrote on 8 Oct 2013, 13:17 last edited by
Hi,
i have a class with plenty of functions and slots and i want to run these slots in another thread!
but the problem is i don't know how to pass parameters to my slots!
here is a sample code:@
class Conn
{
private slots:
int getConn();
int setConn(QString strPath);
.
.
.
};void MyClass::func1()
{
Conn objConn;
QThread thrConn;
thrConn.setObjectName("thrConn");
objConn.moveToThread(&thrConn);
QObject::connect(&thrConn, SIGNAL(started()), &objConn, SLOT(getConn())));
thrConn.start();
}
@in above code I'm executing slot getConn() in another thread! so far so good. but how about another slot named setConn(QString strPath). how am i supposed to supply that parameter to the slot!
right now I'm using public data members! but it sounds silly! i have loads of slots in that class and how many public data members am i supposed to declare! and also the return value is another problem, as long as slots can't return a value using QObject::Connect approach!
any ideas how to deal with these two problems! -
wrote on 8 Oct 2013, 15:10 last edited by
bq. in above code I’m executing slot getConn() in another thread!
I don't think you are. Read http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html#signals-and-slots-across-threads . The slot is executed in the thread that func1 is called in. Also, as a consequence, I think that getConn is called on an inexistent object.
-
wrote on 8 Oct 2013, 15:29 last edited by
[quote author="mmoll" date="1381245038"]bq. in above code I’m executing slot getConn() in another thread!
I don't think you are. Read http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html#signals-and-slots-across-threads . The slot is executed in the thread that func1 is called in. Also, as a consequence, I think that getConn is called on an inexistent object.[/quote]
so in my case it is like Direct Connection !!
and i should run func1 in a new thread so that getConn is also executed in a new thread. is this right?
if not, would you guide me on how to run that slot in a new thread?
btw, i really appreciate it if you could help me with the parameters and return value problems. -
Hi,
One of the problem you have is that objCon and thrCon are going to be destroyed right after your called thrConn.start() so there's probably nothing that will be done.
Use a signal with the "return" value as a parameter and emit it once the work is done. As for the rest, you should setup your worker object before calling start on the thread, this is normal way to do this.
-
wrote on 9 Oct 2013, 07:27 last edited by
[quote author="SGaist" date="1381263180"]Hi,
One of the problem you have is that objCon and thrCon are going to be destroyed right after your called thrConn.start() so there's probably nothing that will be done.
Use a signal with the "return" value as a parameter and emit it once the work is done. As for the rest, you should setup your worker object before calling start on the thread, this is normal way to do this.[/quote]
i still have some code to execute after thrConn.start() so they don't get destroyed!
@
void MyClass::func1()
{
Conn objConn;
QThread thrConn;
thrConn.setObjectName("thrConn");
objConn.moveToThread(&thrConn);
QObject::connect(&thrConn, SIGNAL(started()), &objConn, SLOT(getConn())));
thrConn.start();
if(thrConn.wait())
{
if(!thrConn.isFinished() | thrConn.isRunning())
{
thrConn.quit();
}
}
}int Conn::getConn()
{
.
.
.if(this->thread())
{
if(this->thread()->objectName() == "thrConn")
{
this->thread()->quit();
}
}
}
@
as i start waiting on the thread, getConn slot get executed and at the end of it i quit thrConn, therefore i suppose getConn is running in thrConn. -
You should have posted that code earlier. Since you are waiting on the thread to end its work, why not just call the function directly ? The call to wait will block your calling thread (probably the main thread)
-
wrote on 9 Oct 2013, 11:19 last edited by
[quote author="SGaist" date="1381310836"]You should have posted that code earlier. Since you are waiting on the thread to end its work, why not just call the function directly ? The call to wait will block your calling thread (probably the main thread)[/quote]
you mean this way?
@void MyClass::func1()
{
Conn objConn;
QThread thrConn;
thrConn.setObjectName("thrConn");
objConn.moveToThread(&thrConn);
thrConn.start();
objConn.getConn();
@yes you are right! i shouldn't wait on the thread because doing so, blocks the main thread and things don't happen synchronously :-)
if this way of function calls across threads are okay, then my problem with function return value and parameters are also solved!
i have read somewhere that we should use signal-slots approach when working with threads because it is thread safe in nature! -
No not this way, your thread would still not make sense.
Anyway, you need to allocate both your objConn and thrConn on the heap so they won't get destroyed at the end of func1. Also connect the finished signal to the deleteLater slots of both objConn and thrConn.
There are several articles you can found about this pattern
1/8