PascalSevran
30th April 2014, 13:28
Hi,
I'm currently making a small application using QtCreator where I use RTI DDS to implement some simple request/reply communications.
By clicking on a simple button from a qml designed GUI, I'd like to send a request using a RTI DDS "requester" and then receiving and displaying the reply by using a RTI DDS "replier".
So I created my DDS requester and replier classes, both unheriting from QObject, and made the link with the qml part using setContextProperty.
My problem now is that I'm not very sure what is the right way of using QThreads to launch in paralel both the requester and the replier in a proper way.
What I did for now, which seems to be half working and not very proper is :
In my main :
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
requester req;
replier rep;
QThread requesterThread;
QThread replierThread;
req.moveToThread(&requesterThread);
rep.moveToThread(&replierThread);
req.connect(&requesterThread, SIGNAL(started()), SLOT(runReq())); //where runReq() is the main method of my requester class
rep.connect(&replierThread, SIGNAL(started()), SLOT(runRep()));
req.connect(&requesterThread,SIGNAL(finished()),SLOT(deleteLate r()));
rep.connect(&replierThread,SIGNAL(finished()),SLOT(deleteLater( )));
&requesterThread.connect(&requesterThread,SIGNAL(finished()),SLOT(deleteLate r()));
&replierThread.connect(&replierThread,SIGNAL(finished()),SLOT(deleteLater( )));
//Setting context properties
viewer.rootContext()->setContextProperty("requester",&req );
viewer.rootContext()->setContextProperty("replier", &rep);
viewer.rootContext()->setContextProperty("requesterTHREAD",&requesterThread );
viewer.rootContext()->setContextProperty("replierTHREAD", &replierThread);
viewer.setMainQmlFile(QStringLiteral("qml/ProjectUsecase1/main.qml"));
viewer.showExpanded();
return app.exec();
And then I call : requesterThread.start() and replierThread.start() in my main.qml when the button is clicked.
So, I'm pretty sure there's a better way to create and destroy the threads, anywhere but in the main.cpp. Should I create a class dedicated to the management of my threads ? What's the cleaniest way to destroy the threads after the requester's and replier's execution ?
Thank you very much by advance.
I'm currently making a small application using QtCreator where I use RTI DDS to implement some simple request/reply communications.
By clicking on a simple button from a qml designed GUI, I'd like to send a request using a RTI DDS "requester" and then receiving and displaying the reply by using a RTI DDS "replier".
So I created my DDS requester and replier classes, both unheriting from QObject, and made the link with the qml part using setContextProperty.
My problem now is that I'm not very sure what is the right way of using QThreads to launch in paralel both the requester and the replier in a proper way.
What I did for now, which seems to be half working and not very proper is :
In my main :
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
requester req;
replier rep;
QThread requesterThread;
QThread replierThread;
req.moveToThread(&requesterThread);
rep.moveToThread(&replierThread);
req.connect(&requesterThread, SIGNAL(started()), SLOT(runReq())); //where runReq() is the main method of my requester class
rep.connect(&replierThread, SIGNAL(started()), SLOT(runRep()));
req.connect(&requesterThread,SIGNAL(finished()),SLOT(deleteLate r()));
rep.connect(&replierThread,SIGNAL(finished()),SLOT(deleteLater( )));
&requesterThread.connect(&requesterThread,SIGNAL(finished()),SLOT(deleteLate r()));
&replierThread.connect(&replierThread,SIGNAL(finished()),SLOT(deleteLater( )));
//Setting context properties
viewer.rootContext()->setContextProperty("requester",&req );
viewer.rootContext()->setContextProperty("replier", &rep);
viewer.rootContext()->setContextProperty("requesterTHREAD",&requesterThread );
viewer.rootContext()->setContextProperty("replierTHREAD", &replierThread);
viewer.setMainQmlFile(QStringLiteral("qml/ProjectUsecase1/main.qml"));
viewer.showExpanded();
return app.exec();
And then I call : requesterThread.start() and replierThread.start() in my main.qml when the button is clicked.
So, I'm pretty sure there's a better way to create and destroy the threads, anywhere but in the main.cpp. Should I create a class dedicated to the management of my threads ? What's the cleaniest way to destroy the threads after the requester's and replier's execution ?
Thank you very much by advance.