Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > 5ab010e37991249ab4adaa24d6e39c6e > files > 51

qt5-qtdoc-5.1.1-2.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- gettingstartedqt.qdoc -->
  <title>Getting Started Programming with Qt Widgets | QtDoc 5.1</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader"></div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#creating-the-notepad-project">Creating the Notepad Project</a></li>
<li class="level1"><a href="#main-source-file">Main Source File</a></li>
<li class="level1"><a href="#designing-a-ui">Designing a UI</a></li>
<li class="level2"><a href="#using-qt-designer">Using Qt Designer</a></li>
<li class="level2"><a href="#notepad-header-file">Notepad Header File</a></li>
<li class="level2"><a href="#notepad-source-file">Notepad Source File</a></li>
<li class="level2"><a href="#project-file">Project File</a></li>
<li class="level1"><a href="#adding-user-interaction">Adding User Interaction</a></li>
<li class="level2"><a href="#adding-push-buttons">Adding Push Buttons</a></li>
<li class="level2"><a href="#adding-menu-items">Adding Menu Items</a></li>
<li class="level2"><a href="#opening-files">Opening Files</a></li>
<li class="level2"><a href="#saving-files">Saving Files</a></li>
<li class="level1"><a href="#building-and-running-notepad">Building and Running Notepad</a></li>
<li class="level2"><a href="#building-and-running-from-the-command-line">Building and Running from the Command Line</a></li>
</ul>
</div>
<h1 class="title">Getting Started Programming with Qt Widgets</h1>
<span class="subtitle"></span>
<!-- $$$gettingstartedqt.html-description -->
<div class="descr"> <a name="details"></a>
<p>In this topic, we teach basic Qt knowledge by implementing a simple Notepad application using C++ and the Qt Widgets module. We use the Qt Creator IDE and Qt Designer to generate some code, but you could also write all the code yourself.</p>
<p>After reading this topic, you are ready to refer to our overviews and API documentation, to find the information you need for the application you are developing.</p>
<p>In this topic, we first use Qt Creator to create a project with the necessary files. Then we use Qt Designer to modify the user interface files to show a text edit and a push button in a window on the desktop. This represents a simple Qt application that has a GUI. Finally, we add user interaction to the application by creating actions for opening and saving files.</p>
<p class="centerAlign"><img src="images/gs1.png" alt="&quot;Notepad application&quot;" /></p><p>You can find the final Notepad source files in the qtdoc repository in the snippets/widgets-tutorial/notepad directory. You can either fetch the Qt 5 sources from Qt Project or install them as part of Qt 5.</p>
<a name="creating-the-notepad-project"></a>
<h2>Creating the Notepad Project</h2>
<p>Setting up a new project in Qt Creator is aided by a wizard that guides you step-by-step through the project creation process. The wizard prompts you to enter the settings needed for that particular type of project and creates the project for you.</p>
<p class="centerAlign"><img src="images/gs-project1.png" alt="&quot;Qt Creator New File or Project dialog&quot;" /></p><p>To create the Notepad project, select <b>File</b> &gt; <b>New File or Project</b> &gt; <b>Applications</b> &gt; <b>Qt Gui Application</b> &gt; <b>Choose</b>, and follow the instructions of the wizard. In the <b>Class Information</b> dialog, type <b>Notepad</b> as the class name and select <b>QMainWindow</b> as the base class.</p>
<p class="centerAlign"><img src="images/gs-project2.png" alt="&quot;Class Information Dialog&quot;" /></p><p>The <b>Qt Gui Application</b> wizard creates a project that contains a main source file and a set of files that specify a user interface (Notepad widget):</p>
<ul>
<li>notepad.pro - the project file.</li>
<li>main.cpp - the main source file for the application.</li>
<li>notepad.cpp - the source file of the notepad class of the Notepad widget.</li>
<li>notepad.h - the header file of the notepad class for the Notepad widget.</li>
<li>notepad.ui - the UI form for the Notepad widget.</li>
</ul>
<p>The .cpp, .h, and .ui files come with the necessary boiler plate code for you to be able to build and run the project. The .pro file is complete. We will take a closer look at the file contents in the following sections.</p>
<p><b>Learn More</b></p>
<table class="generic">
 <thead><tr class="qt-style"><th >About</th><th >Here</th></tr></thead>
