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 a QApplication through class private slot
Forum Updated to NodeBB v4.3 + New Features

How to quit a QApplication through class private slot

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.1k Views 1 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.
  • E Offline
    E Offline
    exSnake
    wrote on last edited by exSnake
    #1

    I'll go fast to the point, i have followed this italian tutorial, but seem to be so broken, he said to me to create a quit slot for the notepad for let the user choice if they really want to close it or not, but how i can say that the Quit() slot have to close the QApplication created in the main??? I've tried to implement it to use "this->quit()" but whenever i press it reopen again.
    this is the code:

    Main.cpp

    #include <QApplication>
    #include "notepad.h"
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Notepad w;
        w.show();
    
    
        return a.exec();
    }
    
    
    

    Notepad.h

    #ifndef NOTEPAD_H
    #define NOTEPAD_H
    #include <QApplication>
    #include <QtWidgets>
    #include <QWidget>
    
    
    class Notepad : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Notepad(QWidget *parent = 0);
        ~Notepad();
    
    private slots:
        void quit();
    
    private:
        QTextEdit *textEdit;
        QPushButton *btnQuit;
    };
    
    
    #endif // NOTEPAD_H
    
    

    Notepad.cpp

    #include "notepad.h"
    
    Notepad::Notepad(QWidget *parent) : QWidget(parent)
    {
        textEdit = new QTextEdit;
        btnQuit = new QPushButton(tr("esci"));
    
        connect(btnQuit,SIGNAL(clicked(bool)),this,SLOT(quit()));
    
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(textEdit);
        layout->addWidget(btnQuit);
        setLayout(layout);
        setWindowTitle(tr("Notepad"));
    }
    
    Notepad::~Notepad()
    {
    
    }
    
    void Notepad::quit(){
        QMessageBox *message = new QMessageBox();
        message->setText("Are you sure?");
        message->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        int ret = message->exec();
        switch(ret){
            case QMessageBox::Yes:
                quit();
                break;
            case QMessageBox::No:
                break;
        default:
            break;
        }
    }
    
    
    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, when you're calling quit(); you're just calling your own slot function again (i.e. same as clicking the "Esci" button) try change to:

          case QMessageBox::Yes:
              qApp->quit();
              break;
      1 Reply Last reply
      0
      • E Offline
        E Offline
        exSnake
        wrote on last edited by
        #3

        So qApp is a pointer to the QApplication "instance" ? Is it right?

        I can use it in every class constructor for refer to the QApplication object?

        1 Reply Last reply
        0
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on last edited by
          #4

          Yes and yes :-)

          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