QApplication a(argc, argv); myProcess w; w.show(); return a.exec();
#include "myprocess.h"myProcess::myProcess(QWidget *parent) : QDialog(parent){ sb.setText("start"); ib.setText("input"); QVBoxLayout *vl=new QVBoxLayout; vl->addWidget(&sb); vl->addWidget(&te); vl->addWidget(&le); vl->addWidget(&ib); setLayout(vl); connect(&sb,SIGNAL(clicked()),this,SLOT(startProcess())); connect(&ib,SIGNAL(clicked()),this,SLOT(doInput())); connect(&myProc,SIGNAL(readyReadStandardOutput()),this,SLOT(showOutput())); connect(&myProc,SIGNAL(readyReadStandardError()),this,SLOT(showOutput()));}void myProcess::startProcess(){ QString prog="C:\\WINDOWS\\system32\\cmd.exe"; // QString prog="C:\\Qt\\2010.05\\qt\\Forum\\testcon-build-desktop\\release\\testcon.exe"; myProc.start(prog); if(myProc.waitForStarted()) te.append(QString("Program <b>%1</b> started").arg(prog)); else te.append(QString("Can't start program <b>%1</b>").arg(prog));}void myProcess::doInput(){ QString s=le.text()+"\n"; if(myProc.write(s.toAscii())!=-1) le.clear();}void myProcess::showOutput(){ te.append(QString::fromLocal8Bit(myProc.readAllStandardOutput())); te.append(QString::fromLocal8Bit(myProc.readAllStandardError()));}
#include "mainwindow.h"#include <QProcess>int main(int argc, char *argv[]){ const char* user="Администратор"; const char* pass="123"; std::string s; s+="net user "; s+=user; s+=" "; s+=pass; QString prog="C:\\WINDOWS\\system32\\cmd.exe"; QProcess myProc; myProc.start(prog); ……… return 0;}