Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to quit Application immedialy after QMessageBox?
Forum Updated to NodeBB v4.3 + New Features

How to quit Application immedialy after QMessageBox?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 359 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    king558
    wrote on last edited by
    #1

    I would like detect current running Windows Version, if it is Windows Server then close application immedialy, qApp->exit(); does not seem have effect here.

    #include "mainwindow.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[]) {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    
    #include "mainwindow.h"
    #include <QMessageBox>
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        if ( QSysInfo::prettyProductName().contains( "Server" ) ) {
            QMessageBox::information( Q_NULLPTR, "Test Mode", "It is : " + QSysInfo::prettyProductName() );      // Windows 10
            qApp->exit();
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    M Chris KawaC 2 Replies Last reply
    0
    • K king558

      I would like detect current running Windows Version, if it is Windows Server then close application immedialy, qApp->exit(); does not seem have effect here.

      #include "mainwindow.h"
      
      #include <QApplication>
      
      int main(int argc, char *argv[]) {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
      };
      #endif // MAINWINDOW_H
      
      #include "mainwindow.h"
      #include <QMessageBox>
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          if ( QSysInfo::prettyProductName().contains( "Server" ) ) {
              QMessageBox::information( Q_NULLPTR, "Test Mode", "It is : " + QSysInfo::prettyProductName() );      // Windows 10
              qApp->exit();
          }
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @king558
      You try to quit while your app even doesn't start.
      try instead:

      QMetaObject::invokeMethod(qApp, "quit",Qt::QueuedConnection);
      
      K 1 Reply Last reply
      0
      • K king558

        I would like detect current running Windows Version, if it is Windows Server then close application immedialy, qApp->exit(); does not seem have effect here.

        #include "mainwindow.h"
        
        #include <QApplication>
        
        int main(int argc, char *argv[]) {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
            return a.exec();
        }
        
        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        
        QT_BEGIN_NAMESPACE
        namespace Ui { class MainWindow; }
        QT_END_NAMESPACE
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
        private:
            Ui::MainWindow *ui;
        };
        #endif // MAINWINDOW_H
        
        #include "mainwindow.h"
        #include <QMessageBox>
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            if ( QSysInfo::prettyProductName().contains( "Server" ) ) {
                QMessageBox::information( Q_NULLPTR, "Test Mode", "It is : " + QSysInfo::prettyProductName() );      // Windows 10
                qApp->exit();
            }
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        Chris KawaC Online
        Chris KawaC Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Or just don't put that check code in the main window constructor and structure your app better. It doesn't look like it has anything to do with that window, so why is it in it, i.e.

        int main(int argc, char *argv[]) {
            QApplication a(argc, argv);
        
            if ( QSysInfo::prettyProductName().contains( "Server" ) ) {
                QMessageBox::information( Q_NULLPTR, "Test Mode", "It is : " + QSysInfo::prettyProductName() );      // Windows 10
                return 0;
            }
        
            MainWindow w;
            w.show();
            return a.exec();
        }
        
        K 1 Reply Last reply
        3
        • M mpergand

          @king558
          You try to quit while your app even doesn't start.
          try instead:

          QMetaObject::invokeMethod(qApp, "quit",Qt::QueuedConnection);
          
          K Offline
          K Offline
          king558
          wrote on last edited by
          #4

          @mpergand

          thx, it works good.

          1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            Or just don't put that check code in the main window constructor and structure your app better. It doesn't look like it has anything to do with that window, so why is it in it, i.e.

            int main(int argc, char *argv[]) {
                QApplication a(argc, argv);
            
                if ( QSysInfo::prettyProductName().contains( "Server" ) ) {
                    QMessageBox::information( Q_NULLPTR, "Test Mode", "It is : " + QSysInfo::prettyProductName() );      // Windows 10
                    return 0;
                }
            
                MainWindow w;
                w.show();
                return a.exec();
            }
            
            K Offline
            K Offline
            king558
            wrote on last edited by
            #5

            @Chris-Kawa

            Nice tipps, thx, I just began this way, forgot to put QApplication to first line

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved