I am trying to write a Qt GUI application which can communicate with an executable file I have made which processes the information from the Qt GUI application.
I can understand and have been able to implement a one way popen() pipe which allows me to only send the information to the command line utility, but the output only appears on the application output window at the bottom of the Qt window.
I have been looking around the internet and I think I have to use two pipes with fork() and exec().
My question is does anyone know where a good tutorial for this or some examples or could anyone show me the code to achieve this.
Thanks.
EDIT::
I have this code here but I am confused on where I should put this. If I put in into my Qt GUI application, the close pipes bring back errors.
EDIT AGAIN::
This is my Qt GUI push button click event. However I am getting alot of errors saying that there is something wrong with the close pipe parts.
mainwindow.cpp:85: error: no matching function for call to ‘MainWindow::close(int&)’
what is wrong with the close pipe parts?
void MainWindow::on_pushButton_clicked()
{
QString stringURL = ui->lineEdit->text();
ui->labelError->clear();
if(stringURL.isEmpty() || stringURL.isNull()) {
ui->labelError->setText("You have not entered a URL.");
stringURL.clear();
return;
}
std::string cppString = stringURL.toStdString();
const char* cString = cppString.c_str();
char* output;
//These arrays will hold the file id of each end of two pipes
int fidOut[2];
int fidIn[2];
//Create two uni-directional pipes
int p1 = pipe(fidOut); //populates the array fidOut with read/write fid
int p2 = pipe(fidIn); //populates the array fidIn with read/write fid
if ((p1 == -1) || (p2 == -1)) {
printf("Error\n");
return 0;
}
//To make this more readable - I'm going to copy each fileid
//into a semantically more meaningful name
int parentRead = fidIn[0];
int parentWrite = fidOut[1];
int childRead = fidOut[0];
int childWrite = fidIn[1];
//////////////////////////
//Fork into two processes/
//////////////////////////
pid_t processId = fork();
//Which process am I?
if (processId == 0) {
/////////////////////////////////////////////////
//CHILD PROCESS - inherits file id's from parent/
/////////////////////////////////////////////////
close(parentRead); //Don't need these
close(parentWrite); //
//Map stdin and stdout to pipes
dup2(childRead, STDIN_FILENO);
dup2(childWrite, STDOUT_FILENO);
//Exec - turn child into sort (and inherit file id's)
execlp("htmlstrip", "htmlstrip", "-n", NULL);
} else {
/////////////////
//PARENT PROCESS/
/////////////////
close(childRead); //Don't need this
close(childWrite); //
//Write data to child process
char strMessage[] = cString;
write(parentWrite, strMessage, strlen(strMessage));
close(parentWrite); //this will send an EOF and prompt sort to run
//Read data back from child
char charIn;
while ( read(parentRead, &charIn, 1) > 0 ) {
output = output + (charIn);
}
close(parentRead); //This will prompt the child process to quit
}
return 0;
}
For IPC b/w Qt Applications you can go for Shared Memory or Local Sockets/Server.
Look at the shared Memory example here:
http://www.developer.nokia.com/info/sw.nokia.com/id/ad9f51ff-c18f-4bd7-8bb8-cd9681704783/Qt_QSharedMemory_Example_v1_2_en.zip.html