I know that to use the Qt mechanism in the class of signals and slots inside the class, the class must include the Q_OBJECT macro, but I'm trying to use the signals and slots in main() without using any class.
Here is my code:
#include <QApplication> #include <QWidget> #include <QTextEdit> #include <QtGui> void saveText(); int main(int argv, char **args) { QApplication app(argv, args); QTextEdit textEdit; QPushButton saveButton("Save!"); QPushButton exitButton("Exit!"); QObject::connect(&exitButton,SIGNAL(clicked()),qApp,SLOT(quit())); QObject::connect(&saveButton,SIGNAL(clicked()),qApp,SLOT(saveText())); QVBoxLayout vlyt; vlyt.addWidget(&textEdit); vlyt.addWidget(&exitButton); vlyt.addWidget(&saveButton); QWidget mainWindow; mainWindow.setLayout(&vlyt); mainWindow.show(); return app.exec(); } void saveText() { exit(0); }
Here is the window generated by the graphical interface:

From the above code, the exit button is connected to quit() , which is a Qt function when it is pressed. The save button assigned to saveText() is configured to exit, but does not.
Please tell me where I made a mistake in understanding signals and slots in Qt.
source share