QT下實現對Linux Shell調用的幾種方法


 使用QProcess QThread

============================================

#include <QProcess>
int main()
{
QProcess::execute("ls");
return 0;
}

============================================

 

 

QProcess *poc = new QProcess; 
poc-> start( "ping 222.207.53.1> hh "); 
打開hh文檔 讀取里面的內容給QTextEdit

============================================

QProcess *proc = new QProcess;
proc-&gt;addArgument("qmake");
proc-&gt;addArgument("-o");
proc-&gt;addArgument("/home/sjq/work/jobs/test6/test/Makefile");
proc-&gt;addArgument("/home/sjq/work/jobs/test6/test/cmd.pro");
if(proc-&gt;start())
{
cout&lt;&lt;"error"&lt;&lt;endl;
}

其中使用方法就在於對QProcess 的調用如

mkdir TEST

QProcess *proc = new QProcess;
proc-&gt;addArgument("mkdir");
proc-&gt;addArgument("TEST");

if(proc-&gt;start())
{
cout&lt;&lt;"error"&lt;&lt;endl;
}

============================================

另外的一種使用方法,一種交換

while ( proc-> isRunning()) 

stringOut = proc-> readLineStdout(); 
textEdit-> append( stringOut ); 

這樣能讀出來,但是不知道怎么停下來,在讀的proc執行的過程中,窗口被凍結 
最后用了qApp-> processEvents()來解決的 
我再去研究研究pthread.

============================================

下面是使用Qthread的一種方法

===============================================

#include <QProcess>
#include <QThread>
class MyThread : public QThread
{
public:
void run();
};

void MyThread::run()
{
QProcess::execute("service lighttpd restart");
}

int main()
{
MyThread *thread=new MyThread;
thread->start();
}

============================================

#include <QProcess>
#include <QThread>
#include <QString>
#include <QStringList>
class MyThread : public QThread
{
public:
void run();
};

void MyThread::run()
{
QProcess *testc=new QProcess;
QString program = "rm";
QStringList arguments;
arguments << "./a.txt";
testc->start(program, arguments);
}

int main()
{
MyThread *thread=new MyThread;
thread->start();
}

細化操作--不打印顯示任何的操作結

http://blog.csdn.net/taiyang1987912/article/details/46293929


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM