Qt for beginners Exercise 1 basis
[toc align_right="yes" depth="3"]
Qt for beginners — Exercise 1 : basis
<<< Signals and slots 2 | Summary | Finding information in the documentation >>>
Note: Unfortunately, the images are no longer available. See the official "Getting Started with Qt Widgets":http://doc.qt.io/qt-5/gettingstartedqt.html page for an alternative tutorial.
Widgets
Radio button is a standard GUI component. It is often used to make a unique choice from a list. In Qt, the Doc:QRadioButton is used to create radio buttons.
Thanks to a nice heritance, a QRadioButton just behaves like a QPushButton. All properties of the QPushButton are also the same in the QRadioButton, and everything that was learnt in the second chapter can be reused here
* text
* icon
* tooltip
* …
By default, QRadioButtons are not grouped, so many of them can be checked at the same time. In order to have the "exclusive" behaviour of many radio buttons, we need to use Doc:QButtonGroup. This class can be used like this
<br />// We allocate a new button group,<br />// and attached it to the parent object<br />// note that the parent object might be<br />// the main window, or "this&quot;<br />QButtonGroup '''buttonGroup = new QButtonGroup(object);
<br />// Add buttons in the button group<br />buttonGroup->addButton(button1);<br />buttonGroup->addButton(button2);<br />buttonGroup->addButton(button3);<br />…<br />
What we want is to create a menu picker. In a window, a list of yummy plates should be displayed with radio buttons, and a push button that is used to select the chosen plate should be displayed.
Obviously, nothing will happen (now) when the buttons are clicked.
menu chooser
h2. Signals and slots
Here is an example about signals and slots. We are going to write an application with two buttons. The first button should display information about Qt, like in the following dialog
About Qt
We provide you the following code to complete :
<br />#include <QApplication&gt;<br />#include <QPushButton&gt;
<br />int main(int argc, char'''*argv)<br />{<br /> QApplication app (argc, argv);
QWidget window;<br /> window.setFixedSize(100, 80);
QPushButton *buttonInfo = new QPushButton("Info&quot;, &window);<br /> buttonInfo->setGeometry(10, 10, 80, 30);
QPushButton *buttonQuit = new QPushButton("Quit&quot;, &window);<br /> buttonQuit->setGeometry(10, 40, 80, 30);
window.show();
// Add your code here
return app.exec&amp;#40;&#41;;<br />}<br />
In order to display the information about Qt, you should use the following method
<br />void QApplication::aboutQt();<br />
You can also add icons on the buttons, or resize them. Obviously, the "Quit" button should be more important, so why not make it bigger ?
The answers can be found here. But we really recommend you to try and figure it out by yourself how to solve these exercises.