Qt for beginners Exercise 1 basis: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]<br />[[Category:Qt_for_beginners]]<br />[[Category:Tutorial]]<br />[[Category:HowTo]]
[toc align_right="yes" depth="3"]
[[Category:Qt_for_beginners]]
[[Category:Tutorial]]
[[Category:HowTo]]


= Qt for beginners — Exercise 1 : basis =
= Qt for beginners — Exercise 1 : basis =


[[Qt_for_beginners_Signals_and_slots_2|&lt;&lt;&lt; Signals and slots 2]] | [[Qt_for_beginners|Summary]] | [[Qt_for_beginners_Doc|Finding information in the documentation &gt;&gt;&gt;]]
[[Qt_for_beginners_Signals_and_slots_2|<<< Signals and slots 2]] | [[Qt_for_beginners|Summary]] | [[Qt_for_beginners_Doc|Finding information in the documentation >>>]]


'''''Note:''' Unfortunately, the images are no longer available. See the official &quot;Getting Started with Qt Widgets&amp;quot;:http://doc.qt.io/qt-5/gettingstartedqt.html page for an alternative tutorial.''
'''''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 ==
== Widgets ==
Line 11: Line 14:
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.
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<br />* text<br />* icon<br />* tooltip<br />* …
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 &quot;exclusive&amp;quot; behaviour of many radio buttons, we need to use [[Doc:QButtonGroup]]. This class can be used like this
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


<code><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 &quot;this&amp;quot;<br />QButtonGroup '''buttonGroup = new QButtonGroup(object);
<code>
<br />// Add buttons in the button group<br />buttonGroup-&gt;addButton(button1);<br />buttonGroup-&gt;addButton(button2);<br />buttonGroup-&gt;addButton(button3);<br />…<br /></code>
// We allocate a new button group,
<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.
// and attached it to the parent object
<br />Obviously, nothing will happen (now) when the buttons are clicked.
// note that the parent object might be
<br />[[Image:http://farm8.staticflickr.com/7109/7514416312_880b14d647.jpg|menu chooser]]
// the main window, or "this"
<br />h2. Signals and slots
QButtonGroup '''buttonGroup = new QButtonGroup(object);
<br />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
<br />[[Image:http://farm8.staticflickr.com/7126/7503975304_5d67868890_b.jpg|About Qt]]
<br />We provide you the following code to complete :
<br /><code><br />#include &lt;QApplication&amp;gt;<br />#include &lt;QPushButton&amp;gt;
<br />int main(int argc, char'''*argv)<br />{<br /> QApplication app (argc, argv);


QWidget window;<br /> window.setFixedSize(100, 80);
// Add buttons in the button group
buttonGroup->addButton(button1);
buttonGroup->addButton(button2);
buttonGroup->addButton(button3);
</code>


QPushButton *buttonInfo = new QPushButton(&quot;Info&amp;quot;, &amp;window);<br /> buttonInfo-&gt;setGeometry(10, 10, 80, 30);
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.


QPushButton *buttonQuit = new QPushButton(&quot;Quit&amp;quot;, &amp;window);<br /> buttonQuit-&gt;setGeometry(10, 40, 80, 30);
Obviously, nothing will happen (now) when the buttons are clicked.
 
[[Image:http://farm8.staticflickr.com/7109/7514416312_880b14d647.jpg|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
 
[[Image:http://farm8.staticflickr.com/7126/7503975304_5d67868890_b.jpg|About Qt]]
 
We provide you the following code to complete :
 
<code>
#include <QApplication>
#include <QPushButton>
 
int main(int argc, char'''*argv)
{
QApplication app (argc, argv);
 
QWidget window;
window.setFixedSize(100, 80);
 
QPushButton *buttonInfo = new QPushButton("Info", &amp;window);
buttonInfo->setGeometry(10, 10, 80, 30);
 
QPushButton *buttonQuit = new QPushButton("Quit", &amp;window);
buttonQuit->setGeometry(10, 40, 80, 30);


window.show();
window.show();
Line 37: Line 71:
// Add your code here
// Add your code here


return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
return app.exec();
}
</code>


In order to display the information about Qt, you should use the following method
In order to display the information about Qt, you should use the following method


<code><br />void QApplication::aboutQt();<br /></code>
<code>
void QApplication::aboutQt();
</code>


You can also add icons on the buttons, or resize them. Obviously, the &quot;Quit&amp;quot; button should be more important, so why not make it bigger ?
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 [[Qt_for_beginners_Exercise_1_answer|here]]. But we really recommend you to try and figure it out by yourself how to solve these exercises.
The answers can be found [[Qt_for_beginners_Exercise_1_answer|here]]. But we really recommend you to try and figure it out by yourself how to solve these exercises.

Revision as of 09:25, 25 February 2015

[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

// We allocate a new button group,
// and attached it to the parent object
// note that the parent object might be
// the main window, or "this"
QButtonGroup '''buttonGroup = new QButtonGroup(object);

// Add buttons in the button group
buttonGroup->addButton(button1);
buttonGroup->addButton(button2);
buttonGroup->addButton(button3);

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 :

#include <QApplication>
#include <QPushButton>

int main(int argc, char'''*argv)
{
 QApplication app (argc, argv);

QWidget window;
 window.setFixedSize(100, 80);

QPushButton *buttonInfo = new QPushButton("Info", &amp;window);
 buttonInfo->setGeometry(10, 10, 80, 30);

QPushButton *buttonQuit = new QPushButton("Quit", &amp;window);
 buttonQuit->setGeometry(10, 40, 80, 30);

window.show();

// Add your code here

return app.exec();
}

In order to display the information about Qt, you should use the following method

void QApplication::aboutQt();

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.