Calling the quit () QApplication Method

if I try to use the quit () method directly, it compiles fine, but at runtime the message "Object :: connect: There is no such slot myClass :: quit ()" appears. so is there any way to avoid this? using the quitPicture () method (defined as a slot), the application works fine. is this the only solution?

myClass::myClass(QWidget *parent)
    : QWidget(parent)
{
    QWidget *window = new QWidget;
    window->setWindowTitle(QObject::tr("Class"));

    QPushButton *quitButton = new QPushButton("&Quit");
//    QObject::connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));      //showing run time error
    QObject::connect(quitButton, SIGNAL(clicked()), this, SLOT(quitPicture())); //working perfectly

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(this);
    layout->addWidget(quitButton);
    window->setLayout(layout);
    window->show();
}

void myClass::quitPicture()
{
    std::cout << "calling quitPicture" << std::endl;
    QApplication::quit();
}
+5
source share
3 answers

The button signal clickedcan be connected directly to the application slot quit:

QObject::connect(quitButton, SIGNAL(clicked()),
                 QApplication::instance(), SLOT(quit()));
+11
source

This answer covers the new signal / slot syntax in Qt and further describes how to handle it when using a signal that uses overloads.

QObject :

QObject obj(nullptr); 

QObject::connect(&obj, &QObject::destroyed, QCoreApplication::instance(), \
 &QCoreApplication::quit);

QProcess :

QProcess * process = new QProcess(QCoreApplication::instance());

QObject::connect(process, static_cast<void (QProcess::*)(int)>(&QProcess::finished), \
                     QCoreApplication::instance(), &QCoreApplication::quit); 

, :

static_cast< _signalReturnType_( _ObjectName::*_ )( _overloadType1_, _overloadType2_, \
…etc )>( _&ObjectName::signalName_ )

, , .

QProcess , :

QProcess * process = new QProcess(QCoreApplication::instance());

QObject::connect(process, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>( \
 &QProcess::finished ), QCoreApplication::instance(), &QCoreApplication::quit);

, . , SO , , .

0
void my_exit_func()
{
    // in mainwindow.cpp
    delete MainWindow;
}
0
source

All Articles