Adding a button
The next customization is to add a button to our application. The most common
button used in any GUI is a push or command button. We push (click on) the button
to command the computer to perform some action or answer a decision. Typical
push buttons include OK, Apply, Cancel, Close, Yes, No, and Help. Usually the
push button is rectangular with some label on it. It can also have an optional icon
associated with it. It is also possible to define a shortcut key combination to the
button to which it responds on clicking the key combination.
The button emits a signal when it is activated by any external event, say, mouse
click or by pressing the spacebar or by a keyboard shortcut. A widget may be
associated with this key click event which is executed on receiving this signal and it
is usually called a slot, in Qt. We will learn more about signals and slots in the later
chapters. As for now, be informed that a signal will connect to a slot on emit. There
can also be other signals that are provided on a button like button pressed, button
released, button checked, button down, button enabled, and so on. Apart from a
push button we also have other button types in Qt like QToolButton, QRadioButton,
QCommandLinkButton and QCheckBox will be discussed later.
QPushButton can be instantiated in three ways. It has three constructors with
different signatures. They are:
QPushButton(icon, text, [parent=None])
The parent parameter can be any widget, text is any string or a set of unicode
characters, and icon is a valid QIcon object.
In this example program, we are going to add a button that will close the
application when clicked. We define a button first and will call a function (slot)
when clicked (signal).
""" Function to add a quit button
myButton = QPushButton('Quit', self)