Qt中的進程和線程

一:啓動一個進程:多線程

(1):進程的概述:在設計應用程序時,會在改程序中啓動外部程序來配合完成工做,這時就須要啓動外部進程,
    Qt應用程序能夠很容易地啓動一個外部應用進程,更重要地是QT提供了許多地進程間地通訊地方法:
(2):進程的類(QProcess):
  1:start()啓動進程函數:將須要啓動地進程名稱和命令行參數做爲該函數地參數。執行改函數後Qprocess進入starting狀態,
   進入Running狀態時就會發送started()信號。
  2:當進程退出地時候,Qprocess進入NotRunning狀態,就會發送finished()信號。
  3:finished()信號:提供了進程地退出狀態和退出代碼,
  4:exitCode()函數:獲取一個進程退出地代碼。
  5:exitStatus()函數:獲取進程退出地狀態。
  6:error()函數:任什麼時候間發生了錯誤,Qprocess都會發送error()信號。

 

mainwindow.h函數

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess> //線程類
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public slots:
    void xianchen_clicked();//聲明自定義槽函數.
private slots:
    void on_btn1_clicked();
    void showResult();
/*當槽函數對應地信號有參數時,對應地槽函數須設參數來接收槽函數地參數,他們地參數類型須要保持一致*/
    void showstate(QProcess::ProcessState);
    void showerror();
    void showfinshed(int,QProcess::ExitStatus);
private:
    Ui::MainWindow *ui;
    QProcess myprocess;//實列化一個私有對象
};
#endif // MAINWINDOW_H

main.cppui

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec(); /*循環*/
}

mainwindow.cppthis

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QDebug>
#include <QTextCodec> /*方便處理encoding*/
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent),ui(new Ui::MainWindow)
{
    /*【注意】:這裏採用代碼地方式,沒有使用ui來建立*/
    QPushButton *btn = new QPushButton(this);/*爲何這個按鈕單擊沒有反應*/
    btn->setParent(this); //設置父對象
    btn->setText("啓動一個按鈕");
    ui->setupUi(this);
    this->setWindowTitle("啓動一個外部線程");
    connect(btn,&QPushButton::clicked,this,&MainWindow::xianchen_clicked);
    connect(&myprocess,&QProcess::readyRead,this,&MainWindow::showResult);
 /*signal:
  * QProcess::stateChanged(QProcess::ProcessState newState)*/
    connect(&myprocess,&QProcess::stateChanged,this,&MainWindow::showstate);
    connect(&myprocess,&QProcess::errorOccurred,this,&MainWindow::showerror);
/*1:對改信號和槽函數地解釋
*
*/
    connect(&myprocess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(showfinshed));
}
void MainWindow::xianchen_clicked()
{
    myprocess.start("notepad.exe");//打開一個外部線程
}
MainWindow::~MainWindow()
{
    delete ui;
}
/*enum:枚舉類型地定義,(將其做爲函數參數)
 * enum QProcess::ExitStatus
 * This enum describes the different exit statuses of QProcess.
 0 : The process exited normally.
 1 :  The process crashed.
 * QProcess::ExitStatus exitstatus :枚舉的實列
*/
void MainWindow::showfinshed(int exitCode,QProcess::ExitStatus exitstatus)//exitstatue:枚舉的實列
{
    qDebug()<<"showfinished:"<<endl<<exitCode<<exitstatus<<endl;
}
void MainWindow::showerror() /*類成員函數地定義*/
{
/*errorString()獲取錯誤信息,並將其顯示*/
    qDebug()<<"make error"<<endl<<myprocess.errorString()<<endl;
}
void MainWindow::showstate(QProcess::ProcessState newState)
{
    qDebug()<<"showstate"<<endl;
    if(newState==QProcess::NotRunning)
    {
        qDebug()<<"NotRunning"<<endl;
    }
    else if(newState==QProcess::Starting)
    {
        qDebug()<<"Starting"<<endl;
    }
    else
    {
        qDebug()<<"Running"<<endl;
    }
}
void MainWindow::showResult()
{
/*3:QTextCodec簡介:進行編碼轉換,這樣才能顯示中文或其餘語言
 *
 *
*/
    QTextCodec *codec = QTextCodec::codecForLocale();
    qDebug()<<"showResult"<<endl<<codec->toUnicode(myprocess.readAll());//readALl:讀取全部運行結果
}
void MainWindow::on_btn1_clicked() //使用GUI轉換到槽()方式建立.
{
    QString program = "cmd.exe";
/*2:QStringList:
 *
*/
     QStringList arguments;/*實列化一個對象*/
 /*&pause命令指定指令運行完後暫停*/
     arguments<<"/cdir&pause"; /*"<<"爲其類中的操做符*/
     myprocess.start(program,arguments);/*將須要啓動地進程名稱和命令行參數做爲該函數地參數*/
     myprocess.start("notepad.exe");//打開一個外部線程
}

 

二:進程間的會話:編碼

(1):進程通訊中的阻塞:QProcess提供了一組函數,能夠脫離事件循環來使用,他們會掛起調用的線程,直到肯定的信號被髮送。spa

waitForStarted() //阻塞,直到進程啓動。
waitForReadyRead()//阻塞,直到當前讀通道上有可讀的數據;
waitForBytesWritten()//阻塞,直到一個有效負載數據被寫入到進程
waitForFinished()//阻塞,直到進程結束。

三:多線程:命令行

相關文章
相關標籤/搜索