Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Qt for beginners pretty button: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
=Qt for beginners — A pretty button=
[[Category:Qt_for_beginners]]<br />[[Category:Tutorial]]<br />[[Category:HowTo]]


[[Qt for beginners Hello World|&lt;&lt;&lt; Hello World]] | [[Qt for beginners|Summary]] | [[Qt for beginners Signals and slots|Signals and Slots &gt;&gt;&gt;]]
= Qt for beginners — A pretty button =


'''''Note:''' Unfortunately, the images are no longer available. See the official [http://doc.qt.io/qt-5/gettingstartedqt.html Getting Started with Qt Widgets] ''[qt.io]'' page for an alternative tutorial.''
[[Qt_for_beginners_Hello_World|&lt;&lt;&lt; Hello World]] | [[Qt_for_beginners|Summary]] | [[Qt_for_beginners_Signals_and_slots|Signals and Slots &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.''


This chapter gives an overview of the widgets modules. It will cover widgets properties, the inheritance scheme that is used in widgets, and also the parenting system.
This chapter gives an overview of the widgets modules. It will cover widgets properties, the inheritance scheme that is used in widgets, and also the parenting system.


==A pretty button==
== A pretty button ==


Now that we have our button, we may want to customize it a bit.
Now that we have our button, we may want to customize it a bit.


[[Image:7461280736_3982305f74_z.jpg|buttons]]
[[Image:http://web.archive.org/web/20130724080021/http://farm9.staticflickr.com/8023/7461280736_3982305f74_z.jpg|buttons]]


Qt objects have a lot of attributes that can be modified using getters and setters. In Qt, if an attribute is called ''foo'', the associated getter and setter will have these signatures
Qt objects have a lot of attributes that can be modified using getters and setters. In Qt, if an attribute is called ''foo'', the associated getter and setter will have these signatures


In fact, Qt extends this system of attributes and getters and setters to something called ''property''. A property is a value of any type that can be accessed, be modified or constant, and can notify a change. The property system is useful, especially in the third part (<span class="caps">QML</span>). For now, we will use “attribute” or “property” to do the same thing.
<code><br />T foo() const;<br />void setFoo(const T);<br /></code>


A QPushButton has plenty of properties :
In fact, Qt extends this system of attributes and getters and setters to something called ''property''. A property is a value of any type that can be accessed, be modified or constant, and can notify a change. The property system is useful, especially in the third part (QML). For now, we will use &quot;attribute&amp;quot; or &quot;property&amp;quot; to do the same thing.


* text
A QPushButton has plenty of properties :<br />* text<br />* font<br />* tooltip<br />* icon<br />…
* font
* tooltip
* icon<br /> …


So we can use these to customize the button.
So we can use these to customize the button.


Let’s first change the text and add a tooltip<br />
Let's first change the text and add a tooltip<br /><code><br />#include &lt;QApplication&amp;gt;<br />#include &lt;QPushButton&amp;gt;


Here is the result
int main(int argc, char **argv)<br />{<br /> QApplication app (argc, argv);
 
QPushButton button;<br /> button.setText(&quot;My text&amp;quot;);<br /> button.setToolTip(&quot;A tooltip&amp;quot;);<br /> button.show();


[[Image:7461415006_e067eeca92_m.jpg|Button with tooltip]]
return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>


We can also change the font. In Qt, a font is represented with the [[doc/QFont|QFont]] class. The documentation provides a lot of information. We are especially concerned here with one of the constructors of QFont.
Here is the result


In order to change the font, we have to instantiate a <code>QFont</code> class, and pass it to the <code>QPushButton</code> using <code>setFont</code>. The following snippet will change the font to Courier.
[[Image:http://web.archive.org/web/20130724094319/http://farm9.staticflickr.com/8027/7461415006_e067eeca92_m.jpg|Button with tooltip]]


You can try other parameters of QFont’s constructor to reproduce the button that is represented in the first picture in this chapter.
We can also change the font. In Qt, a font is represented with the [[Doc:QFont]] class. The documentation provides a lot of information. We are especially concerned here with one of the constructors of QFont.


Setting an icon is not very difficult either. An icon is represented with the [[doc/QIcon|QIcon]] class. And you can create an icon provided that it has an absolute (or relative) path in the filesystem. I recommend providing the absolute path in this example. But for deployment considerations, you might use the relative path, or better, the resource system.
<code><br />QFont(const QString &amp; family, int pointSize = –1, int weight = <s>1, bool italic = false)<br /></code>
<br />In order to change the font, we have to instantiate a &lt;code&amp;gt;QFont&amp;lt;/code&amp;gt; class, and pass it to the &lt;code&amp;gt;QPushButton&amp;lt;/code&amp;gt; using &lt;code&amp;gt;setFont&amp;lt;/code&amp;gt;. The following snippet will change the font to Courier.
<br /><code><br />QFont font (&quot;Courier&amp;quot;);<br />button.setFont(font);<br /></code>
<br />You can try other parameters of QFont's constructor to reproduce the button that is represented in the first picture in this chapter.
<br />Setting an icon is not very difficult either. An icon is represented with the [[Doc:QIcon]] class. And you can create an icon provided that it has an absolute (or relative) path in the filesystem. I recommend providing the absolute path in this example. But for deployment considerations, you might use the relative path, or better, the resource system.
<br /><code><br />QIcon icon (&quot;/path/to/my/icon/icon.png&amp;quot;);<br />button.setIcon(icon);<br /></code>
<br />On Linux, and some other OS's, there is a convenient way to set an icon from an icon theme. It can be done by using the static method
<br /><code><br />QIcon Qicon::fromTheme ( const QString &amp;name, const QIcon &amp;fallback = QIcon());<br /></code>
<br />For example, in the screenshot at the beginning of this chapter, the smiley comes from the Oxygen KDE icon theme and was set by
<br /><code><br />button.setIcon(QIcon::fromTheme(&quot;face-smile&amp;quot;));<br /></code>
<br />h2. Qt class hiearchy
<br />Qt widely uses inheritance, especially in the Widgets module. The following graph shows some of these inheritance
<br />[[Image:http://web.archive.org/web/20130724091512/http://farm9.staticflickr.com/8158/7462350426_b5a7557a46_z.jpg|QObject inheritance tree]]
<br />[[Doc:QObject]] is the most basic class in Qt. Most of classes in Qt inherit from this class. QObject provides some very powerful capabilities like :<br />* '''object name''' : you can set a name, as a string, to an object and search for objects by names.<br />* '''parenting system''' (described in the following section)<br />* '''signals and slots''' (described in the next chapter)<br />* '''event management'''
<br />Widgets are able to respond to events and use parenting system and signals and slots mechanism. All widgets inherit from QObject. The most basic widget is the [[Doc:QWidget]]. QWidget contains most properties that are used to describe a window, or a widget, like position and size, mouse cursor, tooltips, etc.
<br />'''Remark''' : in Qt, a widget can also be a window. In the previous section we displayed a button, that is a widget, but it appears directly as a window. There is no need of a &quot;QWindow&amp;quot; class.
<br />Nearly all graphical elements inherit from QWidget. We can list for example<br />* QAbstractButton, a base class for all button types<br />'''''' QPushButton<br />'''''' QCheckBox<br />'''''' QRadioButton<br />* QFrame, that displays a frame<br />* QLabel, that displays text or picture
<br />This inheritance is done in order to facilitate properties management. Shared properties like size are cursors can be used on other graphical components, and QAbstractButton provides basic properties that are shared by all buttons.
<br />h2. Parenting system
<br />Parenting system is a convenient way of dealing with objects in Qt, especially widgets. Any object that inherits from [[Doc:QObject]] can have a parent and children. This hierarchy tree makes many things convenient :<br />* When an object is destroyed, all of its children are destroyed as well. So calling ''delete'' becomes optional in certain cases<br />* All QObjects have ''findChild'' and ''findChildren'' methods that can be used to search for children of a given object.<br />* Child widgets in a [[Doc:QWidget]] automatically appear inside the parent widget.
<br />The following snippet that creates a [[Doc:QPushButton]] inside a QPushButton:
<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);
<br /> QPushButton button1 (&quot;test&amp;quot;);<br /> QPushButton button2 (&quot;other&amp;quot;, &amp;button1);
<br /> button1.show();
<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
<br />[[Image:http://web.archive.org/web/20130724081640/http://farm8.staticflickr.com/7132/7479250714_3335bd48d4.jpg|button_in_button]]
<br />You can also note that when the application is closed, &lt;code&amp;gt;button1&amp;lt;/code&amp;gt;, which is allocated on the stack, is deallocated. Since &lt;code&amp;gt;button2&amp;lt;/code&amp;gt; has &lt;code&amp;gt;button1&amp;lt;/code&amp;gt; as a parent, it is deleted also. You can even test this in Qt Creator in the analyze section, by searching for a memory leak—there won't be any.
<br />There is clearly no benefit in putting a button inside a button, but based on this idea, we might want to put buttons inside a container, that does not display anything. This container is simply the [[Doc:QWidget]].
<br />The following code is used to display a button inside a widget
<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);
<br /> QWidget window;<br /> window.setFixedSize(100, 50);
<br /> QPushButton *button = new QPushButton(&quot;Hello World&amp;quot;, &amp;window);<br /> button</s>&gt;setGeometry(10, 10, 80, 30);


On Linux, and some other OS’s, there is a convenient way to set an icon from an icon theme. It can be done by using the static method
window.show();


For example, in the screenshot at the beginning of this chapter, the smiley comes from the Oxygen <span class="caps">KDE</span> icon theme and was set by
return app.exec&amp;amp;#40;&amp;#41;;


==Qt class hiearchy==
}<br /></code>


Qt widely uses inheritance, especially in the Widgets module. The following graph shows some of these inheritance
Note that we create a fixed size widget (that acts as a window) using ''setFixedSize''. This method has the following signature


[[Image:7462350426_b5a7557a46_z.jpg|QObject inheritance tree]]
<code><br />void QWidget::setFixedSize(int width, int height);<br /></code>


[[doc/QObject|QObject]] is the most basic class in Qt. Most of classes in Qt inherit from this class. QObject provides some very powerful capabilities like :
We also positioned the button using ''setGeometry''. This method has the following signature


* '''object name''' : you can set a name, as a string, to an object and search for objects by names.
<code><br />void QWidget::setGeometry(int x, int y, int width, int height);<br /></code>
* '''parenting system''' (described in the following section)
* '''signals and slots''' (described in the next chapter)
* '''event management'''


Widgets are able to respond to events and use parenting system and signals and slots mechanism. All widgets inherit from QObject. The most basic widget is the [[doc/QWidget|QWidget]]. QWidget contains most properties that are used to describe a window, or a widget, like position and size, mouse cursor, tooltips, etc.
== Subclassing QWidget ==


'''Remark''' : in Qt, a widget can also be a window. In the previous section we displayed a button, that is a widget, but it appears directly as a window. There is no need of a “QWindow” class.
Until now, we have put all of our code in the ''main'' function. This was not a problem for our simple examples, but for more and more complex applications we might want to split our code into different classes. What is often done is to create a class that is used to display a window, and implement all the widgets that are contained in this window as attributes of this class.


Nearly all graphical elements inherit from QWidget. We can list for example
Inside Qt Creator, you can automatically create a new class with File &gt; New file or project &gt; C++ &gt; C++ Class


* QAbstractButton, a base class for all button types
[[Image:http://web.archive.org/web/20130724095005/http://farm8.staticflickr.com/7259/7507029228_790c08c46a_b.jpg|Create class dialog]]
** QPushButton
** QCheckBox
** QRadioButton
* QFrame, that displays a frame
* QLabel, that displays text or picture


This inheritance is done in order to facilitate properties management. Shared properties like size are cursors can be used on other graphical components, and QAbstractButton provides basic properties that are shared by all buttons.
Make the class inherit from QWidget, and you should obtain code similar to below


==Parenting system==
''Header''


Parenting system is a convenient way of dealing with objects in Qt, especially widgets. Any object that inherits from [[doc/QObject|QObject]] can have a parent and children. This hierarchy tree makes many things convenient :
<code><br />#ifndef WINDOW_H<br />#define WINDOW_H


* When an object is destroyed, all of its children are destroyed as well. So calling ''delete'' becomes optional in certain cases
#include &lt;QWidget&amp;gt;
* All QObjects have ''findChild'' and ''findChildren'' methods that can be used to search for children of a given object.
* Child widgets in a [[doc/QWidget|QWidget]] automatically appear inside the parent widget.


The following snippet that creates a [[doc/QPushButton|QPushButton]] inside a QPushButton:
class Window : public QWidget<br />{<br /> Q_OBJECT<br />public:<br /> explicit Window(QWidget *parent = 0);


[[Image:7479250714_3335bd48d4.jpg|button_in_button]]
signals:


You can also note that when the application is closed, <code>button1</code>, which is allocated on the stack, is deallocated. Since <code>button2</code> has <code>button1</code> as a parent, it is deleted also. You can even test this in Qt Creator in the analyze section, by searching for a memory leak—there won’t be any.
public slots:


There is clearly no benefit in putting a button inside a button, but based on this idea, we might want to put buttons inside a container, that does not display anything. This container is simply the [[doc/QWidget|QWidget]].
};


The following code is used to display a button inside a widget
#endif // WINDOW_H<br /></code>


Note that we create a fixed size widget (that acts as a window) using ''setFixedSize''. This method has the following signature
''Source''


We also positioned the button using ''setGeometry''. This method has the following signature
<code><br />#include &quot;window.h&amp;quot;


==Subclassing QWidget==
Window::Window(QWidget '''parent) :<br /> QWidget(parent)<br />{<br />}<br /></code>
<br />You can see that Qt Creator automatically generates a class template. Notice that there are some new elements in the header :<br />''' The '''Q_OBJECT''' macro.<br />* A new category of methods : '''signals'''<br />* A new category of methods : '''public slots'''


Until now, we have put all of our code in the ''main'' function. This was not a problem for our simple examples, but for more and more complex applications we might want to split our code into different classes. What is often done is to create a class that is used to display a window, and implement all the widgets that are contained in this window as attributes of this class.
All these elements will be explained in the next chapter, and none of them are needed now.<br />Implementing the window is done in the constructor. We can declare the size of the window, as well as the widgets that this window contains and their positions. For example, implementing the previous window that contains a button can be done in this way :


Inside Qt Creator, you can automatically create a new class with File &gt; New file or project &gt; C++ &gt; C++ Class
''main.cpp''


[[Image:7507029228_790c08c46a_b.jpg|Create class dialog]]
<code><br />#include &lt;QApplication&amp;gt;<br />#include &quot;window.h&amp;quot;


Make the class inherit from QWidget, and you should obtain code similar to below
int main(int argc, char **argv)<br />{<br /> QApplication app (argc, argv);


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


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


You can see that Qt Creator automatically generates a class template. Notice that there are some new elements in the header :
''window.h''


* The '''Q_OBJECT''' macro.
<code><br />#ifndef WINDOW_H<br />#define WINDOW_H
* A new category of methods : '''signals'''
* A new category of methods : '''public slots'''


All these elements will be explained in the next chapter, and none of them are needed now.<br /> Implementing the window is done in the constructor. We can declare the size of the window, as well as the widgets that this window contains and their positions. For example, implementing the previous window that contains a button can be done in this way :
#include &lt;QWidget&amp;gt;


''main.cpp''
class QPushButton;<br />class Window : public QWidget<br />{<br />public:<br /> explicit Window(QWidget *parent = 0);<br />private:<br /> QPushButton *m_button;<br />};


''window.h''
#endif // WINDOW_H<br /></code>


''window.cpp''
''window.cpp''


Please note that there is no need for writing a destructor for deleting <code>m_button</code>. With the parenting system, when the <code>Window</code> instance is out of the stack, the <code>m_button</code> is automatically deleted.
<code><br />#include &quot;window.h&amp;quot;
 
#include &lt;QPushButton&amp;gt;


==See Also==
Window::Window(QWidget *parent) :<br /> QWidget(parent)<br />{<br /> // Set size of the window<br /> setFixedSize(100, 50);


A better overview of [[doc/qpushbutton|qpushbutton]] is given in this wiki page [[How to Use QPushButton|How to use QPushButton]]
// Create and position the button<br /> m_button = new QPushButton(&quot;Hello World&amp;quot;, this);<br /> m_button-&gt;setGeometry(10, 10, 80, 30);<br />}<br /></code>


[[Qt for beginners Hello World|&lt;&lt;&lt; Hello World]] | [[Qt for beginners|Summary]] | [[Qt for beginners Signals and slots|Signals and Slots &gt;&gt;&gt;]]
Please note that there is no need for writing a destructor for deleting &lt;code&amp;gt;m_button&amp;lt;/code&amp;gt;. With the parenting system, when the &lt;code&amp;gt;Window&amp;lt;/code&amp;gt; instance is out of the stack, the &lt;code&amp;gt;m_button&amp;lt;/code&amp;gt; is automatically deleted.


===Categories:===
== See Also ==


* [[:Category:HowTo|HowTo]]
A better overview of [[Doc:qpushbutton]] is given in this wiki page [[How_to_Use_QPushButton|How to use QPushButton]]
* [[:Category:Qt for beginners|Qt_for_beginners]]
* [[:Category:Tutorial|Tutorial]]

Revision as of 12:47, 23 February 2015



Qt for beginners — A pretty button

<<< Hello World | Summary | Signals and Slots >>>

Note: Unfortunately, the images are no longer available. See the official "Getting Started with Qt Widgets&quot;:http://doc.qt.io/qt-5/gettingstartedqt.html page for an alternative tutorial.

This chapter gives an overview of the widgets modules. It will cover widgets properties, the inheritance scheme that is used in widgets, and also the parenting system.

A pretty button

Now that we have our button, we may want to customize it a bit.

buttons

Qt objects have a lot of attributes that can be modified using getters and setters. In Qt, if an attribute is called foo, the associated getter and setter will have these signatures

<br />T foo() const;<br />void setFoo(const T);<br />

In fact, Qt extends this system of attributes and getters and setters to something called property. A property is a value of any type that can be accessed, be modified or constant, and can notify a change. The property system is useful, especially in the third part (QML). For now, we will use "attribute&quot; or "property&quot; to do the same thing.

A QPushButton has plenty of properties :
* text
* font
* tooltip
* icon

So we can use these to customize the button.

Let's first change the text and add a tooltip

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

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

QPushButton button;<br /> button.setText(&quot;My text&amp;quot;);<br /> button.setToolTip(&quot;A tooltip&amp;quot;);<br /> button.show();

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

Here is the result

Button with tooltip

We can also change the font. In Qt, a font is represented with the Doc:QFont class. The documentation provides a lot of information. We are especially concerned here with one of the constructors of QFont.

<br />QFont(const QString &amp; family, int pointSize = 1, int weight = <s>1, bool italic = false)<br />


In order to change the font, we have to instantiate a <code&gt;QFont&lt;/code&gt; class, and pass it to the <code&gt;QPushButton&lt;/code&gt; using <code&gt;setFont&lt;/code&gt;. The following snippet will change the font to Courier.


<br />QFont font (&quot;Courier&amp;quot;);<br />button.setFont(font);<br />


You can try other parameters of QFont's constructor to reproduce the button that is represented in the first picture in this chapter.
Setting an icon is not very difficult either. An icon is represented with the Doc:QIcon class. And you can create an icon provided that it has an absolute (or relative) path in the filesystem. I recommend providing the absolute path in this example. But for deployment considerations, you might use the relative path, or better, the resource system.


<br />QIcon icon (&quot;/path/to/my/icon/icon.png&amp;quot;);<br />button.setIcon(icon);<br />


On Linux, and some other OS's, there is a convenient way to set an icon from an icon theme. It can be done by using the static method


<br />QIcon Qicon::fromTheme ( const QString &amp;name, const QIcon &amp;fallback = QIcon());<br />


For example, in the screenshot at the beginning of this chapter, the smiley comes from the Oxygen KDE icon theme and was set by


<br />button.setIcon(QIcon::fromTheme(&quot;face-smile&amp;quot;));<br />


h2. Qt class hiearchy
Qt widely uses inheritance, especially in the Widgets module. The following graph shows some of these inheritance
QObject inheritance tree
Doc:QObject is the most basic class in Qt. Most of classes in Qt inherit from this class. QObject provides some very powerful capabilities like :
* object name : you can set a name, as a string, to an object and search for objects by names.
* parenting system (described in the following section)
* signals and slots (described in the next chapter)
* event management
Widgets are able to respond to events and use parenting system and signals and slots mechanism. All widgets inherit from QObject. The most basic widget is the Doc:QWidget. QWidget contains most properties that are used to describe a window, or a widget, like position and size, mouse cursor, tooltips, etc.
Remark : in Qt, a widget can also be a window. In the previous section we displayed a button, that is a widget, but it appears directly as a window. There is no need of a "QWindow&quot; class.
Nearly all graphical elements inherit from QWidget. We can list for example
* QAbstractButton, a base class for all button types
' QPushButton
'
QCheckBox
' QRadioButton
* QFrame, that displays a frame
* QLabel, that displays text or picture

This inheritance is done in order to facilitate properties management. Shared properties like size are cursors can be used on other graphical components, and QAbstractButton provides basic properties that are shared by all buttons.
h2. Parenting system
Parenting system is a convenient way of dealing with objects in Qt, especially widgets. Any object that inherits from Doc:QObject can have a parent and children. This hierarchy tree makes many things convenient :
* When an object is destroyed, all of its children are destroyed as well. So calling delete becomes optional in certain cases
* All QObjects have findChild and findChildren methods that can be used to search for children of a given object.
* Child widgets in a Doc:QWidget automatically appear inside the parent widget.
The following snippet that creates a Doc:QPushButton inside a QPushButton:


<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);
<br /> QPushButton button1 (&quot;test&amp;quot;);<br /> QPushButton button2 (&quot;other&amp;quot;, &amp;button1);
<br /> button1.show();
<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br />


button_in_button
You can also note that when the application is closed, <code&gt;button1&lt;/code&gt;, which is allocated on the stack, is deallocated. Since <code&gt;button2&lt;/code&gt; has <code&gt;button1&lt;/code&gt; as a parent, it is deleted also. You can even test this in Qt Creator in the analyze section, by searching for a memory leak—there won't be any.
There is clearly no benefit in putting a button inside a button, but based on this idea, we might want to put buttons inside a container, that does not display anything. This container is simply the Doc:QWidget.
The following code is used to display a button inside a widget


<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);
<br /> QWidget window;<br /> window.setFixedSize(100, 50);
<br /> QPushButton *button = new QPushButton(&quot;Hello World&amp;quot;, &amp;window);<br /> button</s>&gt;setGeometry(10, 10, 80, 30);

window.show();

return app.exec&amp;amp;#40;&amp;#41;;

}<br />

Note that we create a fixed size widget (that acts as a window) using setFixedSize. This method has the following signature

<br />void QWidget::setFixedSize(int width, int height);<br />

We also positioned the button using setGeometry. This method has the following signature

<br />void QWidget::setGeometry(int x, int y, int width, int height);<br />

Subclassing QWidget

Until now, we have put all of our code in the main function. This was not a problem for our simple examples, but for more and more complex applications we might want to split our code into different classes. What is often done is to create a class that is used to display a window, and implement all the widgets that are contained in this window as attributes of this class.

Inside Qt Creator, you can automatically create a new class with File > New file or project > C++ > C++ Class

Create class dialog

Make the class inherit from QWidget, and you should obtain code similar to below

Header

<br />#ifndef WINDOW_H<br />#define WINDOW_H

#include &lt;QWidget&amp;gt;

class Window : public QWidget<br />{<br /> Q_OBJECT<br />public:<br /> explicit Window(QWidget *parent = 0);

signals:

public slots:

};

#endif // WINDOW_H<br />

Source

<br />#include &quot;window.h&amp;quot;

Window::Window(QWidget '''parent) :<br /> QWidget(parent)<br />{<br />}<br />


You can see that Qt Creator automatically generates a class template. Notice that there are some new elements in the header :
The Q_OBJECT macro.
* A new category of methods :
signals
* A new category of methods :
public slots

All these elements will be explained in the next chapter, and none of them are needed now.
Implementing the window is done in the constructor. We can declare the size of the window, as well as the widgets that this window contains and their positions. For example, implementing the previous window that contains a button can be done in this way :

main.cpp

<br />#include &lt;QApplication&amp;gt;<br />#include &quot;window.h&amp;quot;

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

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

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

window.h

<br />#ifndef WINDOW_H<br />#define WINDOW_H

#include &lt;QWidget&amp;gt;

class QPushButton;<br />class Window : public QWidget<br />{<br />public:<br /> explicit Window(QWidget *parent = 0);<br />private:<br /> QPushButton *m_button;<br />};

#endif // WINDOW_H<br />

window.cpp

<br />#include &quot;window.h&amp;quot;

#include &lt;QPushButton&amp;gt;

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

// Create and position the button<br /> m_button = new QPushButton(&quot;Hello World&amp;quot;, this);<br /> m_button-&gt;setGeometry(10, 10, 80, 30);<br />}<br />

Please note that there is no need for writing a destructor for deleting <code&gt;m_button&lt;/code&gt;. With the parenting system, when the <code&gt;Window&lt;/code&gt; instance is out of the stack, the <code&gt;m_button&lt;/code&gt; is automatically deleted.

See Also

A better overview of Doc:qpushbutton is given in this wiki page How to use QPushButton