user2919227
user2919227

Reputation: 41

using QProcess->setReadChannelMode(QProcess::MergedChannels) and using QProcess->readall()

It's my first time to ask question on stackoverflow. And I'm a chinese girl, if my description about this problem has so much grammar error that you can't understand it easyly, I'm so sorry. Below is my question: 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");
}

sourcefile:

 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.";
        }
    }

when i Commented out code

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

and use

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

it works properly.But if i use

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

instead of

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

it will be wrong. what's the reason?

Upvotes: 1

Views: 708

Answers (1)

Ziming Song
Ziming Song

Reputation: 1346

According to Qt document, both setReadChannelMode and setStandardOutputFile have to be called before QProcess::start to take effect, so replacing

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

with

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

is same as just commenting out qDebug() << process_->readAll();.

So I guess the child process does not output anything, so process_->readAll() will block, and the program halts.

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

Upvotes: 1

Related Questions