Basic Qt Programming Tutorial: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[Basic Qt Programming Tutorial SimplifiedChinese|简体中文]] [[Basic Qt Programming Tutorial Persian|فارسی]] [[Temel Qt Programlama Egitimi|Türkçe]] [[Basic Qt Programming Tutorial Spanish|Español]]
[[Category:HowTo]]<br />[[Category:Developing_with_Qt::General]]<br />[[Category:Tutorial]]


=Introduction=
[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]


This tutorial will explain in detail how to take your first steps in programming with Qt using the Qt Creator integrated development environment (<span class="caps">IDE</span>).
'''English''' [[Basic_Qt_Programming_Tutorial_SimplifiedChinese|简体中文]] [[Basic_Qt_Programming_Tutorial_Persian|فارسی]] [[Temel_Qt_Programlama_Egitimi|Türkçe]] [[Basic_Qt_Programming_Tutorial_Spanish|Español]]


If you want to learn how to make powerful <span class="caps">GUI</span>s with all the latest fancy technologies, this is not the tutorial for you. This is firmly intended as a gentle introduction to help beginners get up and running without scaring them.
= Introduction =


We will begin by creating a new Qt-based project and modifying the generated code to show a very simple graphical user interface (<span class="caps">GUI</span>). Once our basic application project is in place and running, we will go back and modify it to do some slightly useful things.
This tutorial will explain in detail how to take your first steps in programming with Qt using the Qt Creator integrated development environment (IDE).
 
If you want to learn how to make powerful GUIs with all the latest fancy technologies, this is not the tutorial for you. This is firmly intended as a gentle introduction to help beginners get up and running without scaring them.
 
We will begin by creating a new Qt-based project and modifying the generated code to show a very simple graphical user interface (GUI). Once our basic application project is in place and running, we will go back and modify it to do some slightly useful things.


We will start off simple and build up in complexity as you get more familiar with the widgets and other facilities at your disposal.
We will start off simple and build up in complexity as you get more familiar with the widgets and other facilities at your disposal.


So, grab yourself a copy of the Qt <span class="caps">SDK</span> or if you are on Linux the system-provided copy of Qt and a compiler and let’s get started!
So, grab yourself a copy of the Qt SDK or if you are on Linux the system-provided copy of Qt and a compiler and let's get started!


==Baby steps: Creating a new project==
== Baby steps: Creating a new project ==


Let’s try making a trivial application that has a single window that shows a QLabel and a QLineEdit. To do this follow these simple steps:
Let's try making a trivial application that has a single window that shows a QLabel and a QLineEdit. To do this follow these simple steps:


# Start up Qt Creator.<br />[http://gallery.theharmers.co.uk/picture.php?/22/category/7 [[Image:TN-20110503152033-f24040e3.jpg|Start Qt-Creator]]] ''[gallery.theharmers.co.uk]''
# Start up Qt Creator.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503152033-f24040e3.jpg|Start Qt-Creator]]&quot;:http://gallery.theharmers.co.uk/picture.php?/22/category/7
# Go to File-&gt;“New File or Project…” menu entry.
# Go to File-&gt;&quot;New File or Project…&quot; menu entry.
# Choose Qt Gui Application and choose a name for it.<br />[http://gallery.theharmers.co.uk/picture.php?/23/category/7 [[Image:TN-20110503152034-ea9bb58a.jpg|Select project type]]] ''[gallery.theharmers.co.uk]''
# Choose Qt Gui Application and choose a name for it.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503152034-ea9bb58a.jpg|Select project type]]&quot;:http://gallery.theharmers.co.uk/picture.php?/23/category/7
# Enter a project name, “qt-tutorial-01”, say.<br />[http://gallery.theharmers.co.uk/picture.php?/24/category/7 [[Image:TN-20110503152034-8dabd337.jpg|Choose project name]]] ''[gallery.theharmers.co.uk]''
# Enter a project name, &quot;qt-tutorial-01&amp;quot;, say.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503152034-8dabd337.jpg|Choose project name]]&quot;:http://gallery.theharmers.co.uk/picture.php?/24/category/7
# Select one or more versions of Qt to target. A desktop build is fine for this tutorial.<br />[http://gallery.theharmers.co.uk/picture.php?/25/category/7 [[Image:TN-20110503152034-35f9e7cb.jpg|Choose Qt build to target]]] ''[gallery.theharmers.co.uk]''
# Select one or more versions of Qt to target. A desktop build is fine for this tutorial.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503152034-35f9e7cb.jpg|Choose Qt build to target]]&quot;:http://gallery.theharmers.co.uk/picture.php?/25/category/7
# Select the base class to be QWidget (leave the class name as Widget which is the default).<br />[http://gallery.theharmers.co.uk/picture.php?/26/category/7 [[Image:TN-20110503152034-26f92911.jpg|Select project generation options]]] ''[gallery.theharmers.co.uk]''
# Select the base class to be QWidget (leave the class name as Widget which is the default).<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503152034-26f92911.jpg|Select project generation options]]&quot;:http://gallery.theharmers.co.uk/picture.php?/26/category/7
# Check project creation options on summary and click “Finish”.<br />[http://gallery.theharmers.co.uk/picture.php?/27/category/7 [[Image:TN-20110503152034-95ce9f30.jpg|Check project summary]]] ''[gallery.theharmers.co.uk]''
# Check project creation options on summary and click &quot;Finish&amp;quot;.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503152034-95ce9f30.jpg|Check project summary]]&quot;:http://gallery.theharmers.co.uk/picture.php?/27/category/7


