QT-Process

用QProcess类执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
QProcess process;
QString cmd;

QString dir = curEditor->fileInfo->absolutePath()+"/"+curEditor->fileInfo->baseName()+".exe";
cmd = "g++ -o "+dir+" " + curEditor->file->fileName();
//qDebug()<<cmd;
process.start(cmd);
process.waitForFinished();
process.waitForReadyRead();
QByteArray qba = process.readAllStandardError();
//qDebug()<<qba;

QTextCodec * pTextCodec = QTextCodec::codecForName("System");
//assert(pTextCodec != nullptr);
QString str = pTextCodec->toUnicode(qba);

compilationInfoContainer->setPlainText(str);

直接调用命令行

  • system(cmd.toLatin1().data());