Re: QThread clarification
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
Code:
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.
Re: QThread clarification
Quote:
Originally Posted by
janorcutt
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.
Quote:
also why can't i create a pointer to my thread i.e.
Code:
QThreadClass *thread;
What do you mean you "can't"?
Quote:
i have to pass a reference to the connect function i.e.
Code:
QThreadClass thread;
connect(&thread, SIGNAL(signal(arg)), this, SLOT(slot(arg)));
It's not a reference, it's an address (aka temporary pointer).
Re: QThread clarification
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.
Code:
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:
Code:
connect(thread, SIGNAL(signal(int)), this, SLOT(slot(int)));
whereas the following worked
Code:
// header
QThreadClass thread;
//implimentation
connect(&thread, SIGNAL(signal(arg)), this, SLOT(slot(arg)));
i'm going to have to read my c++ book again
Re: QThread clarification
Quote:
Originally Posted by
janorcutt
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:
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 ;)
Re: QThread clarification
Quote:
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.