<tr valign="top" class="odd"><td >Using Qt Creator</td><td ><a href="http://qt-project.org/doc/qtcreator/index.html">Qt Creator</a></td></tr>
<tr valign="top" class="even"><td >Creating other kind of applications with Qt Creator</td><td >Qt Creator Tutorials</td></tr>
</table>
<a name="main-source-file"></a>
<h2>Main Source File</h2>
<p>The wizard generates the following code in the main.cpp file:</p>
<pre class="cpp"><span class="preprocessor">#include &quot;notepad.h&quot;</span>
<span class="preprocessor">#include &lt;QApplication&gt;</span>

<span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
{
    <span class="type">QApplication</span> a(argc<span class="operator">,</span> argv);
    Notepad w;
    w<span class="operator">.</span>show();

    <span class="keyword">return</span> a<span class="operator">.</span>exec();
}</pre>
<p>Let us go through the code line by line. On the first two lines, we include the header files for the Notepad widget and QApplication. All Qt classes have a header file named after them.</p>
<p>Line 4 defines the main function that is the entry point for all C and C++ based applications.</p>
<p>Line 6 creates a QApplication object. This object manages application-wide resources and is necessary to run any Qt program that uses Qt Widgets. It constructs an application object with <tt>argc</tt> command line arguments run in <tt>argv</tt>. (For GUI applications that do not use Qt Widgets, you can use QGuiApplication instead.)</p>
<p>Line 7 creates the Notepad object. This is the object for which the wizard created the class and the UI file. The user interface contains visual elements that are called <tt>widgets</tt> in Qt. Examples of widgets are text edits, scroll bars, labels, and radio buttons. A widget can also be a container for other widgets; a dialog or a main application window, for example.</p>
<p>Line 8 shows the Notepad widget on the screen in its own window. Since widgets also function as containers (for instance a QMainWindow, which has toolbars, menus, a status bar, and a few other widgets), it is possible to show a single widget in its own window. Widgets are not visible by default; the function show() makes the widget visible.</p>
<p>Line 10 makes the QApplication enter its event loop. When a Qt application is running, events are generated and sent to the widgets of the application. Examples of events are mouse presses and key strokes.</p>
<p><b>Learn More</b></p>
<table class="generic">
 <thead><tr class="qt-style"><th >About</th><th >Here</th></tr></thead>
<tr valign="top" class="odd"><td >Widgets and Window Geometry</td><td >Window and Dialog Widgets</td></tr>
<tr valign="top" class="even"><td >Events and event handling</td><td >The Event System</td></tr>
</table>
<a name="designing-a-ui"></a>
<h2>Designing a UI</h2>
<p>The wizard generates a user interface definition in XML format, notepad.ui. When you open the notepad.ui file in Qt Creator, it automatically opens in the integrated Qt Designer.</p>
<p>When you build the application, Qt Creator launches the Qt <a href="uic.html">User Interface Compiler (uic)</a> that reads the .ui file and creates a corresponding C++ header file, ui_notepad.h.</p>
<a name="using-qt-designer"></a>
<h3>Using Qt Designer</h3>
<p>The wizard creates an application that uses a QMainWindow. It has its own layout to which you can add a menu bar, dock widgets, tool bars, and a status bar. The center area can be occupied by any kind of widget. The wizard places the Notepad widget there.</p>
<p>Let us use Qt Designer to add a QTextEdit object and a QPushButton object to the main window. When you type text in the text edit widget, it receives key pressed events and responds by drawing the text typed. The button will exit the Notepad application when pushed (that is, clicked with the mouse).</p>
<p>To add widgets in Qt Designer:</p>
<ol class="1">
<li>In the Qt Creator <b>Editor</b> mode, double-click the notepad.ui file in the <b>Projects</b> view to launch the file in the integrated Qt Designer.</li>
<li>Drag and drop the following widgets to the form:<ul>
<li>Text Edit (QTextEdit)</li>
<li>Push Button (QPushButton)</li>
</ul>
</li>
<li>Double-click the <b>Push Button</b> widget and enter the text <b>Quit</b>.</li>
<li>In the <b>Properties</b> pane, change the value of <b>objectName</b> to <b>quitButton</b>.</li>
<li>Press <b>Ctrl+A</b> (or <b>Cmd+A</b>) to select the widgets and click <b>Lay out Vertically</b> (or press <b>Ctrl+L</b>) to apply a vertical layout (QVBoxLayout).</li>
<li>Press <b>Ctrl+S</b> (or <b>Cmd+S</b>) to save your changes.</li>
</ol>
<p>The UI now looks as follows in Qt Designer:</p>
<p class="centerAlign"><img src="images/gs2.png" alt="" /></p><p>You can view the generated XML file in the code editor:</p>
<pre class="qml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;ui version=&quot;4.0&quot;&gt;
 &lt;class&gt;Notepad&lt;/class&gt;
 &lt;widget class=&quot;QMainWindow&quot; name=&quot;Notepad&quot;&gt;
  &lt;property name=&quot;geometry&quot;&gt;
   &lt;rect&gt;
    &lt;x&gt;0&lt;/x&gt;
    &lt;y&gt;0&lt;/y&gt;
    &lt;width&gt;400&lt;/width&gt;
    &lt;height&gt;300&lt;/height&gt;
   &lt;/rect&gt;
  &lt;/property&gt;
  &lt;property name=&quot;windowTitle&quot;&gt;
   &lt;string&gt;Notepad&lt;/string&gt;
  &lt;/property&gt;
  &lt;widget class=&quot;QWidget&quot; name=&quot;centralWidget&quot;&gt;
   &lt;widget class=&quot;QWidget&quot; name=&quot;&quot;&gt;
    &lt;property name=&quot;geometry&quot;&gt;
     &lt;rect&gt;
      &lt;x&gt;70&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;268&lt;/width&gt;
      &lt;height&gt;235&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;layout class=&quot;QVBoxLayout&quot; name=&quot;verticalLayout&quot;&gt;
     &lt;item&gt;
      &lt;widget class=&quot;QTextEdit&quot; name=&quot;textEdit&quot;/&gt;
     &lt;/item&gt;
     &lt;item&gt;
      &lt;widget class=&quot;QPushButton&quot; name=&quot;quitButton&quot;&gt;
       &lt;property name=&quot;text&quot;&gt;
        &lt;string&gt;Quit&lt;/string&gt;
       &lt;/property&gt;
      &lt;/widget&gt;
     &lt;/item&gt;
    &lt;/layout&gt;
   &lt;/widget&gt;
  &lt;/widget&gt;
  &lt;widget class=&quot;QMenuBar&quot; name=&quot;menuBar&quot;&gt;
    ...</pre>
<p>Line 1 contains the XML declaration, which specifies the XML version and character encoding used in the document.</p>
<p>Line 2 creates an <tt>ui</tt> element that defines a Notepad widget.</p>
<p>Line 26 creates a QVBoxLayout widget that contains a QTextEdit and QPushButton widget. As mentioned, widgets can contain other widgets. It is possible to set the bounds (the location and size) of child widgets directly, but it is usually easier to use a layout. A layout manages the bounds of a widget's children. QVBoxLayout, for instance, places the children in a vertical row.</p>
<p>The UI file is used together with the header and source file of the Notepad class. We will look at the rest of the UI file in the later sections.</p>
<a name="notepad-header-file"></a>
<h3>Notepad Header File</h3>
<p>The wizard generated a header file for the Notepad class that has the necessary #includes, a constructor, a destructor, and the Ui object. The file looks as follows:</p>
<pre class="cpp"><span class="preprocessor">#include &lt;QMainWindow&gt;</span>

