gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:31:45 24 4
gpt4 key购买 nike

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

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

会出错。这是什么原因?

最佳答案

根据Qt文档,setReadChannelModesetStandardOutputFile必须在 QProcess::start 之前调用生效,所以更换

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

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

等同于注释掉 qDebug() << process_->readAll(); .

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

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

关于c++ - 使用 QProcess->setReadChannelMode(QProcess::MergedChannels) 并使用 QProcess->readall(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35433517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com