How to quit a QApplication through class private slot
-
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; } }
-
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;
-
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?
-
Yes and yes :-)