The above will create you a simple project consisting of four files:
The above will create you a simple project consisting of four files:
Line 32: Line 36:
* widget.ui
* widget.ui


==Learning to crawl: Editing the project files==
== Learning to crawl: Editing the project files ==


We will edit the widget.ui file first so:
We will edit the widget.ui file first so:


# Click on that and designer will switch to design mode and open up the file. You should see a blank widget. Now do this:<br />[http://gallery.theharmers.co.uk/picture.php?/28/category/7 [[Image:TN-20110503154826-af1ea190.jpg|Open designer]]] ''[gallery.theharmers.co.uk]''
# Click on that and designer will switch to design mode and open up the file. You should see a blank widget. Now do this:<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503154826-af1ea190.jpg|Open designer]]&quot;:http://gallery.theharmers.co.uk/picture.php?/28/category/7
# Using the toolbox on the left, drag a Label onto the widget form<br />[http://gallery.theharmers.co.uk/picture.php?/29/category/7 [[Image:TN-20110503154826-661b1ceb.jpg|Add label]]] ''[gallery.theharmers.co.uk]''
# Using the toolbox on the left, drag a Label onto the widget form<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503154826-661b1ceb.jpg|Add label]]&quot;:http://gallery.theharmers.co.uk/picture.php?/29/category/7
# Do similarly for a Line Edit and place it to the right of the Label. The exact position is not important.<br />[http://gallery.theharmers.co.uk/picture.php?/30/category/7 [[Image:TN-20110503154826-8c16787b.jpg|Add line edit]]] ''[gallery.theharmers.co.uk]''
# Do similarly for a Line Edit and place it to the right of the Label. The exact position is not important.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503154826-8c16787b.jpg|Add line edit]]&quot;:http://gallery.theharmers.co.uk/picture.php?/30/category/7
# Click on the widget background so that both of your new widgets (the label and line edit) get deselected.<br />[http://gallery.theharmers.co.uk/picture.php?/31/category/7 [[Image:TN-20110503154826-5084f96c.jpg|Select parent widget]]] ''[gallery.theharmers.co.uk]''
# Click on the widget background so that both of your new widgets (the label and line edit) get deselected.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503154826-5084f96c.jpg|Select parent widget]]&quot;:http://gallery.theharmers.co.uk/picture.php?/31/category/7
# In the toolbar at the top click on the “Lay out Horizontally” button or press Ctrl-H to add all widgets to a horizontal layout. The layout will take care of resizing your widgets for you if the parent widget’s size changes.<br />[http://gallery.theharmers.co.uk/picture.php?/32/category/7 [[Image:TN-20110503154826-01e7e207.jpg|Apply horizontal layout]]] ''[gallery.theharmers.co.uk]''
# In the toolbar at the top click on the &quot;Lay out Horizontally&amp;quot; button or press Ctrl-H to add all widgets to a horizontal layout. The layout will take care of resizing your widgets for you if the parent widget's size changes.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503154826-01e7e207.jpg|Apply horizontal layout]]&quot;:http://gallery.theharmers.co.uk/picture.php?/32/category/7
# Double click on the Label and it will switch to edit mode. Change the text to “My name is:<br />[http://gallery.theharmers.co.uk/picture.php?/33/category/7 [[Image:TN-20110503154826-4e8b7939.jpg|Change label text]]] ''[gallery.theharmers.co.uk]''
# Double click on the Label and it will switch to edit mode. Change the text to &quot;My name is:&quot;<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503154826-4e8b7939.jpg|Change label text]]&quot;:http://gallery.theharmers.co.uk/picture.php?/33/category/7
# Press Ctrl-S to save the form.
# Press Ctrl-S to save the form.
# Click on the Edit mode button in the left hand panel of creator to switch back to the text editor. You will probably see the raw xml content of the UI file at this point. Just close it we are done with it for now.<br />[http://gallery.theharmers.co.uk/picture.php?/34/category/7 [[Image:TN-20110503154826-abad6378.jpg|Switch to Edit mode]]] ''[gallery.theharmers.co.uk]''
# Click on the Edit mode button in the left hand panel of creator to switch back to the text editor. You will probably see the raw xml content of the UI file at this point. Just close it we are done with it for now.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503154826-abad6378.jpg|Switch to Edit mode]]&quot;:http://gallery.theharmers.co.uk/picture.php?/34/category/7


