PDA

View Full Version : Interconnect Qt application & External .cpp application!!!!



Krish
28th February 2008, 14:07
Hello! Everybody,
I am planning to design a GUI for an external .cpp application. I am using RedHat Linux with Qt3.3.3. I want user to select some data from GUI like int variable; string name; etc and then GUI should send this selected data as arguments to the external .cpp application for its calculation. And finally after calculations are over in external application i need to send the result to GUI for displaying it to the user.

For example i have: -
In Qt project: - i.) main.cpp ii.)gui.cpp iii.)gui.h
In External application: - i.) MyProgram.cpp working using normal c++ headers & libraries.

"MyProgram.cpp is not a part of the Qt project, its an external application"

I will be obliged if suggested proper path/direction to do the desired work.

Thanks very much in advance.

THRESHE
28th February 2008, 14:15
I don't know much about Qt 3 but I would suggest to use sockets for interprocess communication. Personally I use QUdpSocket to communicate between apps but they are both Qt 4 apps...

high_flyer
28th February 2008, 14:17
there are various ways to do this.
And it has nothing to do with Qt.
Your best bet is probably using sockets - in that case you can use Qt socket classes.
(you can also write you non gui applicatino with Qt)
But shared memory and pipes are also options.
You should decide which option to use by considering your implementation requirements.

Krish
28th February 2008, 15:25
Hello! high_flyer,
Thanks for taking out ime & replying me. As you said in ur reply: -

Your best bet is probably using sockets - in that case you can use Qt socket classes.(you can also write you non gui applicatino with Qt)

Sorry to say but i didn't completely understand it. I took a look at QSocket class & its functions.

Even if i use QSocket class for the purpose, how can i connect to host as the application runs on my very same PC? Also for connection how can i get "host" & "port" parameters?
And after connecting i would be in the folder & how can i start MyProgram.cpp application? Also how could i send the arguments to specific functions in MyProgram.cpp application and get the result back?

Thanks very much if shown or directed to an example as i am pretty new to Qt.

THRESHE
28th February 2008, 15:29
Is it compulsory to use Qt 3 ?

high_flyer
28th February 2008, 15:45
@Krish:
socket programming has nothing to do with Qt, except for the fact that Qt wraps that functionality too.
However, since you are using Qt, have a look at the examples (http://doc.trolltech.com/3.3/network-examples.html).
In addition do a search in google for "socket programming C++ example" - you will get lots of information.

Also, if there is no special reason to use Qt3, I would also recommend to use Qt4.

Krish
28th February 2008, 15:47
Hello! Threshe,
Yes buddy i have to use the same to acheive it. But i am able to use Qt4 can you please broaden my info over how actually did you use QUdpSocket. I mean it just connects you to the host. How can you run the program, call its functions, send data, recieve data n all?
OR can i use QProcess/QThread????

Thanks in advance.

THRESHE
28th February 2008, 16:04
I used QUdpSocket to transfer path for opening files like this


QSocketListener::QSocketListener()
: QObject(parent)
{
socket_ = new QUdpSocket();
socket_->bind(QHostAddress::LocalHost, 40002);
connect(socket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
}

void QSocketListener::SendPath(QString filePath)//Send Data
{
socket_->disconnect();
socket_->writeDatagram(filePath.toUtf8(), QHostAddress::LocalHost, 40002 );
}

void QSocketListener::readPendingDatagrams()
{
QByteArray datagram;
datagram.resize(socket_->pendingDatagramSize());

socket_->readDatagram(datagram.data(), datagram.size());
QString path = datagram;
emit openFile(path);
}

I hope it helps ;)

high_flyer
28th February 2008, 16:05
UDP is not recommended, since this protocol can't deliver you any security regarding the data sent/received as TCP/IP does.

THRESHE
28th February 2008, 16:15
I know but for my purpose it works well :)

Krish
29th February 2008, 09:52
Hello! high_flyer,
Thanks for directing me to this stuff.

However, since you are using Qt, have a look at the examples (http://doc.trolltech.com/3.3/network-examples.html).

Its really a nice class to be used for communicating between more programs. But it has used 2 QT application/program for communicating between them whereas i have one Qt & other Non-Qt applications/programs, because of which i have few questions to ask like: -

1.) Can i simply include the required Qt header files in MyProgram.cpp to create socket, handle connection & transfer data as it is Non-Qt .cpp file? or will it create problems like can't find the required libraries or not compatible?

2.) Also how can i send more than one type of data say int, str,.... at the same time through the same socket and recieve it properly to process them in reciever program's function? Just as we send arguments to the functions!!!!

3.) As you had said in previous reply that i can write MyProgram.cpp in Qt itself, how as it does its own work using different libraries & all?

Thanks again in advance.:)

Krish
29th February 2008, 09:57
Hello! Threshe,
Thanks for showing me coded example. But i would like to ask few things more like: -
1.) As my both applications are running on the same machine, will the parameter "host"=localhost or something else? Anyway if the both of them were on different PC's how can i get the host name properly?

2.)Please let me know as you used port 40002 for i/o connection, which should be used by me or from where to get that info?

Thanks again in advance.:)

THRESHE
29th February 2008, 13:35
1.) As my both applications are running on the same machine, will the parameter "host"=localhost or something else? Anyway if the both of them were on different PC's how can i get the host name properly?
For my purposes I've used only localHost so I cannot help you with other hosts sorry...


2.)Please let me know as you used port 40002 for i/o connection, which should be used by me or from where to get that info?

Honestly I've just picked a random port number :o