Qt for beginners Exercise 1 answer: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
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]]
[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|&lt;&lt;&lt; Exercise 1 : basis]] | [[Qt_for_beginners|Summary]] | Exercise 2 &gt;&gt;&gt;
[[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''<br /><code><br />#include &lt;QApplication&amp;gt;<br />#include &quot;window.h&amp;quot;
''main.cpp''
<code>
#include <QApplication>
#include "window.h"


int main(int argc, char **argv)<br />{<br /> QApplication app (argc, argv);
int main(int argc, char **argv)
{
QApplication app (argc, argv);


Window window;<br /> window.show();
Window window;
window.show();


return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
return app.exec();
}
</code>


''window.h''<br /><code><br />#ifndef WINDOW_H<br />#define WINDOW_H
''window.h''
<code>
#ifndef WINDOW_H
#define WINDOW_H


#include &lt;QWidget&amp;gt;
#include <QWidget>


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;
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;<br />};
QPushButton *m_selectButton;
};


#endif // WINDOW_H<br /></code>
#endif // WINDOW_H
</code>


''window.cpp''<br /><code><br />#include &quot;window.h&amp;quot;
''window.cpp''
<code>
#include "window.h"


#include &lt;QRadioButton&amp;gt;<br />#include &lt;QPushButton&amp;gt;<br />#include &lt;QButtonGroup&amp;gt;
#include <QRadioButton>
#include <QPushButton>
#include <QButtonGroup>


Window::Window(QWidget *parent) :<br /> QWidget(parent)<br />{<br /> // Set size of the window<br /> setFixedSize(200, 150);
Window::Window(QWidget *parent) :
QWidget(parent)
{
// Set size of the window
setFixedSize(200, 150);


// Create and position the button<br /> m_chickenButton = new QRadioButton(&quot;Roasted chicken&amp;quot;, this);<br /> m_chickenButton-&gt;setGeometry(10, 10, 180, 30);
// Create and position the button
m_chickenButton = new QRadioButton("Roasted chicken", this);
m_chickenButton->setGeometry(10, 10, 180, 30);


m_sandwichButton = new QRadioButton(&quot;Sandwich&amp;quot;, this);<br /> m_sandwichButton-&gt;setGeometry(10, 40, 180, 30);
m_sandwichButton = new QRadioButton("Sandwich", this);
m_sandwichButton->setGeometry(10, 40, 180, 30);


m_soupButton = new QRadioButton(&quot;Soup&amp;quot;, this);<br /> m_soupButton-&gt;setGeometry(10, 70, 180, 30);
m_soupButton = new QRadioButton("Soup", this);
m_soupButton->setGeometry(10, 70, 180, 30);


m_selectButton = new QPushButton(&quot;Select this menu&amp;quot;, this);<br /> m_selectButton-&gt;setGeometry(10, 110, 180, 30);
m_selectButton = new QPushButton("Select this menu", this);
m_selectButton->setGeometry(10, 110, 180, 30);


QButtonGroup '''group = new QButtonGroup(this);<br /> group-&gt;addButton(m_chickenButton);<br /> group-&gt;addButton(m_sandwichButton);<br /> group-&gt;addButton(m_soupButton);<br />}<br /></code>
QButtonGroup '''group = new QButtonGroup(this);
<br />You can also remove the buttons from the attributes list and put the inside the constructor.
group->addButton(m_chickenButton);
group->addButton(m_sandwichButton);
group->addButton(m_soupButton);
}
</code>


<br />h2. Signals and slots
You can also remove the buttons from the attributes list and put the inside the constructor.
<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);
h2. Signals and slots


QPushButton *buttonQuit = new QPushButton(&quot;Quit&amp;quot;, &amp;window);<br /> buttonQuit-&gt;setGeometry(10, 40, 80, 30);
<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", &amp;window);
buttonInfo->setGeometry(10, 10, 80, 30);
 
QPushButton *buttonQuit = new QPushButton("Quit", &amp;window);
buttonQuit->setGeometry(10, 40, 80, 30);


window.show();
window.show();


// Connections<br /> // Connecting the button associated to info to the &quot;aboutQt&amp;quot; slot of QApplication<br /> QObject::connect(buttonInfo, SIGNAL (clicked()), &amp;app, SLOT (aboutQt()));
// Connections
// Connecting the button associated to info to the "aboutQt" slot of QApplication
QObject::connect(buttonInfo, SIGNAL (clicked()), &amp;app, SLOT (aboutQt()));


// Connecting the button associated to close to the &quot;quit&amp;quot; slot of QApplication<br /> QObject::connect(buttonQuit, SIGNAL (clicked()), &amp;app, SLOT (quit()));
// Connecting the button associated to close to the "quit" slot of QApplication
QObject::connect(buttonQuit, SIGNAL (clicked()), &amp;app, SLOT (quit()));


return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
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", &amp;window);
 buttonInfo->setGeometry(10, 10, 80, 30);

QPushButton *buttonQuit = new QPushButton("Quit", &amp;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()), &amp;app, SLOT (aboutQt()));

// Connecting the button associated to close to the "quit" slot of QApplication
 QObject::connect(buttonQuit, SIGNAL (clicked()), &amp;app, SLOT (quit()));

return app.exec();
}