View Full Version : QThread clarification
janorcutt
5th January 2011, 11:59
Hi peeps.
I'm creating a worker thread to keep my app responsive while accessing a database etc,
and had a couple of questions.
1. can i call slots in my worker thread from my main thread before i start the thread?
and if so
2. how do i stop this from happening?
also why can't i create a pointer to my thread i.e.
QThreadClass *thread;
I get a sigsev when i try to connect signals and slots, i have to pass a reference to the connect function i.e.
QThreadClass thread;
connect(&thread, SIGNAL(signal(arg)), this, SLOT(slot(arg)));
hope i'm not being too daft.....
MarekR22
5th January 2011, 12:48
I recommend to use this kind of pattern:
Create simple class inheriting from QObject which handles data base and emits signals and have some slots (lets call it DataBaseHandler).
then create thread like that
QThread *thread = new QThread(this);
DataBaseHandler *database = new DataBaseHandler(thread);
database->moveToThread(thread);
// example connections:
connect(this, SIGNAL(buttonWasPressed()),
database, SLOT(featchSomeData()));
connect(database, SIGNAL(dataReady(QString)),
someLabel, SLOT(setText(QString)));
thread->start();
Qt will do all tricks required for such simple case.
wysota
5th January 2011, 13:39
1. can i call slots in my worker thread from my main thread before i start the thread?
You mean slots of the worker thread or slots of the worker thread object? As for the former - no, because the thread doesn't exist yet so no objects live in that thread. As for the latter - yes but the thread object is associated with the thread that created the object and not with the thread controlled by the object.
also why can't i create a pointer to my thread i.e.
QThreadClass *thread;
What do you mean you "can't"?
i have to pass a reference to the connect function i.e.
QThreadClass thread;
connect(&thread, SIGNAL(signal(arg)), this, SLOT(slot(arg)));
It's not a reference, it's an address (aka temporary pointer).
janorcutt
6th January 2011, 00:33
ok thanks for the info.
as for the slots, i'm not going to worry about it too much as i only need to emit signals which i can already do, as long as i register the types e.g.
qRegisterMetaType<object>();
as for the pointer, i should have clarified, i can create the pointer however my program sigsev'd at run time at the following code:
connect(thread, SIGNAL(signal(int)), this, SLOT(slot(int)));
whereas the following worked
// header
QThreadClass thread;
//implimentation
connect(&thread, SIGNAL(signal(arg)), this, SLOT(slot(arg)));
i'm going to have to read my c++ book again
wysota
6th January 2011, 01:12
as for the pointer, i should have clarified, i can create the pointer however my program sigsev'd at run time at the following code:
connect(thread, SIGNAL(signal(int)), this, SLOT(slot(int)));
Because you created a pointer and not an object. It's like you had a parking space without a car in it. Try opening a door of an empty parking lot ;)
high_flyer
6th January 2011, 10:11
Because you created a pointer and not an object.
To clear possible confusion, just creating an object will not do either - you need the pointer to point (have an address) of a created object - this is called "initializing a pointer".
If initializing pointers is something new to you, or how pointers work, I really suggest you don't jump straight in to threads, which is a complex issue on its own.
First be comfortable with the language, then try more complex stuff.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.