how can i post an event from a thread to another?
my application have a thread that read from a serial port and another
thread that wait for something received on the serial port.
I cannot use postEvent because in one thread i don'e have reference to
the other...
How can i do that?
Thanks,
Riccardo
_______________________________________________
Interest mailing list
Inte...@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
If you just want to signal that something has arrived, why not simply use signals and slots ? They work across threads.
Hope this helps
Samuel
De : Riccardo Roasio <riccard...@gmail.com>
À : inte...@qt-project.org
Envoyé le : Lundi 2 avril 2012 18h05
Objet : [Interest] post event between threads
Then you cannot post events between the threads. You could just revert
to plain old function calls, ensuring proper locking.
Also note, to "post events into a thread" you actually need a QEventLoop
running in that thread.
Andreas
You don't post events to threads. You post events to objects only.
The events are delivered in each object's associated thread.
So, use:
QCoreApplication::postEvent(myObject, new MyEvent);
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Intel Sweden AB - Registration Number: 556189-6027
Knarrarnäsgatan 15, 164 40 Kista, Stockholm, Sweden
just use Qt::QueuedConnection, by default it is a Qt::AutoConnection
for me it works well,
http://qt-project.org/doc/qt-4.8/qobject.html#connect
http://qt-project.org/doc/qt-4.8/qt.html#ConnectionType-enum
Tue, 3 Apr 2012 09:07:37 +0200 от Riccardo Roasio <riccard...@gmail.com>:
If you emit a signal and the receiver lives on the current thread and
the connect() call did not use Qt::QueuedConnection, then it is
"blocking". If you use Qt::DirectConnection or
Qt::BlockingQueuedConnection then it is always "blocking".
But if the receiver lives on another thread or if you specified
Qt::QueuedConnection then the signal puts an object on the event loop
and returns immediately.
--
Lincoln Ramsay - Senior Software Engineer
Qt Development Frameworks, Nokia - http://qt.nokia.com/
i will try!
thanks everybody
- gui thread (main)
- serial rx thread
- scriptmanager thread
|
|----- script runner 1 thread (started from scriptsmanager)
|
|----- script runner 2 thread (started from scriptsmanager)
|
|----- script runner N thread (started from scriptsmanager)
now the event is emitted from serial rx thread
the main thread is able to catch the signal
the scriptmanager is able to catch the signal
the scriptrunner is NOT able to catch the signal
the only difference is that the sciptrunner has no parent (in the
construtor the parent is NULL)
Riccardo
Samuel
here serial rx threaad emit the signal:
emit MessageReceivedSignal();
here the main thread catch the signal:
connect(rxRoutine, SIGNAL(MessageReceivedSignal()),this,
SLOT(TestSlot()),Qt::QueuedConnection);
here is the constructor of scriptrunner that should catch the signal
but it doesn't
ScriptRunner::ScriptRunner(QObject *parent,Configuration *c,Script
*s,TxRoutine *t,RxRoutine *r,ComunicationLogConfigList *l,Message1281*
m) :
QThread(parent)
{
message1281=m;
comunicationLog=l;
activeConfiguration=c;
txRoutine=t;
rxRoutine=r;
myScript=s;
exit=false;
currentStep=0;
connect(rxRoutine, SIGNAL(MessageReceivedSignal()),this,
SLOT(TestSlot()),Qt::QueuedConnection);
}
and here is when i create the thread scriptrunner:
qDebug()<<"going to start... "<<tmpScript->GetName();
tmpRunner=new
ScriptRunner(0,activeConfiguration,tmpScript,txRoutine,rxRoutine,comunicationLog,message1281);
tmpScript->SetRunning(true);
tmpRunner->start();
runners.append(tmpRunner);
Please read ALL of the following docs, wiki articles, blog posts:
http://qt-project.org/doc/qt-4.8/thread-basics.html
http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/
http://qt-project.org/wiki/Threads_Events_QObjects
http://qt-project.org/wiki/QThreads_general_usage
http://labs.qt.nokia.com/2006/12/04/threading-without-the-headache/
--
Giuseppe D'Angelo
André
Read those docs.
In essence, you want something like this:
QThread *thread = new QThread;
MyObject *object = new MyObject;
object->moveToThread(thread);
thread->start();
connect(something, SIGNAL(blah()), object, SLOT(foo()));
--
Lincoln Ramsay - Senior Software Engineer
Qt Development Frameworks, Nokia - http://qt.nokia.com/