QProcess output not showing

2019-08-30 01:24发布

I have some C++ code that uses Qt's QProcess to run an scp command in Linux, but I never get any output from the process:

void CopyClass::CopyClass()
{ 
    mpScpProcess = new QProcess(this);
    connect(mpScpProcess, SIGNAL(finished(int)),  this, SLOT(onCopyFinished(int)));
    connect(mpScpProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(onReadCopyOutput()));
}

void CopyClass::startScp()
{
    mpScpProcess->setProcessChannelMode(QProcess::MergedChannels); 
    mpScpProcess->start("scp 192.168.1.100:/file.txt ./");
}

void CopyClass::onCopyFinished(int val)
{
    qWarning("Copy Finished");
}

void CopyClass::onReadCopyOutput()
{
    QString output = mpScpProcess->readAll().data();

    qWarning("Output: %s", qPrintable(output));
}

onCopyFinished gets called, and the scp copy succeeds, but no output ever comes out (onReadCopyOutput is never called). But I know it should output something like this:

file.txt                                     100%  1KB   1.9MB/s   00:00

Anyone know why it's not working? Thanks.

标签: c++ linux qt scp
1条回答
做个烂人
2楼-- · 2019-08-30 01:48

scp does not generate output when its standard output is being redirected to a pipe:

scp src dest > out.txt

You'll see that out.txt is empty. I don't think there's much you can do about it.

查看更多
登录 后发表回答