|
|
| (4 intermediate revisions by 3 users not shown) |
| Line 1: |
Line 1: |
| [toc align_right="yes&quot; depth="3&quot;]<br />[[Category:Qt_for_beginners]]<br />[[Category:Tutorial]]<br />[[Category:HowTo]]
| | #REDIRECT [[Qt for Beginners]] |
| | |
| = Qt for beginners — Exercise 1 : basis (Answer) =
| |
| | |
| [[Qt_for_beginners_Exercise_1_basis|<<< Exercise 1 : basis]] | [[Qt_for_beginners|Summary]] | Exercise 2 >>>
| |
| | |
| == Widgets ==
| |
| | |
| Since this application seems to be quite complicated, we have chosen to split into several files :
| |
| | |
| ''main.cpp''<br /><code><br />#include <QApplication&gt;<br />#include "window.h&quot;
| |
| | |
| int main(int argc, char **argv)<br />{<br /> QApplication app (argc, argv);
| |
| | |
| Window window;<br /> window.show();
| |
| | |
| return app.exec&amp;#40;&#41;;<br />}<br /></code>
| |
| | |
| ''window.h''<br /><code><br />#ifndef WINDOW_H<br />#define WINDOW_H
| |
| | |
| #include <QWidget&gt;
| |
| | |
| class QPushButton;<br />class QRadioButton;<br />class Window : public QWidget<br />{<br />public:<br /> explicit Window(QWidget *parent = 0);<br />private:<br /> QRadioButton *m_chickenButton;<br /> QRadioButton *m_sandwichButton;<br /> QRadioButton *m_soupButton;
| |
| | |
| QPushButton *m_selectButton;<br />};
| |
| | |
| #endif // WINDOW_H<br /></code>
| |
| | |
| ''window.cpp''<br /><code><br />#include "window.h&quot;
| |
| | |
| #include <QRadioButton&gt;<br />#include <QPushButton&gt;<br />#include <QButtonGroup&gt;
| |
| | |
| Window::Window(QWidget *parent) :<br /> QWidget(parent)<br />{<br /> // Set size of the window<br /> setFixedSize(200, 150);
| |
| | |
| // Create and position the button<br /> m_chickenButton = new QRadioButton("Roasted chicken&quot;, this);<br /> m_chickenButton->setGeometry(10, 10, 180, 30);
| |
| | |
| m_sandwichButton = new QRadioButton("Sandwich&quot;, this);<br /> m_sandwichButton->setGeometry(10, 40, 180, 30);
| |
| | |
| m_soupButton = new QRadioButton("Soup&quot;, this);<br /> m_soupButton->setGeometry(10, 70, 180, 30);
| |
| | |
| m_selectButton = new QPushButton("Select this menu&quot;, this);<br /> m_selectButton->setGeometry(10, 110, 180, 30);
| |
| | |
| QButtonGroup '''group = new QButtonGroup(this);<br /> group->addButton(m_chickenButton);<br /> group->addButton(m_sandwichButton);<br /> group->addButton(m_soupButton);<br />}<br /></code>
| |
| <br />You can also remove the buttons from the attributes list and put the inside the constructor.
| |
| | |
| <br />h2. Signals and slots
| |
| <br /><code><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();
| |
| | |
| // Connections<br /> // Connecting the button associated to info to the "aboutQt&quot; slot of QApplication<br /> QObject::connect(buttonInfo, SIGNAL (clicked()), &app, SLOT (aboutQt()));
| |
| | |
| // Connecting the button associated to close to the "quit&quot; slot of QApplication<br /> QObject::connect(buttonQuit, SIGNAL (clicked()), &app, SLOT (quit()));
| |
| | |
| return app.exec&amp;#40;&#41;;<br />}<br /></code>
| |