|
|
| Line 1: |
Line 1: |
| {{Cleanup | reason=Auto-imported from ExpressionEngine.}}
| | [[Category:Delete]] |
| | | {{WarningBox|text=Merged into [[Qt for Beginners]]}} |
| [toc align_right="yes" depth="3"]
| |
| [[Category:Qt_for_beginners]] | |
| [[Category:Tutorial]]
| |
| [[Category:HowTo]] | |
| | |
| = 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''
| |
| <code>
| |
| #include <QApplication>
| |
| #include "window.h"
| |
| | |
| int main(int argc, char **argv)
| |
| {
| |
| QApplication app (argc, argv);
| |
| | |
| Window window;
| |
| window.show();
| |
| | |
| return app.exec();
| |
| } | |
| </code>
| |
| | |
| ''window.h''
| |
| <code>
| |
| #ifndef WINDOW_H
| |
| #define WINDOW_H
| |
| | |
| #include <QWidget>
| |
| | |
| class QPushButton;
| |
| class QRadioButton;
| |
| class Window : public QWidget
| |
| {
| |
| public:
| |
| explicit Window(QWidget *parent = 0);
| |
| private:
| |
| QRadioButton *m_chickenButton;
| |
| QRadioButton *m_sandwichButton;
| |
| QRadioButton *m_soupButton;
| |
| | |
| QPushButton *m_selectButton;
| |
| };
| |
| | |
| #endif // WINDOW_H
| |
| </code>
| |
| | |
| ''window.cpp''
| |
| <code>
| |
| #include "window.h"
| |
| | |
| #include <QRadioButton>
| |
| #include <QPushButton>
| |
| #include <QButtonGroup>
| |
| | |
| Window::Window(QWidget *parent) :
| |
| QWidget(parent)
| |
| {
| |
| // Set size of the window
| |
| setFixedSize(200, 150);
| |
| | |
| // Create and position the button
| |
| m_chickenButton = new QRadioButton("Roasted chicken", this);
| |
| m_chickenButton->setGeometry(10, 10, 180, 30);
| |
| | |
| m_sandwichButton = new QRadioButton("Sandwich", this);
| |
| m_sandwichButton->setGeometry(10, 40, 180, 30);
| |
| | |
| m_soupButton = new QRadioButton("Soup", this);
| |
| m_soupButton->setGeometry(10, 70, 180, 30);
| |
| | |
| m_selectButton = new QPushButton("Select this menu", this);
| |
| m_selectButton->setGeometry(10, 110, 180, 30);
| |
| | |
| QButtonGroup '''group = new QButtonGroup(this);
| |
| group->addButton(m_chickenButton);
| |
| group->addButton(m_sandwichButton);
| |
| group->addButton(m_soupButton);
| |
| }
| |
| </code>
| |
| | |
| You can also remove the buttons from the attributes list and put the inside the constructor.
| |
| | |
| | |
| == Signals and slots ==
| |
| <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", &window);
| |
| buttonInfo->setGeometry(10, 10, 80, 30);
| |
| | |
| QPushButton *buttonQuit = new QPushButton("Quit", &window);
| |
| buttonQuit->setGeometry(10, 40, 80, 30);
| |
| | |
| window.show();
| |
| | |
| // Connections
| |
| // Connecting the button associated to info to the "aboutQt" slot of QApplication
| |
| QObject::connect(buttonInfo, SIGNAL (clicked()), &app, SLOT (aboutQt()));
| |
| | |
| // Connecting the button associated to close to the "quit" slot of QApplication
| |
| QObject::connect(buttonQuit, SIGNAL (clicked()), &app, SLOT (quit()));
| |
| | |
| return app.exec();
| |
| } | |
| </code>
| |