<span class="keyword">namespace</span> Ui {
<span class="keyword">class</span> Notepad;
}

<span class="keyword">class</span> Notepad : <span class="keyword">public</span> <span class="type">QMainWindow</span>
{
    Q_OBJECT

<span class="keyword">public</span>:
    <span class="keyword">explicit</span> Notepad(<span class="type">QWidget</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
    <span class="operator">~</span>Notepad();

<span class="keyword">private</span>:
    Ui<span class="operator">::</span>Notepad <span class="operator">*</span>ui;
};</pre>
<p>Line 4 includes QMainWindow that provides a main application window.</p>
<p>Line 6 declares the Notepad class in the Ui namespace, which is the standard namespace for the UI classes generated from .ui files by the <tt>uic</tt> tool.</p>
<p>Line 10 contains the <tt>Q_OBJECT</tt> macro. It must come first in the class definition, and declares our class as a QObject. Naturally, it must also inherit from QObject. A QObject adds several abilities to a normal C++ class. Notably, the class name and slot names can be queried at run-time. It is also possible to query a slot's parameter types and invoke it.</p>
<p>Line 15 declares a constructor that has a default argument called <tt>parent</tt>. The value 0 indicates that the widget has no parent (it is a top-level widget).</p>
<p>Line 16 declares a virtual destructor to free the resources that were acquired by the object during its life-cycle. According to the C++ naming convention, destructors have the same name as the class they are associated with, prefixed with a tilde (~). In QObject, destructors are virtual to ensure that the destructors of derived classes are invoked properly when an object is deleted through a pointer-to-base-class.</p>
<p>Line 19 declares a member variable which is a pointer to the Notepad UI class. A member variable is associated with a specific class, and accessible for all its methods.</p>
<a name="notepad-source-file"></a>
<h3>Notepad Source File</h3>
<p>The source file that the wizard generated for the Notepad class looks as follows:</p>
<pre class="cpp"><span class="preprocessor">#include &quot;notepad.h&quot;</span>
<span class="preprocessor">#include &quot;ui_notepad.h&quot;</span>

Notepad<span class="operator">::</span>Notepad(<span class="type">QWidget</span> <span class="operator">*</span>parent) :
    <span class="type">QMainWindow</span>(parent)<span class="operator">,</span>
    ui(<span class="keyword">new</span> Ui<span class="operator">::</span>Notepad)
{
    ui<span class="operator">-</span><span class="operator">&gt;</span>setupUi(<span class="keyword">this</span>);
}

Notepad<span class="operator">::</span><span class="operator">~</span>Notepad()
{
    <span class="keyword">delete</span> ui;
}</pre>
<p>The first two lines include the Notepad class header file that was generated by the wizard and the UI header file that was generated by the <tt>uic</tt> tool.</p>
<p>Line 4 defines the <tt>Notepad</tt> constructor and sets up the UI file.</p>
<p>Line 5 calls the QMainWindow constructor, which is the base class for the Notepad class.</p>
<p>Line 6 creates the UI class instance and assigns it to the <tt>ui</tt> member.</p>
<p>Line 8 sets up the UI.</p>
<p>Line 11 destructs the <tt>ui</tt>.</p>
<a name="project-file"></a>
<h3>Project File</h3>
<p>The wizard generates the following project file, <tt>notepad.pro</tt>, for us:</p>
<pre class="cpp">QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Notepad
TEMPLATE = app

SOURCES += main.cpp\
        notepad.cpp

HEADERS  += notepad.h

FORMS    += notepad.ui</pre>
<p>The project file specifies the application name and the <tt>qmake</tt> template to use for generating the project, as well as the source, header, and UI files included in the project.</p>
<p>You could also use <tt>qmake</tt>'s <tt>-project</tt> option to generate the .pro file. Although, in that case, you have to remember to add the line <tt>QT += widgets</tt> to the generated file in order to link against the Qt Widgets Module.</p>
<p><b>Learn More</b></p>
<table class="generic">
 <thead><tr class="qt-style"><th >About</th><th >Here</th></tr></thead>
<tr valign="top" class="odd"><td >Using Qt Designer</td><td >Qt Designer Manual</td></tr>
<tr valign="top" class="even"><td >Layouts</td><td >Layout Management, Widgets and Layouts, <a href="examples-layouts.html">Layout Examples</a></td></tr>
<tr valign="top" class="odd"><td >The widgets that come with Qt</td><td >Qt Widget Gallery</td></tr>
<tr valign="top" class="even"><td >Main windows and main window classes</td><td >Application Main Window, Main Window Examples</td></tr>
<tr valign="top" class="odd"><td >QObjects and the Qt Object model (This is essential to understand Qt)</td><td >Object Model</td></tr>
<tr valign="top" class="even"><td >qmake and the Qt build system</td><td >qmake Manual</td></tr>
</table>
<a name="adding-user-interaction"></a>
<h2>Adding User Interaction</h2>
<p>We now have a user interface, but it does not really do anything useful, as it only contains a text edit and a push button, as well as some standard functions for quitting, minimizing and maximizing the application. To make the application useful, we will add user interaction to it. First, we will add functionality to the push button. Second, we will add functions for loading a file to the text edit and for saving the contents of the text edit as a file.</p>
<a name="adding-push-buttons"></a>
<h3>Adding Push Buttons</h3>
<p>Most desktop operating systems have standard ways of enabling users to quit applications. However, in this example we use this basic function to illustrate how you can add user interaction to applications. To do this, we add a slot that we connect to the <b>Quit button</b>.</p>
<p>To exit the application when the <b>Quit</b> button is pushed, you use the Qt signals and slots mechanism. A signal is emitted when a particular event occurs and a slot is a function that is called in response to a particular signal. Qt widgets have predefined signals and slots that you can use directly from Qt Designer.</p>
<p>To use Qt Designer to add a slot for the quit function, right-click the <b>Quit</b> button to open a context-menu and then select <b>Go to slot</b> &gt; <b>clicked()</b>.</p>
<p>A private slot, <tt>on_quitButton_clicked()</tt>, is added to the Notepad widget class header file, notepad.h and a private function, <tt>Notepad::on_quitButton_clicked()</tt>, is added to the Notepad widget class source file, notepad.cpp. We just need to write the code to execute the quit function in the source file.</p>
<p>Let us look at the modified code in the header file, notepad.h:</p>
<pre class="cpp"><span class="keyword">namespace</span> Ui {
<span class="keyword">class</span> Notepad;
}

<span class="keyword">class</span> Notepad : <span class="keyword">public</span> <span class="type">QMainWindow</span>
{
    Q_OBJECT

<span class="keyword">public</span>:
    <span class="keyword">explicit</span> Notepad(<span class="type">QWidget</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
    <span class="operator">~</span>Notepad();

<span class="keyword">private</span> <span class="keyword">slots</span>:
    <span class="type">void</span> on_quitButton_clicked();

<span class="keyword">private</span>:
    Ui<span class="operator">::</span>Notepad <span class="operator">*</span>ui;
};</pre>
<p>Line 14 uses Qt's signals and slots mechanism to make the application exit when the <b>Quit button</b> is pushed. Qt Designer uses QMetaObject auto-connection facilities to connect the button's clicked() signal to a slot in the Notepad class. The <tt>uic</tt> tool automatically generates code in the dialog's setupUi() function to do this, so Qt Designer only needs to declare and implement a slot with a name that follows a standard convention.</p>
<p>The corresponding code in the source file, notepad.cpp, looks as follows:</p>
<pre class="cpp"><span class="type">void</span> Notepad<span class="operator">::</span>on_quitButton_clicked()
{

}</pre>
<p>The code defines the private function that is executed when QPushButton emits the clicked() signal.</p>
<p>We now complement the code to have the quit() slot of QApplication exit Notepad:</p>
<pre class="cpp"><span class="type">void</span> Notepad<span class="operator">::</span>on_quitButton_clicked()
{
    qApp<span class="operator">-</span><span class="operator">&gt;</span>quit();
}</pre>
<p><b>Learn More</b></p>
<table class="generic">
 <thead><tr class="qt-style"><th >About</th><th >Here</th></tr></thead>
<tr valign="top" class="odd"><td >Signals and slots</td><td >Signals &amp; Slots</td></tr>
</table>
<a name="adding-menu-items"></a>
<h3>Adding Menu Items</h3>
<p>Often, in a main window, the same slot should be invoked by several widgets. Examples are menu items and buttons on a tool bar. To make this easier, Qt provides QAction, which can be given to several widgets, and be connected to a slot. For instance, both QMenu and QToolBar can create menu items and tool buttons from the same QAction.</p>
<p>To learn how to use actions with signals and slots, we add menu items to open and save a document and connect them to slots.</p>
<p>As before, we use Qt Designer to add the widgets to the user interface. The wizard creates an application with a QMenu widget, with the text <b>Type Here</b> as a placeholder for menu and menu item names. Double-click the text to enter names for the <b>File</b> menu and <b>Open</b> and <b>Save</b> menu items. Qt Designer automatically generates the appropriate actions.</p>
<p class="centerAlign"><img src="images/gs3.png" alt="" /></p><p>To connect the actions to slots, right-click an action and select <b>Go to slot</b> &gt; <b>triggered()</b>.</p>
<p>QAction instances are created with the text that should appear on the widgets that we add them to (in our case, menu items). If we also wanted to add the actions to a tool bar, we could have specified icons for them.</p>
<p>The modified code in notepad.ui now looks as follows:</p>
<pre class="qml">  &lt;widget class=&quot;QMenuBar&quot; name=&quot;menuBar&quot;&gt;
   &lt;property name=&quot;geometry&quot;&gt;
    &lt;rect&gt;
     &lt;x&gt;0&lt;/x&gt;
     &lt;y&gt;0&lt;/y&gt;
     &lt;width&gt;400&lt;/width&gt;
     &lt;height&gt;22&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
   &lt;widget class=&quot;QMenu&quot; name=&quot;menuFile&quot;&gt;
    &lt;property name=&quot;title&quot;&gt;
     &lt;string&gt;File&lt;/string&gt;
    &lt;/property&gt;
    &lt;addaction name=&quot;actionOpen&quot;/&gt;
    &lt;addaction name=&quot;actionSave&quot;/&gt;
   &lt;/widget&gt;
   &lt;addaction name=&quot;menuFile&quot;/&gt;
  &lt;/widget&gt;
  &lt;widget class=&quot;QToolBar&quot; name=&quot;mainToolBar&quot;&gt;
   &lt;attribute name=&quot;toolBarArea&quot;&gt;
    &lt;enum&gt;TopToolBarArea&lt;/enum&gt;
   &lt;/attribute&gt;
   &lt;attribute name=&quot;toolBarBreak&quot;&gt;
    &lt;bool&gt;false&lt;/bool&gt;
   &lt;/attribute&gt;
  &lt;/widget&gt;
  &lt;widget class=&quot;QStatusBar&quot; name=&quot;statusBar&quot;/&gt;
  &lt;action name=&quot;actionOpen&quot;&gt;
   &lt;property name=&quot;text&quot;&gt;
    &lt;string&gt;Open&lt;/string&gt;
   &lt;/property&gt;
  &lt;/action&gt;
  &lt;action name=&quot;actionSave&quot;&gt;
   &lt;property name=&quot;text&quot;&gt;
    &lt;string&gt;Save&lt;/string&gt;
   &lt;/property&gt;
  &lt;/action&gt;
 &lt;/widget&gt;</pre>
<p>Qt Designer adds the private slots <tt>on_actionOpen_triggered()</tt> and <tt>on_actionSave_triggered()</tt> to notepad.h and the private functions <tt>Notepad::on_actionOpen_triggered()</tt> and <tt>Notepad::on_actionSave_triggered()</tt> to notepad.cpp.</p>
<p>In the following sections, we complement the code to load and save files. When a menu item is clicked, the item triggers the action, and the respective slot is invoked.</p>
<a name="opening-files"></a>
<h3>Opening Files</h3>
<p>In this section, we implement the functionality of the <tt>on_actionOpen_triggered()</tt> slot. The first step is asking the user for the name of the file to open. Qt comes with QFileDialog, which is a dialog from which the user can select a file. The appearance of the dialog depends on the desktop platform that you run the application on. The following image shows the dialog on Mac OS:</p>
<p class="centerAlign"><img src="images/gs4.png" alt="" /></p><p>We complement the code generated by Qt Designer in notepad.cpp, as follows:</p>
<pre class="cpp"><span class="type">void</span> Notepad<span class="operator">::</span>on_actionOpen_triggered()
{
    <span class="type">QString</span> fileName <span class="operator">=</span> <span class="type">QFileDialog</span><span class="operator">::</span>getOpenFileName(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Open File&quot;</span>)<span class="operator">,</span> <span class="type">QString</span>()<span class="operator">,</span>
            tr(<span class="string">&quot;Text Files (*.txt);;C++ Files (*.cpp *.h)&quot;</span>));

    <span class="keyword">if</span> (<span class="operator">!</span>fileName<span class="operator">.</span>isEmpty()) {
        <span class="type">QFile</span> file(fileName);
        <span class="keyword">if</span> (<span class="operator">!</span>file<span class="operator">.</span>open(<span class="type">QIODevice</span><span class="operator">::</span>ReadOnly)) {
            <span class="type">QMessageBox</span><span class="operator">::</span>critical(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Error&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;Could not open file&quot;</span>));
            <span class="keyword">return</span>;
        }
        <span class="type">QTextStream</span> in(<span class="operator">&amp;</span>file);
        ui<span class="operator">-</span><span class="operator">&gt;</span>textEdit<span class="operator">-</span><span class="operator">&gt;</span>setText(in<span class="operator">.</span>readAll());
        file<span class="operator">.</span>close();
    }
}</pre>
<p>The static getOpenFileName() function displays a modal file dialog. It returns the file path of the file selected, or an empty string if the user canceled the dialog.</p>
<p>If we have a file name, we try to open the file with open(), which returns true if the file could be opened. We will not go into error handling here, but you can follow the links from the learn more section. If the file could not be opened, we use QMessageBox to display a dialog with an error message (see the QMessageBox class description for further details).</p>
<p>Actually reading in the data is trivial using the QTextStream class, which wraps the QFile object. The readAll() function returns the contents of the file as a QString. The contents can then be displayed in the text edit. We then close() the file to return the file descriptor back to the operating system.</p>
<p>We now use the function tr() around our user visible strings. This function is necessary when you want to provide your application in more than one language (for example, English and Chinese). We will not go into details here, but you can follow the <tt>Qt Linguist</tt> link from the learn more table.</p>
<p>To use QFileDialog, QFile, QMessageBox, and QTextStream, add the following includes to notepad.cpp:</p>
<pre class="cpp"><span class="preprocessor">#include &lt;QFileDialog&gt;</span>
<span class="preprocessor">#include &lt;QFile&gt;</span>
<span class="preprocessor">#include &lt;QMessageBox&gt;</span>
<span class="preprocessor">#include &lt;QTextStream&gt;</span></pre>
<a name="saving-files"></a>
<h3>Saving Files</h3>
<p>Now, let us move on to the <tt>on_actionSave_triggered()</tt> slot, which also uses QFileDialog to create a dialog in which the user can save a file with the specified name in the specified location.</p>
<p class="centerAlign"><img src="images/gs5.png" alt="" /></p><p>We complement the code generated by Qt Designer in notepad.cpp, as follows:</p>
<pre class="cpp"><span class="type">void</span> Notepad<span class="operator">::</span>on_actionSave_triggered()
{
    <span class="type">QString</span> fileName <span class="operator">=</span> <span class="type">QFileDialog</span><span class="operator">::</span>getSaveFileName(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Save File&quot;</span>)<span class="operator">,</span> <span class="type">QString</span>()<span class="operator">,</span>
            tr(<span class="string">&quot;Text Files (*.txt);;C++ Files (*.cpp *.h)&quot;</span>));

    <span class="keyword">if</span> (<span class="operator">!</span>fileName<span class="operator">.</span>isEmpty()) {
        <span class="type">QFile</span> file(fileName);
        <span class="keyword">if</span> (<span class="operator">!</span>file<span class="operator">.</span>open(<span class="type">QIODevice</span><span class="operator">::</span>WriteOnly)) {
            <span class="comment">// error message</span>
        } <span class="keyword">else</span> {
            <span class="type">QTextStream</span> stream(<span class="operator">&amp;</span>file);
            stream <span class="operator">&lt;</span><span class="operator">&lt;</span> ui<span class="operator">-</span><span class="operator">&gt;</span>textEdit<span class="operator">-</span><span class="operator">&gt;</span>toPlainText();
            stream<span class="operator">.</span>flush();
            file<span class="operator">.</span>close();
        }
    }
}</pre>
<p>When we write the contents of the text edit to the file, we use the QTextStream class again. QTextStream can also write QStrings to the file with the &lt;&lt; operator.</p>
<p><b>Learn More</b></p>
<table class="generic">
 <thead><tr class="qt-style"><th >About</th><th >Here</th></tr></thead>
