Pages

A button to quit

Here we are just getting started with Qt, so this post is not very clear. Is just a first look to how to write a button to close our application. It is not a complicated issue, but it is not such an easy task to be clarified in few lines.

The complication is in the fact that we should create a connection between the "signal" that is generated by the button when clicked and the "slot" (actually, a function) that we want to execute.

To to that we use a static method of the QObject class, connect(), and a couple of macros, SIGNAL and SLOT.

So, here is the code for our little application, a button that fill all our tiny window and that, when clicked, close it.

#include <QtGui/QApplication>
#include <QtGui/QPushButton>

int b102(int argc, char** argv)
{
QApplication app(argc, argv);
QPushButton* button = new QPushButton("Quit");
QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
button->show();
return app.exec();
}

In our case, when the button is clicked - or the blank key is pressed - the quit() method of our QApplication is called.

I wrote this post as homework while reading "C++ GUI Programming with Qt 4, Second Edition" by Jasmin Blanchette and Mark Summerfield.

No comments:

Post a Comment