QT启动 一个进程-一个exe文件方法。
启动notepad.exe或cmd.exe
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include
- #include
-
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
-
- private slots:
- void on_pushButton_clicked();
- void showResult();
- void showState(QProcess::ProcessState);
- void showError();
- void showFinished(int,QProcess::ExitStatus);
-
-
- private:
- Ui::MainWindow *ui;
- QProcess my_Process;
- };
- #endif // MAINWINDOW_H
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include
- #include
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- connect(&my_Process,&QProcess::readyRead,this,&MainWindow::showResult);
- connect(&my_Process,&QProcess::stateChanged,this,&MainWindow::showState);
- connect(&my_Process,&QProcess::errorOccurred,this,&MainWindow::showError);
- connect(&my_Process,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(showFinished(int,QProcess::ExitStatus)));
-
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
-
- void MainWindow::on_pushButton_clicked()
- {
- // my_Process.start("notepad.exe");
- QString program ="cmd.exe";
- QStringList arguments;
- arguments<<"/c dir&pause"; // /c指定命令dir在cmd中执行
- my_Process.start(program,arguments);
- }
- void MainWindow::showState(QProcess::ProcessState state)
- {
- qDebug()<<"showState: ";
- if(state == QProcess::NotRunning)
- {
- qDebug()<<"没有程序运行";
- }
- else if(state==QProcess::Starting)
- {
- qDebug()<<"开始中";
- }
- else
- {
- qDebug()<<"运行中。。。";
- }
- }
- void MainWindow::showResult()
- {
- QTextCodec *codec=QTextCodec::codecForLocale();
- qDebug()<<"显示结果:"<
toUnicode(my_Process.readAll()); - }
- void MainWindow::showError()
- {
- qDebug()<<"显示错误信息:"<
errorString(); - }
- void MainWindow::showFinished(int exitCode,QProcess::ExitStatus exit_stattus)
- {
- qDebug()<<"显示完成:"<
"退出代码:"<",退出状态:"< - }