我想寫一個Qt GUI應用程序,它可以與我從Qt GUI應用程序處理信息的可執行文件進行通信。Qt通過管道到可執行的Linux的雙向通信
我可以理解並已經能夠實現一個單向的popen()管道,它允許我只將信息發送到命令行實用程序,但輸出只出現在Qt底部的應用程序輸出窗口中窗口。
我一直在尋找互聯網,我想我必須使用fork()和exec()兩個管道。
我的問題是沒有人知道這個或一些例子的好教程或任何人都可以看到代碼來實現這個。
謝謝。
編輯::
我這裏有這個代碼,但我在哪裏,我應該把這個困惑。如果我插入到我的Qt GUI應用程序中,關閉管道會導致錯誤。
再編輯::
這是我的Qt GUI按鈕單擊事件。但是,我收到了很多錯誤,說關閉的管道部件有問題。
mainwindow.cpp:85: error: no matching function for call to ‘MainWindow::close(int&)’
關閉管道部件有什麼問題?
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;
}