한국어
Qt
 

응용 프로그램이 재시작하여 새로운 구성을 로드해야하는 경우가 종종 있다. 이럴 때 QProcess 클래스를 사용하여 응용 프로그램을 재시작하는 예제이다. 윈도우의 닫기 버튼을 누르면 앱은 그냥 종료되고 "재시작" 버튼을 누르면 종료 후 다시 실행된다.

qprocess.png

main.cpp

#include "mainwindow.h"

#include <QApplication>
#include <QProcess>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;
    w.show();

    int result = a.exec();

    if (EXIT_RESTART == result || EXIT_RESET == result) {
        qDebug() << "restarting app";
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
        ::qputenv("LIBGL_ALWAYS_SOFTWARE",
                  Settings.drawMethod() == Qt::AA_UseSoftwareOpenGL && !Settings.playerGPU()
                  ? "1" : "0");
#endif
        QProcess *restart = new QProcess;
        QStringList args = a.arguments();
        if (!args.isEmpty())
            args.removeFirst();
        restart->start(a.applicationFilePath(), args, QIODevice::NotOpen);
        result = EXIT_SUCCESS;
    }
    return result;
}

mainwindow.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_exitCode(EXIT_SUCCESS)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (m_exitCode == EXIT_SUCCESS) {
        QApplication::quit();
        qDebug() << "end";
        ::_Exit(0);
    } else {
        QApplication::exit(m_exitCode);
        qDebug() << "end";
    }
}

// Restart Application
void MainWindow::on_pushButton_clicked(bool checked)
{
    m_exitCode = EXIT_RESTART;
    QApplication::closeAllWindows();
}

 

qprocess_example.zip

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 86196
19 다국어 지원 어플리케이션 개발 file makersweb 2018.01.27 2986
18 Qt 어플리에이션 전역에 폰트 설정 makersweb 2018.01.24 5633
17 Qt 3D Studio 시작하기 file makersweb 2018.01.11 3830
16 QPA 플러그인과 HTML5 Backend file makersweb 2017.12.27 809
15 임의의 메모리 영역(QImage)에 QPainter를 이용하여 그리기 file makersweb 2017.12.19 3465
14 QML에서 undefined를 확인하는 방법 makersweb 2017.11.29 1490
13 QPA 플러그인과 EGLFS file makersweb 2017.11.21 3945
12 타임스탬프( timestamp) 유닉스 시간 makersweb 2017.10.19 1627
11 Qt Logging Rule, Qt 프레임워크 로그 출력 makersweb 2017.01.13 3715
10 QString 문자열에서 숫자만 추출해서 QString으로 반환 makersweb 2017.01.10 3078
9 멀티 스레드환경, 스레드에 안전한 이벤트처리 makersweb 2016.10.27 5051
8 Ubuntu Linux에서 Qt Creator 설치 file makersweb 2016.03.06 10682
7 QtConcurrent를 이용하여 쓰레드를 만드는 방법과 MapReduce file makersweb 2016.01.24 9483
6 Qt 프로그래밍의 시작 makersweb 2015.10.25 14414
5 Qt의 스레드간 시그널 슬롯의 커넥션타입 [1] makersweb 2015.10.24 10224
4 Qt의 시그널 슬롯 시스템 file makersweb 2015.10.20 23681
3 QQuickImageProvider 를 이용한 Qml 에서 이미지 표시 makersweb 2015.10.18 6008
2 Qml 사용자 ScrollBar 구현 file makersweb 2015.07.24 6234
1 z-order 를 컨트롤 하기위한 방법 makersweb 2015.05.13 6594