Now open up the widget.h file and edit it so that it looks like this:
Now open up the widget.h file and edit it so that it looks like this:


Now edit the corresponding .cpp file to look like this:
<code><br />#ifndef WIDGET_H<br />#define WIDGET_H
 
#include &lt;QWidget&amp;gt;
 
namespace Ui {<br /> class Widget;<br />}
 
class Widget : public QWidget<br />{<br /> Q_OBJECT


Finally edit main.cpp to this:
public:<br /> explicit Widget(QWidget *parent = 0);<br /> ~Widget();


==Up and running: Building and running the application==
void setName(const QString &amp;name);<br /> QString name() const;


Now build (Hammer icon in lower left or default shortcut of Ctrl-Shift-B) and run the application (green “play” icon in lower left corner). You will see some compiler messages go past in the “Compile Output” panel at the bottom whilst building.<br />[http://gallery.theharmers.co.uk/picture.php?/35/category/7 [[Image:TN-20110503160137-3e61f249.jpg|Compiling the project]]] ''[gallery.theharmers.co.uk]''
private:<br /> Ui::Widget *ui;<br />};


This is what the application looks like when it is executed:<br />[http://gallery.theharmers.co.uk/picture.php?/36/category/7 [[Image:TN-20110503160137-aac48922.jpg|The running application]]] ''[gallery.theharmers.co.uk]''
#endif // WIDGET_H<br /></code>


As you can see the main() function is very simple. All we do is create a [http://doc.qt.nokia.com/latest/qapplication.html QApplication] ''[doc.qt.nokia.com]'' and then a Widget (this is our custom widget that we layed out in designer and added custom behaviour to in code with the name() and setName() functions).
Now edit the corresponding .cpp file to look like this:


We then call our custom setName function on the widget. This in turn gets a pointer to the [http://doc.qt.nokia.com/latest/qlineedit.html QLineEdit] ''[doc.qt.nokia.com]'' widget that we placed on the form and calls the [http://doc.qt.nokia.com/latest/qlineedit.html#text-prop setText()] ''[doc.qt.nokia.com]''() function of QLineEdit.
<code><br />#include &quot;widget.h&amp;quot;<br />#include &quot;ui_widget.h&amp;quot;


Finally we show the widget and enter the event loop by calling a.exec().
Widget::Widget(QWidget *parent) :<br /> QWidget(parent),<br /> ui(new Ui::Widget)<br />{<br /> ui-&gt;setupUi(this);<br />}


Once you understand how this simple app works then you can start adding some more bells and whistles like signal/slot connections.
Widget::~Widget()<br />{<br /> delete ui;<br />}


==See also==
void Widget::setName(const QString &amp;name)<br />{<br /> ui-&gt;lineEdit-&gt;setText(name);<br />}


* [[Introduction to Qt Quick for Cpp developers|“Introduction to Qt Quick for C++ Developers”]]<br />''Note:'' The paper above is deprecated. Its content is now part of [[Introduction to Qt Quick|“Introduction to Qt Quick”]].
QString Widget::name() const<br />{<br /> return ui-&gt;lineEdit-&gt;text();<br />}<br /></code>
* [[How to Use QPushButton|“How to Use QPushButton”]]
 
* [[How-to-use-signals-and-slots|“How to use signals and slots”]]
Finally edit main.cpp to this:


===Categories:===
<code><br />#include &lt;QApplication&amp;gt;<br />#include &quot;widget.h&amp;quot;


* [[:Category:Developing with Qt|Developing_with_Qt]]
int main(int argc, char '''argv[])<br />{<br /> QApplication a(argc, argv);<br /> Widget w;
** [[:Category:Developing with Qt::General|General]]
<br /> w.setName(&quot;Slim Shady&amp;quot;);
* [[:Category:HowTo|HowTo]]
<br /> w.show();
* [[:Category:Tutorial|Tutorial]]
<br /> return a.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
<br />h2. Up and running: Building and running the application
<br />Now build (Hammer icon in lower left or default shortcut of Ctrl-Shift-B) and run the application (green &quot;play&amp;quot; icon in lower left corner). You will see some compiler messages go past in the &quot;Compile Output&amp;quot; panel at the bottom whilst building.<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503160137-3e61f249.jpg|Compiling the project]]&quot;:http://gallery.theharmers.co.uk/picture.php?/35/category/7
<br />This is what the application looks like when it is executed:<br />&quot;[[Image:http://gallery.theharmers.co.uk/upload/2011/05/03/thumbnail/TN-20110503160137-aac48922.jpg|The running application]]&quot;:http://gallery.theharmers.co.uk/picture.php?/36/category/7
<br />As you can see the main() function is very simple. All we do is create a &quot;QApplication&amp;quot;:http://doc.qt.nokia.com/latest/qapplication.html and then a Widget (this is our custom widget that we layed out in designer and added custom behaviour to in code with the name() and setName() functions).
<br />We then &quot;just call&amp;quot;:http://www.fixithere.net/sky-customer-service/ our custom setName function on the widget. This in turn gets a pointer to the &quot;QLineEdit&amp;quot;:http://doc.qt.nokia.com/latest/qlineedit.html widget that we placed on the form and calls the &quot;setText()&quot;:http://doc.qt.nokia.com/latest/qlineedit.html#text-prop() function of QLineEdit.
<br />Finally we show the widget and enter the event loop by calling a.exec&amp;amp;#40;&amp;#41;.
<br />Once you understand how this simple app works then you can start adding some more bells and whistles like signal/slot connections.
<br />h2. See also
<br />''' [[Introduction_to_Qt_Quick_for_Cpp_developers|&quot;Introduction to Qt Quick for C++ Developers&amp;quot;]]<br />''Note:'' The paper above is deprecated. Its content is now part of [[Introduction_to_Qt_Quick|&quot;Introduction to Qt Quick&amp;quot;]].<br />* [[How_to_Use_QPushButton|&quot;How to Use QPushButton&amp;quot;]]

Revision as of 14:08, 23 February 2015



[toc align_right="yes&quot; depth="2&quot;]

English 简体中文 فارسی Türkçe Español

Introduction

This tutorial will explain in detail how to take your first steps in programming with Qt using the Qt Creator integrated development environment (IDE).

If you want to learn how to make powerful GUIs with all the latest fancy technologies, this is not the tutorial for you. This is firmly intended as a gentle introduction to help beginners get up and running without scaring them.

We will begin by creating a new Qt-based project and modifying the generated code to show a very simple graphical user interface (GUI). Once our basic application project is in place and running, we will go back and modify it to do some slightly useful things.

We will start off simple and build up in complexity as you get more familiar with the widgets and other facilities at your disposal.

So, grab yourself a copy of the Qt SDK or if you are on Linux the system-provided copy of Qt and a compiler and let's get started!

Baby steps: Creating a new project

Let's try making a trivial application that has a single window that shows a QLabel and a QLineEdit. To do this follow these simple steps:

  1. Start up Qt Creator.
    "Start Qt-Creator":http://gallery.theharmers.co.uk/picture.php?/22/category/7
  2. Go to File->"New File or Project…" menu entry.
  3. Choose Qt Gui Application and choose a name for it.
    "Select project type":http://gallery.theharmers.co.uk/picture.php?/23/category/7
  4. Enter a project name, "qt-tutorial-01&quot;, say.
    "Choose project name":http://gallery.theharmers.co.uk/picture.php?/24/category/7
  5. Select one or more versions of Qt to target. A desktop build is fine for this tutorial.
    "Choose Qt build to target":http://gallery.theharmers.co.uk/picture.php?/25/category/7
  6. Select the base class to be QWidget (leave the class name as Widget which is the default).
    "Select project generation options":http://gallery.theharmers.co.uk/picture.php?/26/category/7
  7. Check project creation options on summary and click "Finish&quot;.
    "Check project summary":http://gallery.theharmers.co.uk/picture.php?/27/category/7

The above will create you a simple project consisting of four files:

  • main.cpp
  • widget.h
  • widget.cpp
  • widget.ui

Learning to crawl: Editing the project files

We will edit the widget.ui file first so:

  1. Click on that and designer will switch to design mode and open up the file. You should see a blank widget. Now do this:
    "Open designer":http://gallery.theharmers.co.uk/picture.php?/28/category/7
  2. Using the toolbox on the left, drag a Label onto the widget form
    "Add label":http://gallery.theharmers.co.uk/picture.php?/29/category/7
  3. Do similarly for a Line Edit and place it to the right of the Label. The exact position is not important.
    "Add line edit":http://gallery.theharmers.co.uk/picture.php?/30/category/7
  4. Click on the widget background so that both of your new widgets (the label and line edit) get deselected.
    "Select parent widget":http://gallery.theharmers.co.uk/picture.php?/31/category/7
  5. In the toolbar at the top click on the "Lay out Horizontally&quot; button or press Ctrl-H to add all widgets to a horizontal layout. The layout will take care of resizing your widgets for you if the parent widget's size changes.
    "Apply horizontal layout":http://gallery.theharmers.co.uk/picture.php?/32/category/7
  6. Double click on the Label and it will switch to edit mode. Change the text to "My name is:"
    "Change label text":http://gallery.theharmers.co.uk/picture.php?/33/category/7
  7. Press Ctrl-S to save the form.
  8. Click on the Edit mode button in the left hand panel of creator to switch back to the text editor. You will probably see the raw xml content of the UI file at this point. Just close it we are done with it for now.
    "Switch to Edit mode":http://gallery.theharmers.co.uk/picture.php?/34/category/7

Now open up the widget.h file and edit it so that it looks like this:

<br />#ifndef WIDGET_H<br />#define WIDGET_H

#include &lt;QWidget&amp;gt;

namespace Ui {<br /> class Widget;<br />}

class Widget : public QWidget<br />{<br /> Q_OBJECT

public:<br /> explicit Widget(QWidget *parent = 0);<br /> ~Widget();

void setName(const QString &amp;name);<br /> QString name() const;

private:<br /> Ui::Widget *ui;<br />};

#endif // WIDGET_H<br />

Now edit the corresponding .cpp file to look like this:

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

Widget::Widget(QWidget *parent) :<br /> QWidget(parent),<br /> ui(new Ui::Widget)<br />{<br /> ui-&gt;setupUi(this);<br />}

Widget::~Widget()<br />{<br /> delete ui;<br />}

void Widget::setName(const QString &amp;name)<br />{<br /> ui-&gt;lineEdit-&gt;setText(name);<br />}

QString Widget::name() const<br />{<br /> return ui-&gt;lineEdit-&gt;text();<br />}<br />

Finally edit main.cpp to this:

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

int main(int argc, char '''argv[])<br />{<br /> QApplication a(argc, argv);<br /> Widget w;
<br /> w.setName(&quot;Slim Shady&amp;quot;);
<br /> w.show();
<br /> return a.exec&amp;amp;#40;&amp;#41;;<br />}<br />


h2. Up and running: Building and running the application
Now build (Hammer icon in lower left or default shortcut of Ctrl-Shift-B) and run the application (green "play&quot; icon in lower left corner). You will see some compiler messages go past in the "Compile Output&quot; panel at the bottom whilst building.
"Compiling the project":http://gallery.theharmers.co.uk/picture.php?/35/category/7
This is what the application looks like when it is executed:
"The running application":http://gallery.theharmers.co.uk/picture.php?/36/category/7
As you can see the main() function is very simple. All we do is create a "QApplication&quot;:http://doc.qt.nokia.com/latest/qapplication.html and then a Widget (this is our custom widget that we layed out in designer and added custom behaviour to in code with the name() and setName() functions).
We then "just call&quot;:http://www.fixithere.net/sky-customer-service/ our custom setName function on the widget. This in turn gets a pointer to the "QLineEdit&quot;:http://doc.qt.nokia.com/latest/qlineedit.html widget that we placed on the form and calls the "setText()":http://doc.qt.nokia.com/latest/qlineedit.html#text-prop() function of QLineEdit.
Finally we show the widget and enter the event loop by calling a.exec&amp;#40;&#41;.
Once you understand how this simple app works then you can start adding some more bells and whistles like signal/slot connections.
h2. See also
"Introduction to Qt Quick for C++ Developers&quot;
Note: The paper above is deprecated. Its content is now part of "Introduction to Qt Quick&quot;.
* "How to Use QPushButton&quot;