QT QProcess -1- readAll()无法完整读取所有信息的问题 <备忘>
QT QProcess -1- readAll()无法完整读取所有信息的问题
近来试了试QProcess这东西,遇到了一个目前无法解决的问题,现记录下来,备忘。
本想写了个小程序来对sdb进行操作,主要是无聊想给sdb换个界面,主要代码如下:
QString CSdbView::SendCmdToSdb(QString string)
{
QString cmd = ltxtedtSdbCmdInput->text();
if(cmd.isEmpty())
{
qDebug()<<"cmd.isEmpty()"<<string;
myProcess->start(DEFAULT_SDB_PATH);//, cmdiit);
return "INIT";
}
QString cmdInput = QString("%1 %2").arg(programPath).arg(cmd);
myProcess->start(cmdInput);
return "OK";
}
其中
#define DEFAULT_SDB_PATH "E:/Tizen/tizen-sdk/tools/sdb.exe"
因为遇到错误,为了方便调试,添加了以下代码:
void CSdbView::InitConnect()
{
connect(myProcess, SIGNAL(started()), this, SLOT(onStartComplete()));
connect(myProcess, SIGNAL(readyRead()), this, SLOT(onReadOutput()));
connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(onReadAllStardOut()));
connect(myProcess, SIGNAL(readyReadStandardError()), this, SLOT(onReadAllStandardError()));
connect(myProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onReadErrorType(QProcess::ProcessError)));
connect(myProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinish(int, QProcess::ExitStatus)));
}
在启动调试的时候,会出现readAll()无法完整的读取myProcess的output数据的情况。
程序刚启动时,都会列出help message信息,并且应该是从readAll()这个函数里读出来的。但是实际情况却是从:
connect(myProcess, SIGNAL(readyReadStandardError()), this, SLOT(onReadAllStandardError()));
这个里面读取的。
1 cmd.isEmpty() "init"
2 CSdbView::onStartComplete()
3 CSdbView::onReadAllStandardError()
4 "Smart Development Bridge version 2.2.48
5
6 Usage : sdb [option] <command> [parameters]
7
8 options:
9 -e, --emulator - direct command to the only running emulator
10 return an error if more than one emulator is running
11 -d, --device - direct command to the only connected USB device
12 -s, --serial <serial_number> - direct command to the USB device or emulator with the given serial number
13
14 commands:
15 sdb root <on | off> - switch to root or developer account mode
16 'on' means to root mode, and vice versa
17 sdb status-window - continuously print device status for a specified device
18 sdb get-serialno - print: <serial-number>
19 sdb get-state - print: offline | locked | device
20 sdb kill-server - kill the server if it is running
21 sdb start-server - ensure that there is a server running
22 sdb version - show TçÅACSdbView::onReadAllStandardError()
23 "ersion num
24 sdb help - show this help message
25 sdb forward <local> <remote> - forward socket connections
26 For example: sdb forward tcp:9999 tcp:9999
27 sdb uninstall <pkg_id> - uninstall an app from the device
28 the <pkg_id> is an unique 10-digit unique identifier for the application. The following command shows an example:
29 Ex.) sdb uninstall ko983dw33q
30 sdb install <pkg_path> - push package file and install it
31 sdb dlog [<filter_spec>] - view device log
32 sdb shell [command] - if argument is null, run remote shell interactively
33 if argument is not null, run command in the remote shell
34 sdb pull <remote> [<local>] - copy file/dir from device
35 sdb push <local> <remote> [--with-utf8]
36 - copy file/dir to device
37 (--with-utf8 means to create the remTçÅ
注意Line3和Line22,显示这段输出是onReadAllStandardError()里输出的。
当然重新输入version这个参数的时候,显示却是这样的:
CSdbView::onStartComplete()
CSdbView::onReadOutput()
Smart Development Bridge version 2.2.48
根据红色部分的Log显示,这个确实是正确的从 SLOT(onReadOutput())里输出的信息。
为了解决心中的疑问,就再次输入help这个参数看看输出的效果,结果和程序刚启动时的输出情况类似,还是从onReadAllStandardError()里输出的log信息。
这时只能怀疑是QProcess一次读取的数据长度有限制,可是找不到设置读取长度的地方。
花了两个晚上的时候,试过QT帮助文档里QProcess的各种接口,还是无法实现完整正确的读出sdb.exe返回的信息,于是这个问题就只能搁浅了。
后来又试了另一个启动myProcess的接口excute,
QString CSdbView::SendCmdToSdb(QString string)
{
QString cmd = ltxtedtSdbCmdInput->text();
if(cmd.isEmpty())
{
qDebug()<<"cmd.isEmpty()"<<string;
myProcess->execute(DEFAULT_SDB_PATH);//, cmdiit);
return "INIT";
}
QString cmdInput = QString("%1 %2").arg(programPath).arg(cmd);
myProcess->start(cmdInput);
return "OK";
}
此时程序启动的时候,在qt creator的[应用程序输出]里却能看到完整的sdb help信息。
但是start(...)这个启动myProcess的方式就是用来满足可以从调用的程序里读到返回消息的,execute(...)这个api一般用来阻塞式启动新程序的。
在这里就遇到矛盾点了,可以读取数据的启动方式start(...)无法完整的得到返回的数据,而execute(...)的启动方式却无法响应
connect(myProcess, SIGNAL(readyRead()), this, SLOT(onReadOutput()));
connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(onReadAllStardOut()));
这两个中的任何一个。
此问题正式搁浅,在此记下,有路过的大神有空的话帮帮忙。我也打算周末了再试试android的adb工具是不是也这样。
浙公网安备 33010602011771号