【问题标题】:QProcess read and writeQProcess 读写
【发布时间】:2011-02-09 19:32:09
【问题描述】:

我现在正在尝试从 qprocess 读取和写入。我做了一个小测试程序,它接受输入并在屏幕上循环显示。这是我的 Qt 代码

  QString path = "./test";

        tcd = new QProcess(this);
        QStringList args;
        args << "";
        tcd->start(path,args);

        if(!tcd->waitForStarted(3000))
        {
            stdoutput->append("<h1><font color=red>There was a problem starting the software, please try running the program again.</font></h1>");

        }
        tcd->write("hello\n");
        tcd->write("hello\n");
        tcd->write("hello\n");
        tcd->write("hello\n");
        //tcd->write("quit\n");

QObject::connect(tcd, SIGNAL(readyReadStandardOutput()), this, SLOT(appendTextBox()));

除非我发送最后一个退出命令(这会终止我的测试程序),否则这将不起作用。

这是我的读取命令:

void TCD2_GUI::appendTextBox(){
    stdoutput->append("new output available: \n");
    QByteArray newData = tcd->readAllStandardOutput();
    stdoutput->append(QString::fromLocal8Bit(newData));
}

如果我发送退出,我将立即获得程序的所有输出,包括我发送的所有内容。

我在这里做错了什么?

根据要求,这是程序的代码:

int main(int argC[], char* argV[]){
    printf("Welcome!\n");
    char* input = malloc(160);
    gets(input);

    while(strcmp(input,"quit") != 0)
    {
        printf("Got input %s\n", input);
        gets(input);

    }

}

【问题讨论】:

    标签: qt qprocess


    【解决方案1】:

    来自文档:

    QIODevice 的某些子类,例如 作为 QTcpSocket 和 QProcess,是 异步。这意味着 I/O write() 或 read() 等函数 总是立即返回,而 与设备本身的通信 当控制权返回时可能会发生 事件循环。 QIODevice 提供 允许您强制的功能 这些要执行的操作 立即,同时阻止 调用线程并且没有进入 事件循环。

    ...

    waitForBytesWritten() - 这个函数 在调用中挂起操作 线程直到一个数据有效载荷 已写入设备。

    ...

    从主函数调用这些函数, GUI线程,可能会导致你的用户 界面冻结。

    link

    【讨论】:

    • 是的,但我正在捕捉信号,所以我认为这无关紧要。但也许我对这个理解很差。无论如何,我放了一个 tcd->waitForBytesWritten();在我的代码中,仍然没有。编辑:我有一个按钮可以将输入发送到我的代码中,所以在信号启动之后,仍然没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多