Qt for beginners Exercise 1 answer: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[toc align_right= | [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 (Answer) = | ||
[[Qt_for_beginners_Exercise_1_basis| | [[Qt_for_beginners_Exercise_1_basis|<<< Exercise 1 : basis]] | [[Qt_for_beginners|Summary]] | Exercise 2 >>> | ||
== Widgets == | == Widgets == | ||
Line 9: | Line 12: | ||
Since this application seems to be quite complicated, we have chosen to split into several files : | Since this application seems to be quite complicated, we have chosen to split into several files : | ||
''main.cpp'' | ''main.cpp'' | ||
<code> | |||
#include <QApplication> | |||
#include "window.h" | |||
int main(int argc, char **argv) | int main(int argc, char **argv) | ||
{ | |||
QApplication app (argc, argv); | |||
Window window; | Window window; | ||
window.show(); | |||
return app.exec | return app.exec(); | ||
} | |||
</code> | |||
''window.h'' | ''window.h'' | ||
<code> | |||
#ifndef WINDOW_H | |||
#define WINDOW_H | |||
#include | #include <QWidget> | ||
class QPushButton; | 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; | QPushButton *m_selectButton; | ||
}; | |||
#endif // WINDOW_H | #endif // WINDOW_H | ||
</code> | |||
''window.cpp'' | ''window.cpp'' | ||
<code> | |||
#include "window.h" | |||
#include | #include <QRadioButton> | ||
#include <QPushButton> | |||
#include <QButtonGroup> | |||
Window::Window(QWidget *parent) : | Window::Window(QWidget *parent) : | ||
QWidget(parent) | |||
{ | |||
// Set size of the window | |||
setFixedSize(200, 150); | |||
// Create and position the button | // Create and position the button | ||
m_chickenButton = new QRadioButton("Roasted chicken", this); | |||
m_chickenButton->setGeometry(10, 10, 180, 30); | |||
m_sandwichButton = new QRadioButton( | m_sandwichButton = new QRadioButton("Sandwich", this); | ||
m_sandwichButton->setGeometry(10, 40, 180, 30); | |||
m_soupButton = new QRadioButton( | m_soupButton = new QRadioButton("Soup", this); | ||
m_soupButton->setGeometry(10, 70, 180, 30); | |||
m_selectButton = new QPushButton( | m_selectButton = new QPushButton("Select this menu", this); | ||
m_selectButton->setGeometry(10, 110, 180, 30); | |||
QButtonGroup '''group = new QButtonGroup(this); | 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. | |||
h2. Signals and slots | |||
QPushButton * | <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(); | window.show(); | ||
// Connections | // 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 | // Connecting the button associated to close to the "quit" slot of QApplication | ||
QObject::connect(buttonQuit, SIGNAL (clicked()), &app, SLOT (quit())); | |||
return app.exec | return app.exec(); | ||
} | |||
</code> |
Revision as of 10:16, 25 February 2015
[toc align_right="yes" depth="3"]
Qt for beginners — Exercise 1 : basis (Answer)
<<< Exercise 1 : basis | Summary | Exercise 2 >>>
Widgets
Since this application seems to be quite complicated, we have chosen to split into several files :
main.cpp
#include <QApplication>
#include "window.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
Window window;
window.show();
return app.exec();
}
window.h
#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
window.cpp
#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);
}
You can also remove the buttons from the attributes list and put the inside the constructor.
h2. Signals and slots
#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();
}