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:
=Qt for beginners — Exercise 1 : basis=
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]<br />[[Category:Qt_for_beginners]]<br />[[Category:Tutorial]]<br />[[Category:HowTo]]


[[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 — Exercise 1 : basis =


'''''Note:''' Unfortunately, the images are no longer available. See the official [http://doc.qt.io/qt-5/gettingstartedqt.html Getting Started with Qt Widgets] ''[qt.io]'' page for an alternative tutorial.''
[[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;]]


==Widgets==
'''''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.''


Radio button is a standard <span class="caps">GUI</span> component. It is often used to make a unique choice from a list. In Qt, the [[doc/QRadioButton|QRadioButton]] is used to create radio buttons.
== Widgets ==


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
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.


* text
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 />* …
* 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|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 &quot;exclusive&amp;quot; behaviour of many radio buttons, we need to use [[Doc:QButtonGroup]]. This class can be used like this


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.
<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);
<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>
<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.
<br />Obviously, nothing will happen (now) when the buttons are clicked.
<br />[[Image:http://farm8.staticflickr.com/7109/7514416312_880b14d647.jpg|menu chooser]]
<br />h2. Signals and slots
<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);


Obviously, nothing will happen (now) when the buttons are clicked.
QWidget window;<br /> window.setFixedSize(100, 80);


[[Image:7514416312_880b14d647.jpg|menu chooser]]
QPushButton *buttonInfo = new QPushButton(&quot;Info&amp;quot;, &amp;window);<br /> buttonInfo-&gt;setGeometry(10, 10, 80, 30);


==Signals and slots==
QPushButton *buttonQuit = new QPushButton(&quot;Quit&amp;quot;, &amp;window);<br /> buttonQuit-&gt;setGeometry(10, 40, 80, 30);


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
window.show();


[[Image:7503975304_5d67868890_b.jpg|About Qt]]
// Add your code here


We provide you the following code to complete :
return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></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


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 ?
<code><br />void QApplication::aboutQt();<br /></code>


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.
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 ?


[[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;]]
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.
 
===Categories:===
 
* [[:Category:HowTo|HowTo]]
* [[:Category:Qt for beginners|Qt_for_beginners]]
* [[:Category:Tutorial|Tutorial]]

Revision as of 08:56, 24 February 2015

[toc align_right="yes&quot; depth="3&quot;]


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&quot;: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&quot; 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 &quot;this&amp;quot;<br />QButtonGroup '''buttonGroup = new QButtonGroup(object);
<br />// Add buttons in the button group<br />buttonGroup-&gt;addButton(button1);<br />buttonGroup-&gt;addButton(button2);<br />buttonGroup-&gt;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 &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);

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

QPushButton *buttonQuit = new QPushButton(&quot;Quit&amp;quot;, &amp;window);<br /> buttonQuit-&gt;setGeometry(10, 40, 80, 30);

window.show();

// Add your code here

return app.exec&amp;amp;#40;&amp;#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&quot; 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.