PDA

View Full Version : how can thread receive data sent by main thread



Myx
28th April 2009, 14:12
got this


#include <QThread>

class QString;

class SerialThread : public QThread
{
Q_OBJECT

public:
SerialThread(QObject *parent = 0);

protected:
void run();

signals:
void testowy(QString);
};

while putting signal-slot mechanism to deliver some data from thread to my main its enough to do something like this in main thread as i got

SerialThread thread;



(...)

connect(&thread, SIGNAL(testowy(QString)),msg,SLOT(append(QString)) );

but i would like also to send a QString from main thread to thread

I could call something like thread.publicFunction(QString) from main but i wonder if its a propper way

Grimlock
28th April 2009, 16:01
Try adding exec(); at the end of the run function. Its going to start an event loop for the thread and you will be able to connect some signals to solots in the thread, using Qt::QueuedConnection.

jpn
29th April 2009, 06:27
Just be aware that having slots in a QThread subclass is problematic. Please read the Multithreading (http://labs.qtsoftware.com/page/Projects/DevDays/DevDays2007) material by Bradley T. Hughes, starting from page 33 to understand why.

Myx
29th April 2009, 08:20
Hello
tx for replies!


Just be aware that having slots in a QThread subclass is problematic. Please read the Multithreading (http://labs.qtsoftware.com/page/Projects/DevDays/DevDays2007) material by Bradley T. Hughes, starting from page 33 to understand why.

just read it and hope understood as well:)

anyway it should not be so problematic in my application

just short question

as i'm sending data fom thread to main i use a &thread pointer in connect()...
is there a way to use app.thread() pointer to make a signal-slot connection using connect() in created thread or there is another way to establish this-way connection?

as my main tread is a GUI where i just wish to pick one parameter and send it to Qthread subclass maybe there would be better way to make

label->moveToThread(thread)

Greetz, Myx

Myx
29th April 2009, 11:51
Try adding exec(); at the end of the run function. Its going to start an event loop for the thread and you will be able to connect some signals to solots in the thread, using Qt::QueuedConnection.

by calling thread event loop by exec(); i got problems with signal-slot connection with, as i mentioned above, with stting propper pointer to my main thread

jpn
29th April 2009, 16:03
label->moveToThread(thread)

What are you trying to do? You must not touch GUI in a worker thread:

Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.

Myx
29th April 2009, 18:17
What are you trying to do? You must not touch GUI in a worker thread:

just gave a guess but didnt try it this way...
i made something like this



class SerialThread : public QThread
{
Q_OBJECT

public:
SerialThread(QObject *mainGUI, QObject *parent = 0)
{
GUI=mainGUI;
}

protected:
void run()
{
connect(GUI,SIGNAL(),this,SLOT());
exec();
}

private:
QObject *GUI;

(...)
};

and while creating a thread



SerialThread *thread=new SerialThread(this);