<tr valign="top" class="odd"><td >MDI applications</td><td >QMdiArea, MDI Example</td></tr>
<tr valign="top" class="even"><td >Files and I/O devices</td><td >QFile, QIODevice</td></tr>
<tr valign="top" class="odd"><td >tr() and internationalization</td><td >Qt Linguist Manual, <a href="i18n-source-translation.html">Writing Source Code for Translation</a>, <a href="internationalization.html">Internationalization with Qt</a></td></tr>
</table>
<a name="building-and-running-notepad"></a>
<h2>Building and Running Notepad</h2>
<p>Now that you have all the necessary files, select <b>Build</b> &gt; <b>Build Project Notepad</b> to build and run the application. Qt Creator uses <tt>qmake</tt> and <tt>make</tt> to create an executable in the directory specified in the build settings of the project and runs it.</p>
<a name="building-and-running-from-the-command-line"></a>
<h3>Building and Running from the Command Line</h3>
<p>To build the application from the command line, switch to the directory in which you have the <tt>.cpp</tt> file of the application and add the project file (suffixed .pro) described earlier. The following shell commands then build the application:</p>
<pre class="cpp">qmake
make (<span class="keyword">or</span> nmake on Windows)</pre>
<p>The commands create an executable in the project directory. The <tt>qmake</tt> tool reads the project file and produces a <tt>Makefile</tt> with instructions on how to build the application. The <tt>make</tt> tool (or the <tt>nmake</tt> tool) then reads the <tt>Makefile</tt> and produces the executable binary.</p>
</div>
<!-- @@@gettingstartedqt.html -->
</div>
</div>
</div>
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>