Qt和Linux管道

问题描述:

我编写了一个程序,该程序从用户指定的HTML网站剥离标签.我知道创建一个与此相关的GUI程序,以允许用户输入URL.

I have written a program which strips the tags from a HTML website which the user specifies. I am know creating a GUI program to go with this to allow the user to input the URL.

我有以下代码,它打开一个管道来打开我制作的可执行文件,该文件处理来自QT程序的输入.

I have the following code which opens a pipe to open the executable file I made which processes the input from the QT program.

    QString stringURL = ui->lineEdit->text();
    const char* result;

    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();

    FILE *fid;
    fid = popen("htmlstrip", "w");    //Note that this application is in the PATH
    fprintf(fid, "%s\n", cString);    //Send URL
    pclose(fid);

但是,上面的代码仅允许我写入管道.谁能告诉我如何允许Qt程序将输入发送到可执行文件,然后在执行处理并将其放入Qt程序的文本框/文本区域后,从可执行文件接收输出?

However the code above only allows me to write to the pipe. Could anyone tell me how I would allow the Qt program to send the input to the executable and then receive the output from the executable once it has done the processing and put this into a textbox/textarea in the Qt program?

您可以使用 QProcess .

#include <QDebug>
#include <QProcess>
#include <QString>

int main()
{
    QProcess echo;

    // call your program (e.g. echo) and add your input as argument
    echo.start("echo", QStringList() << "foo bar");

    // wait until your program has finished 
    if (!echo.waitForFinished())
        return 1;

    // read the output
    qDebug() << echo.readAll();

    return 0;
}