Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 1dd17e0d683ef79b4bb6872bbf359d7f > files > 1704

qt4-doc-4.8.7-26.2.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>Qt 4.8: Getting Started Programming with Qt</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li>Getting Started Programming with Qt</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#hello-notepad">Hello Notepad</a></li>
<li class="level2"><a href="#learn-more">Learn More</a></li>
<li class="level1"><a href="#adding-a-quit-button">Adding a Quit Button</a></li>
<li class="level2"><a href="#learn-more">Learn More</a></li>
<li class="level1"><a href="#subclassing-qwidget">Subclassing QWidget</a></li>
<li class="level2"><a href="#learn-more">Learn More</a></li>
<li class="level2"><a href="#creating-a-pro-file">Creating a .pro file</a></li>
<li class="level1"><a href="#using-a-qmainwindow">Using a QMainWindow</a></li>
<li class="level2"><a href="#learn-more">Learn More</a></li>
<li class="level1"><a href="#saving-and-loading">Saving and Loading</a></li>
<li class="level2"><a href="#learn-more">Learn More</a></li>
</ul>
</div>
<h1 class="title">Getting Started Programming with Qt</h1>
<span class="subtitle"></span>
<!-- $$$gettingstartedqt.html-description -->
<div class="descr"> <a name="details"></a>
<p>Welcome to the world of Qt--the cross-platform GUI toolkit. In this getting started guide, we teach basic Qt knowledge by implementing a simple Notepad application. After reading this guide, you should be ready to delve into our overviews and API documentation, and find the information you need for the application you are developing.</p>
<p>The code for this tutorial is available in <tt>examples/tutorials/gettingStarted/gsQt</tt> under your Qt installation. If you are using the Qt SDK, you will find it in <tt>Examples/4.7/tutorials/gettingStarted/gsQt</tt> (change <tt>4.7</tt> if you are using a later Qt version).</p>
<a name="hello-notepad"></a>
<h2>Hello Notepad</h2>
<p>In this first example, we simply create and show a text edit in a window frame on the desktop. This represents the simplest possible Qt program that has a GUI.</p>
<p class="centerAlign"><img src="images/gs1.png" alt="" /></p><p>Here is the code:</p>
<pre class="cpp">  <span class="number">1</span>        <span class="preprocessor">#include &lt;QApplication&gt;</span>
  <span class="number">2</span>        <span class="preprocessor">#include &lt;QTextEdit&gt;</span>
  <span class="number">3</span>
  <span class="number">4</span>        <span class="type">int</span> main(<span class="type">int</span> argv<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>args)
  <span class="number">5</span>        {
  <span class="number">6</span>            <span class="type"><a href="qapplication.html">QApplication</a></span> app(argv<span class="operator">,</span> args);
  <span class="number">7</span>
  <span class="number">8</span>            <span class="type"><a href="qtextedit.html">QTextEdit</a></span> textEdit;
  <span class="number">9</span>            textEdit<span class="operator">.</span>show();
 <span class="number">10</span>
 <span class="number">11</span>            <span class="keyword">return</span> app<span class="operator">.</span>exec();
 <span class="number">12</span>        }</pre>
<p>Let us go through the code line by line. In the first two lines, we include the header files for <a href="qapplication.html">QApplication</a> and <a href="qtextedit.html">QTextEdit</a>, which are the two classes that we need for this example. All Qt classes have a header file named after them.</p>
<p>Line 6 creates a <a href="qapplication.html">QApplication</a> object. This object manages application-wide resources and is necessary to run any Qt program that has a GUI. It needs <tt>argv</tt> and <tt>args</tt> because Qt accepts a few command line arguments.</p>
<p>Line 8 creates a <a href="qtextedit.html">QTextEdit</a> object. A text edit is a visual element in the GUI. In Qt, we call such elements widgets. Examples of other widgets are 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 9 shows the text edit on the screen in its own window frame. Since widgets also function as containers (for instance a <a href="qmainwindow.html">QMainWindow</a>, 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 <a href="qwidget.html#show">show()</a> makes the widget visible.</p>
<p>Line 11 makes the <a href="qapplication.html">QApplication</a> 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. When you type text in the text edit widget, it receives key pressed events and responds by drawing the text typed.</p>
<p>To run the application, open a command prompt, and enter the directory in which you have the <tt>.cpp</tt> file of the program. The following shell commands build the program.</p>
<pre class="cpp">         qmake <span class="operator">-</span>project
         qmake
         make</pre>
<p>This will leave an executable in the <tt>part1</tt> directory (note that on Windows, you may have to use <tt>nmake</tt> instead of <tt>make</tt>. Also, the executable will be placed in part1\debug or part1\release (these directories are created when you run <tt>make</tt>). <tt>qmake</tt> is Qt's build tool, which takes a configuration file. <tt>qmake</tt> generates this for us when given the <tt>-project</tt> argument. Given the configuration file (suffixed .pro), <tt>qmake</tt> produces a <tt>make</tt> file that will build the program for you. We will look into writing our own <tt>.pro</tt> files later.</p>
<a name="learn-more"></a>
<h3>Learn More</h3>
<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 ><a href="application-windows.html">Window and Dialog Widgets</a></td></tr>
<tr valign="top" class="even"><td >Events and event handling</td><td ><a href="eventsandfilters.html">The Event System</a></td></tr>
</table>
<a name="adding-a-quit-button"></a>
<h2>Adding a Quit Button</h2>
<p>In a real application, you will normally need more than one widget. We will now introduce a <a href="qpushbutton.html">QPushButton</a> beneath the text edit. The button will exit the Notepad application when pushed (i.e&#x2e;, clicked on with the mouse).</p>
<p class="centerAlign"><img src="images/gs2.png" alt="" /></p><p>Let us take a look at the code.</p>
<pre class="cpp">  <span class="number">1</span>      <span class="preprocessor">#include &lt;QtGui&gt;</span>
  <span class="number">2</span>
  <span class="number">3</span>      <span class="type">int</span> main(<span class="type">int</span> argv<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>args)
  <span class="number">4</span>      {
  <span class="number">5</span>          <span class="type"><a href="qapplication.html">QApplication</a></span> app(argv<span class="operator">,</span> args);
  <span class="number">6</span>
  <span class="number">7</span>          <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>textEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>;
  <span class="number">8</span>          <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>quitButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(<span class="string">&quot;&amp;Quit&quot;</span>);
  <span class="number">9</span>
 <span class="number">10</span>          <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(quitButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> qApp<span class="operator">,</span> SLOT(quit()));
 <span class="number">11</span>
 <span class="number">12</span>          <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>;
 <span class="number">13</span>          layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(textEdit);
 <span class="number">14</span>          layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(quitButton);
 <span class="number">15</span>
 <span class="number">16</span>          <span class="type"><a href="qwidget.html">QWidget</a></span> window;
 <span class="number">17</span>          window<span class="operator">.</span>setLayout(layout);
 <span class="number">18</span>
 <span class="number">19</span>          window<span class="operator">.</span>show();
 <span class="number">20</span>
 <span class="number">21</span>          <span class="keyword">return</span> app<span class="operator">.</span>exec();
 <span class="number">22</span>      }</pre>
<p>Line 1 includes <a href="qtgui.html">QtGui</a>, which contains all of Qt's GUI classes.</p>
<p>Line 10 uses Qt's Signals and Slots mechanism to make the application exit when the <b>Quit button</b> is pushed. A slot is a function that can be invoked at runtime using its name (as a literal string). A signal is a function that when called will invoke slots registered with it; we call that to connect the slot to the signal and to emit the signal.</p>
<p><a href="qcoreapplication.html#quit">quit()</a> is a slot of <a href="qapplication.html">QApplication</a> that exits the application. <a href="qabstractbutton.html#clicked">clicked()</a> is a signal that <a href="qpushbutton.html">QPushButton</a> emits when it is pushed. The static <a href="qobject.html#connect">QObject::connect</a>() function takes care of connecting the slot to the signal. SIGNAL() and SLOT() are two macros that take the function signatures of the signal and slot to connect. We also need to give pointers to the objects that should send and receive the signal.</p>
<p>Line 12 creates a <a href="qvboxlayout.html">QVBoxLayout</a>. 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. <a href="qvboxlayout.html">QVBoxLayout</a>, for instance, places the children in a vertical row.</p>
<p>Line 13 and 14 adds the text edit and button to the layout. In line 17, we set the layout on a widget.</p>
<a name="learn-more"></a>
<h3>Learn More</h3>
<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 ><a href="signalsandslots.html">Signals &amp; Slots</a></td></tr>
<tr valign="top" class="even"><td >Layouts</td><td ><a href="layout.html">Layout Management</a>, <a href="widgets-and-layouts.html">Widgets and Layouts</a>, <a href="examples-layouts.html">Layout Examples</a></td></tr>
<tr valign="top" class="odd"><td >The widgets that come with Qt</td><td ><a href="gallery.html">Qt Widget Gallery</a>, <a href="examples-widgets.html">Widget Examples</a></td></tr>
</table>
<a name="subclassing-qwidget"></a>
<h2>Subclassing QWidget</h2>
<p>When the user wants to quit an application, you might want to pop-up a dialog that asks whether he/she really wants to quit. In this example, we subclass <a href="qwidget.html">QWidget</a>, and add a slot that we connect to the <b>Quit button</b>.</p>
<p class="centerAlign"><img src="images/gs3.png" alt="" /></p><p>Let us look at the code:</p>
<pre class="cpp">  <span class="number">5</span>        <span class="keyword">class</span> Notepad : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
  <span class="number">6</span>        {
  <span class="number">7</span>            Q_OBJECT
  <span class="number">8</span>
  <span class="number">9</span>        <span class="keyword">public</span>:
 <span class="number">10</span>            Notepad();
 <span class="number">11</span>
 <span class="number">12</span>        <span class="keyword">private</span> <span class="keyword">slots</span>:
 <span class="number">13</span>            <span class="type">void</span> quit();
 <span class="number">14</span>
 <span class="number">15</span>        <span class="keyword">private</span>:
 <span class="number">16</span>            <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>textEdit;
 <span class="number">17</span>            <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>quitButton;
 <span class="number">18</span>        };</pre>
<p>The <tt>Q_OBJECT</tt> macro must be first in the class definition, and declares our class as a <tt>QObject</tt> (Naturally, it must also inherit from <a href="qobject.html">QObject</a>). A <a href="qobject.html">QObject</a> 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 13 declares the slot <tt>quit()</tt>. This is easy using the <tt>slots</tt> macro. The <tt>quit()</tt> slot can now be connected to signals. We will do that later.</p>
<p>Instead of setting up the GUI and connecting the slot in the <tt>main()</tt> function, we now use <tt>Notepad</tt>'s constructor.</p>
<pre class="cpp"> <span class="number">20</span>        Notepad<span class="operator">::</span>Notepad()
 <span class="number">21</span>        {
 <span class="number">22</span>            textEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>;
 <span class="number">23</span>            quitButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;Quit&quot;</span>));
 <span class="number">24</span>
 <span class="number">25</span>            connect(quitButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(quit()));
 <span class="number">26</span>
 <span class="number">27</span>            <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>;
 <span class="number">28</span>            layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(textEdit);
 <span class="number">29</span>            layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(quitButton);
 <span class="number">30</span>
 <span class="number">31</span>            setLayout(layout);
 <span class="number">32</span>
 <span class="number">33</span>            setWindowTitle(tr(<span class="string">&quot;Notepad&quot;</span>));
 <span class="number">34</span>        }</pre>
<p>As you saw in the class definition, we use pointers to our <a href="qobject.html">QObject</a>s (<tt>textEdit</tt> and <tt>quitButton</tt>). As a rule, you should always allocate <a href="qobject.html">QObject</a>s on the heap and never copy them.</p>
<p>We now use the function <a href="qobject.html#tr">tr()</a> around our user visible strings. This function is necessary when you want to provide your application in more than one language (e.g&#x2e; 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>Here is the <tt>quit()</tt> slot:</p>
<pre class="cpp"> <span class="number">75</span>      <span class="type">void</span> Notepad<span class="operator">::</span>quit()
 <span class="number">76</span>      {
 <span class="number">77</span>          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span> messageBox;
 <span class="number">78</span>          messageBox<span class="operator">.</span>setWindowTitle(tr(<span class="string">&quot;Notepad&quot;</span>));
 <span class="number">79</span>          messageBox<span class="operator">.</span>setText(tr(<span class="string">&quot;Do you really want to quit?&quot;</span>));
 <span class="number">80</span>          messageBox<span class="operator">.</span>setStandardButtons(<span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Yes <span class="operator">|</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>No);
 <span class="number">81</span>          messageBox<span class="operator">.</span>setDefaultButton(<span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>No);
 <span class="number">82</span>          <span class="keyword">if</span> (messageBox<span class="operator">.</span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Yes)
 <span class="number">83</span>              qApp<span class="operator">-</span><span class="operator">&gt;</span>quit();
 <span class="number">84</span>      }</pre>
<p>We use the <a href="qmessagebox.html">QMessageBox</a> class to display a dialog that asks the user whether he/she really wants to quit.</p>
<a name="learn-more"></a>
<h3>Learn More</h3>
<table class="generic">
<thead><tr class="qt-style"><th >About</th><th >Here</th></tr></thead>
<tr valign="top" class="odd"><td >tr() and internationalization</td><td ><a href="linguist-manual.html">Qt Linguist Manual</a>, <a href="i18n-source-translation.html">Writing Source Code for Translation</a>, <a href="linguist-hellotr.html">Hello tr</a>() Example, <a href="internationalization.html">Internationalization with Qt</a></td></tr>
<tr valign="top" class="even"><td ><a href="qtwebkit-bridge.html#qobjects">QObjects</a> and the Qt Object model (This is essential to understand Qt)</td><td ><a href="object.html">Object Model</a></td></tr>
<tr valign="top" class="odd"><td >qmake and the Qt build system</td><td ><a href="qmake-manual.html">qmake Manual</a></td></tr>
</table>
<a name="creating-a-pro-file"></a>
<h3>Creating a .pro file</h3>
<p>For this example, we write our own <tt>.pro</tt> file instead of using <tt>qmake</tt>'s <tt>-project</tt> option.</p>
<pre class="cpp">  <span class="number">1</span>        HEADERS <span class="operator">=</span>  notepad<span class="operator">.</span>h
  <span class="number">2</span>        SOURCES <span class="operator">=</span>  notepad<span class="operator">.</span>cpp \
  <span class="number">3</span>                   main<span class="operator">.</span>cpp</pre>
<p>The following shell commands build the example.</p>
<pre class="cpp">         qmake
         make</pre>
<a name="using-a-qmainwindow"></a>
<h2>Using a QMainWindow</h2>
<p>Many applications will benefit from using a <a href="qmainwindow.html">QMainWindow</a>, which has its own layout to which you can add a menu bar, dock widgets, tool bars, and a status bar. <a href="qmainwindow.html">QMainWindow</a> has a center area that can be occupied by any kind of widget. In our case, we will place our text edit there.</p>
<p class="centerAlign"><img src="images/gs4.png" alt="" /></p><p>Let us look at the new <tt>Notepad</tt> class definition.</p>
<pre class="cpp">  <span class="number">2</span>        <span class="preprocessor">#include &lt;QtGui&gt;</span>
  <span class="number">3</span>
  <span class="number">4</span>        <span class="keyword">class</span> Notepad : <span class="keyword">public</span> <span class="type"><a href="qmainwindow.html">QMainWindow</a></span>
  <span class="number">5</span>        {
  <span class="number">6</span>            Q_OBJECT
  <span class="number">7</span>
  <span class="number">8</span>        <span class="keyword">public</span>:
  <span class="number">9</span>            Notepad();
 <span class="number">10</span>
 <span class="number">11</span>        <span class="keyword">private</span> <span class="keyword">slots</span>:
 <span class="number">12</span>            <span class="type">void</span> open();
 <span class="number">13</span>            <span class="type">void</span> save();
 <span class="number">14</span>            <span class="type">void</span> quit();
 <span class="number">15</span>
 <span class="number">16</span>        <span class="keyword">private</span>:
 <span class="number">17</span>            <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>textEdit;
 <span class="number">18</span>
 <span class="number">19</span>            <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>openAction;
 <span class="number">20</span>            <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>saveAction;
 <span class="number">21</span>            <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>exitAction;
 <span class="number">22</span>
 <span class="number">23</span>            <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>fileMenu;
 <span class="number">24</span>        };</pre>
<p>We include two more slots that can save and open a document. We will implement these in the next section.</p>
<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 <a href="qaction.html">QAction</a>, which can be given to several widgets, and be connected to a slot. For instance, both <a href="qmenu.html">QMenu</a> and <a href="qtoolbar.html">QToolBar</a> can create menu items and tool buttons from the same <a href="qaction.html">QAction</a>s. We will see how this works shortly.</p>
<p>As before, we use the <tt>Notepad</tt>s constructor to set up the GUI.</p>
<pre class="cpp"> <span class="number">25</span>        Notepad<span class="operator">::</span>Notepad()
 <span class="number">26</span>        {
 <span class="number">27</span>            openAction <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Open&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
 <span class="number">28</span>            saveAction <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Save&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
 <span class="number">29</span>            exitAction <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;E&amp;xit&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
 <span class="number">30</span>
 <span class="number">31</span>            connect(openAction<span class="operator">,</span> SIGNAL(triggered())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(open()));
 <span class="number">32</span>            connect(saveAction<span class="operator">,</span> SIGNAL(triggered())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(save()));
 <span class="number">33</span>            connect(exitAction<span class="operator">,</span> SIGNAL(triggered())<span class="operator">,</span> qApp<span class="operator">,</span> SLOT(quit()));
 <span class="number">34</span>
 <span class="number">35</span>            fileMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;File&quot;</span>));
 <span class="number">36</span>            fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(openAction);
 <span class="number">37</span>            fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(saveAction);
 <span class="number">38</span>            fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();
 <span class="number">39</span>            fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(exitAction);
 <span class="number">40</span>
 <span class="number">41</span>            textEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>;
 <span class="number">42</span>            setCentralWidget(textEdit);
 <span class="number">43</span>
 <span class="number">44</span>            setWindowTitle(tr(<span class="string">&quot;Notepad&quot;</span>));
 <span class="number">45</span>        }</pre>
<p><a href="qaction.html">QAction</a>s 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 them to a tool bar, we could have given <a href="qicon.html">icons</a> to the actions.</p>
<p>When a menu item is clicked now, the item will trigger the action, and the respective slot will be invoked.</p>
<a name="learn-more"></a>
<h3>Learn More</h3>
<table class="generic">
<thead><tr class="qt-style"><th >About</th><th >Here</th></tr></thead>
<tr valign="top" class="odd"><td >Main windows and main window classes</td><td ><a href="mainwindow.html">Application Main Window</a>, <a href="examples-mainwindow.html">Main Window Examples</a></td></tr>
<tr valign="top" class="even"><td >MDI applications</td><td ><a href="qmdiarea.html">QMdiArea</a>, <a href="mainwindows-mdi.html">MDI Example</a></td></tr>
</table>
<a name="saving-and-loading"></a>
<h2>Saving and Loading</h2>
<p>In this example, we will implement the functionality of the <tt>open()</tt> and <tt>save()</tt> slots that we added in the previous example.</p>
<p class="centerAlign"><img src="images/gs5.png" alt="" /></p><p>We will start with the <tt>open()</tt> slot:</p>
<pre class="cpp"> <span class="number">48</span>    <span class="type">void</span> Notepad<span class="operator">::</span>open()
 <span class="number">49</span>    {
 <span class="number">50</span>        <span class="type"><a href="qstring.html">QString</a></span> fileName <span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></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="string">&quot;&quot;</span><span class="operator">,</span>
 <span class="number">51</span>            tr(<span class="string">&quot;Text Files (*.txt);;C++ Files (*.cpp *.h)&quot;</span>));
 <span class="number">52</span>
 <span class="number">53</span>        <span class="keyword">if</span> (fileName <span class="operator">!</span><span class="operator">=</span> <span class="string">&quot;&quot;</span>) {
 <span class="number">54</span>            <span class="type"><a href="qfile.html">QFile</a></span> file(fileName);
 <span class="number">55</span>            <span class="keyword">if</span> (<span class="operator">!</span>file<span class="operator">.</span>open(<span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly)) {
 <span class="number">56</span>                <span class="type"><a href="qmessagebox.html">QMessageBox</a></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="number">57</span>                <span class="keyword">return</span>;
 <span class="number">58</span>            }
 <span class="number">59</span>            <span class="type"><a href="qtextstream.html">QTextStream</a></span> in(<span class="operator">&amp;</span>file);
 <span class="number">60</span>            textEdit<span class="operator">-</span><span class="operator">&gt;</span>setText(in<span class="operator">.</span>readAll());
 <span class="number">61</span>            file<span class="operator">.</span>close();
 <span class="number">62</span>        }
 <span class="number">63</span>    }</pre>
<p>The first step is asking the user for the name of the file to open. Qt comes with <a href="qfiledialog.html">QFileDialog</a>, which is a dialog from which the user can select a file. The image above shows the dialog on Kubuntu. The static <a href="qfiledialog.html#getOpenFileName">getOpenFileName()</a> 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 <a href="qiodevice.html#open">open()</a>, 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 <a href="qmessagebox.html">QMessageBox</a> to display a dialog with an error message (see the <a href="qmessagebox.html">QMessageBox</a> class description for further details).</p>
<p>Actually reading in the data is trivial using the <a href="qtextstream.html">QTextStream</a> class, which wraps the <a href="qfile.html">QFile</a> object. The <a href="qtextstream.html#readAll">readAll()</a> function returns the contents of the file as a <a href="qstring.html">QString</a>. The contents can then be displayed in the text edit. We then <a href="qiodevice.html#close">close()</a> the file to return the file descriptor back to the operating system.</p>
<p>Now, let us move on to the the <tt>save()</tt> slot.</p>
<pre class="cpp"> <span class="number">65</span>    <span class="type">void</span> Notepad<span class="operator">::</span>save()
 <span class="number">66</span>    {
 <span class="number">67</span>        <span class="type"><a href="qstring.html">QString</a></span> fileName <span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></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="string">&quot;&quot;</span><span class="operator">,</span>
 <span class="number">68</span>        tr(<span class="string">&quot;Text Files (*.txt);;C++ Files (*.cpp *.h)&quot;</span>));
 <span class="number">69</span>
 <span class="number">70</span>        <span class="keyword">if</span> (fileName <span class="operator">!</span><span class="operator">=</span> <span class="string">&quot;&quot;</span>) {
 <span class="number">71</span>            <span class="type"><a href="qfile.html">QFile</a></span> file(fileName);
 <span class="number">72</span>            <span class="keyword">if</span> (<span class="operator">!</span>file<span class="operator">.</span>open(<span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>WriteOnly)) {
 <span class="number">73</span>                <span class="comment">// error message</span>
 <span class="number">74</span>            } <span class="keyword">else</span> {
 <span class="number">75</span>                <span class="type"><a href="qtextstream.html">QTextStream</a></span> stream(<span class="operator">&amp;</span>file);
 <span class="number">76</span>                stream <span class="operator">&lt;</span><span class="operator">&lt;</span> textEdit<span class="operator">-</span><span class="operator">&gt;</span>toPlainText();
 <span class="number">77</span>                stream<span class="operator">.</span>flush();
 <span class="number">78</span>                file<span class="operator">.</span>close();
 <span class="number">79</span>            }
 <span class="number">80</span>        }
 <span class="number">81</span>    }</pre>
<p>When we write the contents of the text edit to the file, we use the <a href="qtextstream.html">QTextStream</a> class again. <a href="qtextstream.html">QTextStream</a> can also write <a href="qstring.html">QString</a>s to the file with the &lt;&lt; operator.</p>
<a name="learn-more"></a>
<h3>Learn More</h3>
<table class="generic">
<thead><tr class="qt-style"><th >About</th><th >Here</th></tr></thead>
<tr valign="top" class="odd"><td >Files and I/O devices</td><td ><a href="qfile.html">QFile</a>, <a href="qiodevice.html">QIODevice</a></td></tr>
</table>
</div>
<!-- @@@gettingstartedqt.html -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2015 The Qt Company Ltd.
      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>
      Qt and respective logos are trademarks of The Qt Company Ltd 
      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>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>