Qt for beginners Exercise 1 basis: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Redirect to Qt for Beginners (Merged))
 
(5 intermediate revisions by 3 users not shown)
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]]
#REDIRECT [[Qt for Beginners]]
 
= 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;]]
 
'''''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.''
 
== 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<br />* text<br />* icon<br />* tooltip<br />* …
 
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
 
<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);
 
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 /></code>
 
In order to display the information about Qt, you should use the following method
 
<code><br />void QApplication::aboutQt();<br /></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 ?
 
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.

Latest revision as of 14:35, 5 May 2015

Redirect to: