使用QProcess-> setReadChannelMode(QProcess :: MergedChannels)并使用QProcess-> readall()

时间:2016-02-16 13:09:05

标签: c++ qt

这是我第一次在stackoverflow上提问。我是一个中国女孩,如果我对这个问题的描述有太多的语法错误,你不能轻易理解它,我很抱歉。 以下是我的问题: headerfile:

class AdbDriver : public QObject
{
    Q_OBJECT
private:
    QString PnPutilPath_;
    QProcess *process_;
public:
    explicit AdbDriver(QObject *parent = 0);
    ~AdbDriver();
    void installDriver();
};
AdbDriver::AdbDriver(QObject *parent):QObject(parent){
    PnPutilPath_ = qgetenv("WINDIR") + "\\sysnative\\pnputil.exe";
    process_ = new QProcess();
    process_->setReadChannelMode(QProcess::MergedChannels);
    process_->setStandardOutputFile("E:/log.txt");
}

的资源文件:

 AdbDriver::~AdbDriver(){
        delete process_;
    }

    void AdbDriver::installDriver(){
        QFile file(PnPutilPath_);
        if(file.exists()){
            qDebug()<<"pnputil.exe exist";    
            QString generaladbDriver = "E:/driver_androidusb/generaladb.inf";
            qDebug()<<"the programming include driver:"<<generaladbDriver;
            QFile file(generaladbDriver);
            if(file.exists()){
                qDebug()<<"yes, the driver is right in bihu package";
            }
            else{
                qDebug()<<"loss driver in bihu package";
            }
            QStringList arguments;
            arguments<<"-i"<<"-a"<<generaladbDriver;
            process_->start(PnPutilPath_, arguments);
            while(!process_->waitForStarted()){
                qDebug()<<"wait";
            }
            qDebug()<<"while out";
            process_->waitForReadyRead();
            qDebug()<<"start";
    //      qDebug()<<process_->readAll();
            process_->close();
        }
        else{
            qDebug()<<"sorry, your computer has no tool pnputil.exe.";
        }
    }

当我注释掉代码时

qDebug()<<process_->readAll();

并使用

 process_->setReadChannelMode(QProcess::MergedChannels);
 process_->setStandardOutputFile("E:/log.txt");

它工作正常。但如果我使用

qDebug()<<process_->readAll();
而不是

 process_->setReadChannelMode(QProcess::MergedChannels);
 process_->setStandardOutputFile("E:/log.txt");

这将是错误的。是什么原因?

1 个答案:

答案 0 :(得分:1)

根据Qt文档,必须在setReadChannelMode生效之前调用setStandardOutputFileQProcess::start,因此替换

qDebug() << process_->readAll();

process_->setReadChannelMode(QProcess::MergedChannels);
process_->setStandardOutputFile("E:/log.txt");

与仅发表评论qDebug() << process_->readAll();相同。

所以我猜这个子进程没有输出任何东西,所以process_->readAll()将阻塞,程序停止。

(你看一下E:/log.txt有没有内容,估计是process_->readAll()阻塞了)