• 8548阅读
  • 1回复

关于QProcess的管道 [复制链接]

上一主题 下一主题
离线ttheaven
 
只看楼主 倒序阅读 楼主  发表于: 2009-05-04
自己VS2008编译的4.5.1版本,感觉极其的不准啊,有时候能读出输出,有时候不能,还是我代码有问题?
ps:adb.exe是Android SDK里的东东,命令行下使用正常。

  1. #ifndef HIADBWRAPPER_H
  2. #define HIADBWRAPPER_H
  3. #include <QProcess>
  4. #include <QByteArray>
  5. #include <QString>
  6. #include <QStringList>
  7. class HiAdbWrapper: public QObject
  8. {
  9.     Q_OBJECT
  10. public:
  11.     HiAdbWrapper(QObject* parent = 0);
  12.     virtual ~HiAdbWrapper();
  13. public:
  14.     QStringList devices();
  15.     QString runCommand(const QString& device, const QString& command);
  16.     bool installApp(const QString& device, const QString& path);
  17.     bool removeApp(const QString& device, const QString& package);
  18.     bool downloadFile(const QString& device, const QString& remote, const QString& local);
  19.     bool uploadFile(const QString& device, const QString& local, const QString& remote);
  20.    
  21. protected slots:
  22.     void readStdOut();
  23.     void readStdErr();
  24. private:
  25.     QProcess* theProcess;
  26.     QString stdOut;
  27.     QString stdErr;
  28. };
  29. #endif


  1. #include "hiadbwrapper.h"
  2. HiAdbWrapper::HiAdbWrapper(QObject* parent): QObject(parent)
  3. {
  4.     theProcess = new QProcess(this);
  5.     connect(theProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut()));
  6.     connect(theProcess, SIGNAL(readyReadStandardError()), this, SLOT(readStdErr()));
  7. }
  8. HiAdbWrapper::~HiAdbWrapper()
  9. {
  10. }
  11. QStringList HiAdbWrapper::devices()
  12. {
  13.     stdOut = QString::null;
  14.     stdErr = QString::null;
  15.     QStringList args;
  16.     QStringList deviceList;
  17.     args<<"devices";
  18.     theProcess->start(QString("res\\bin\\adb.exe"), args);
  19.     theProcess->waitForFinished();
  20.     if(stdErr != QString::null)
  21.         return deviceList;
  22.     deviceList = stdOut.split(QChar('\n'));
  23.     deviceList.removeAt(0);
  24.     for(int i = deviceList.count()-1; i >= 0; i--)
  25.     {
  26.         deviceList[i].replace(QRegExp("\t.+[    wind_phpcode_2    ]quot;), QString::null);
  27.         deviceList[i] = deviceList[i].trimmed();
  28.         if(deviceList[i].isEmpty())
  29.             deviceList.removeAt(i);
  30.     }
  31.     return deviceList;
  32. }
  33. QString HiAdbWrapper::runCommand(const QString& device, const QString& command)
  34. {
  35.     stdOut = QString::null;
  36.     stdErr = QString::null;
  37.     QStringList args;
  38.     args<<"-s"<<device<<"shell"<<command;
  39.     theProcess->start(QString("res\\bin\\adb.exe"), args);
  40.     theProcess->waitForFinished();
  41.     if(stdErr != QString::null)
  42.         return stdErr;
  43.     else
  44.         return stdOut;
  45. }
  46. bool HiAdbWrapper::installApp(const QString& device, const QString& path)
  47. {
  48.     stdOut = QString::null;
  49.     stdErr = QString::null;
  50.     QStringList args;
  51.     args<<"-s"<<device<<"install"<<"-r"<<path;
  52.     theProcess->start(QString("res\\bin\\adb.exe"), args);
  53.     theProcess->waitForFinished();
  54.     if(stdErr != QString::null)
  55.         return false;
  56.     else
  57.     {
  58.         if(stdOut.indexOf(QString("Success")) != -1)
  59.             return true;
  60.         else
  61.             return false;
  62.     }
  63. }
  64. bool HiAdbWrapper::removeApp(const QString& device, const QString& package)
  65. {
  66.     stdOut = QString::null;
  67.     stdErr = QString::null;
  68.     QStringList args;
  69.     args<<"-s"<<device<<"uninstall"<<package;
  70.     theProcess->start(QString("res\\bin\\adb.exe"), args);
  71.     theProcess->waitForFinished();
  72.     if(stdErr != QString::null)
  73.         return false;
  74.     else
  75.     {
  76.         if(stdOut.indexOf(QString("Success")) != -1)
  77.             return true;
  78.         else
  79.             return false;
  80.     }
  81. }
  82. bool HiAdbWrapper::downloadFile(const QString& device, const QString& remote, const QString& local)
  83. {
  84.     stdOut = QString::null;
  85.     stdErr = QString::null;
  86.     QStringList args;
  87.     args<<"-s"<<device<<"pull"<<remote<<local;
  88.     theProcess->start(QString("res\\bin\\adb.exe"), args);
  89.     theProcess->waitForFinished();
  90.     if((stdOut.indexOf(QString("KB/s")) != -1) || (stdErr.indexOf(QString("KB/s")) != -1))
  91.         return true;
  92.     else
  93.         return false;
  94. }
  95. bool HiAdbWrapper::uploadFile(const QString& device, const QString& local, const QString& remote)
  96. {
  97.     stdOut = QString::null;
  98.     stdErr = QString::null;
  99.     QStringList args;
  100.     args<<"-s"<<device<<"push"<<local<<remote;
  101.     theProcess->start(QString("res\\bin\\adb.exe"), args);
  102.     theProcess->waitForFinished();
  103.     if((stdOut.indexOf(QString("KB/s")) != -1) || (stdErr.indexOf(QString("KB/s")) != -1))
  104.         return true;
  105.     else
  106.         return false;
  107. }
  108. void HiAdbWrapper::readStdOut()
  109. {
  110.     stdOut += theProcess->readAllStandardOutput();
  111. }
  112. void HiAdbWrapper::readStdErr()
  113. {
  114.     stdErr += theProcess->readAllStandardError();
  115. }

离线ttheaven
只看该作者 1楼 发表于: 2009-05-04
自己解决了,windows的管道问题,进程退出了再读就好了。
快速回复
限100 字节
 
上一个 下一个