한국어
Qt
 

C++ Class QProcess 예제 (프로그램 재시작)

makersweb 2023.01.25 15:57 조회 수 : 1067

응용 프로그램이 재시작하여 새로운 구성을 로드해야하는 경우가 종종 있다. 이럴 때 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 86898
139 QML에서 Websocket 서버와 통신 file makersweb 2021.09.18 898
138 Qt 6의 C++ 프로퍼티 바인딩 예제 makersweb 2021.11.01 904
137 QML에서 D-Bus 통신 file makersweb 2023.03.15 914
136 Qt로 작성된 안드로이드 APP에 Splash Screen을 추가 file makersweb 2020.03.10 931
135 Qt 응용프로그램에서 Lottie Animation사용 file makersweb 2021.05.30 938
134 QML 에서 QR코드 생성 file makersweb 2021.08.20 941
133 Qt로 작성된 iOS 앱에서 시리얼 통신 file makersweb 2022.04.30 946
132 QML 바인딩 끊김 진단 makersweb 2020.11.08 959
131 Qt 5.15 및 Qt 6의 출시 일정 makersweb 2020.04.09 965
130 Qt 5 코드를 Qt 6로 포팅하기 위해 도움이 되는 Clazy Framework file makersweb 2021.03.01 987
129 Qt기반의 서버와 클라이언트간 SOAP(Simple Object Access Protocol) file makersweb 2020.05.11 1012
128 Qt 6.0의 개발 호스트 및 대상 플랫폼 makersweb 2020.09.16 1014
127 Qt MQTT 에 대해서 file makersweb 2020.06.02 1014
126 [Qt] Google Play의 향후 요구 사항을 준수하는 방법 [2] j2doll 2019.07.29 1017
125 Qt로 XML 파싱 : Qt 6에서 업데이트된 (Parsing XML with Qt: Updates for Qt 6) [1] j2doll 2020.01.16 1019
124 Qt 6에서 QList 변경사항 makersweb 2020.10.08 1051
» QProcess 예제 (프로그램 재시작) file makersweb 2023.01.25 1067
122 QScopedPointer 소개 및 사용법 makersweb 2019.11.29 1074
121 QOpenGLWidget 을 투명하게 적용 file makersweb 2020.02.05 1108
120 QML 전역 객체 (Global Object) file makersweb 2019.04.10 1116