summaryrefslogtreecommitdiffstats
path: root/doc/src/tutorials
diff options
context:
space:
mode:
authorMichael Brasser <michael.brasser@nokia.com>2009-05-22 04:41:04 (GMT)
committerMichael Brasser <michael.brasser@nokia.com>2009-05-22 04:41:04 (GMT)
commitba9caa7181282109ea8def0994838b6538da0b13 (patch)
treef1ae3f595fe6f601ce411d69179d6c0c8c650386 /doc/src/tutorials
parent2575eac4c26ad92dde95959a82f576edc3e76e1d (diff)
parentf9d26f0bebd5bcc32d15c4a627251c44cf78389e (diff)
downloadQt-ba9caa7181282109ea8def0994838b6538da0b13.zip
Qt-ba9caa7181282109ea8def0994838b6538da0b13.tar.gz
Qt-ba9caa7181282109ea8def0994838b6538da0b13.tar.bz2
Merge branch 'master' of git@scm.dev.nokia.troll.no:qt/qt into kinetic-declarativeui
Conflicts: src/corelib/kernel/kernel.pri src/corelib/tools/tools.pri tools/qdoc3/htmlgenerator.cpp tools/qdoc3/htmlgenerator.h
Diffstat (limited to 'doc/src/tutorials')
-rw-r--r--doc/src/tutorials/addressbook-fr.qdoc7
-rw-r--r--doc/src/tutorials/addressbook.qdoc9
-rw-r--r--doc/src/tutorials/widgets-tutorial.qdoc132
3 files changed, 132 insertions, 16 deletions
diff --git a/doc/src/tutorials/addressbook-fr.qdoc b/doc/src/tutorials/addressbook-fr.qdoc
index 2847f1b..512a404 100644
--- a/doc/src/tutorials/addressbook-fr.qdoc
+++ b/doc/src/tutorials/addressbook-fr.qdoc
@@ -239,13 +239,16 @@
\snippet tutorials/addressbook/part1/main.cpp main function
- On construit un nouveau widget \c AddressBook sur le tas en utilisant le mot-clé
- \c new et en invoquant sa méthode \l{QWidget::show()}{show()} pour l'afficher.
+ On construit un nouveau widget \c AddressBook sur la pile et on invoque
+ sa méthode \l{QWidget::show()}{show()} pour l'afficher.
Cependant, le widget ne sera pas visible tant que la boucle d'évènements
n'aura pas été lancée. On démarre la boucle d'évènements en appelant la
méthode \l{QApplication::}{exec()} de l'application; le résultat renvoyé
par cette méthode est lui même utilisé comme valeur de retour pour la méthode
\c main().
+ On comprend maintenant pourquoi \c AddressBook a été créé sur la pile: à la fin
+ du programme, l'objet sort du scope de la fonction \c main() et tous ses widgets enfants
+ sont supprimés, assurant ainsi qu'il n'y aura pas de fuites de mémoire.
*/
/*!
diff --git a/doc/src/tutorials/addressbook.qdoc b/doc/src/tutorials/addressbook.qdoc
index 3b0d2bc..38200b0 100644
--- a/doc/src/tutorials/addressbook.qdoc
+++ b/doc/src/tutorials/addressbook.qdoc
@@ -242,12 +242,15 @@
\snippet tutorials/addressbook/part1/main.cpp main function
- We construct a new \c AddressBook widget on the heap using the \c new
- keyword and invoke its \l{QWidget::show()}{show()} function to display it.
+ We construct a new \c AddressBook widget on the stack and invoke
+ its \l{QWidget::show()}{show()} function to display it.
However, the widget will not be shown until the application's event loop
is started. We start the event loop by calling the application's
\l{QApplication::}{exec()} function; the result returned by this function
- is used as the return value from the \c main() function.
+ is used as the return value from the \c main() function. At this point,
+ it becomes apparent why we instanciated \c AddressBook on the stack: It
+ will now go out of scope. Therefore, \c AddressBook and all its child widgets
+ will be deleted, thus preventing memory leaks.
*/
/*!
diff --git a/doc/src/tutorials/widgets-tutorial.qdoc b/doc/src/tutorials/widgets-tutorial.qdoc
index ead44af..1e89431 100644
--- a/doc/src/tutorials/widgets-tutorial.qdoc
+++ b/doc/src/tutorials/widgets-tutorial.qdoc
@@ -41,11 +41,14 @@
/*!
\page widgets-tutorial.html
+ \startpage {index.html}{Qt Reference Documentation}
+ \nextpage {tutorials/widgets/toplevel}{Creating a Window}
\title Widgets Tutorial
\ingroup tutorials
- \brief This tutorial covers basic usage of widgets and layouts, showing how they are used to build GUI applications.
+ \brief This tutorial covers basic usage of widgets and layouts, showing how
+ they are used to build GUI applications.
\section1 Introduction
@@ -68,7 +71,60 @@
occupied by its parent. This means that, when a window is deleted, all
the widgets it contains are automatically deleted.
- \section1 Creating a Window
+ \section1 Writing a main Function
+
+ Many of the GUI examples in Qt follow the pattern of having a \c{main.cpp}
+ file containing code to initialize the application, and a number of other
+ source and header files containing the application logic and custom GUI
+ components.
+
+ A typical \c main() function, written in \c{main.cpp}, looks like this:
+
+ \quotefile doc/src/snippets/widgets-tutorial/template.cpp
+
+ We first construct a QApplication object which is configured using any
+ arguments passed in from the command line. After any widgets have been
+ created and shown, we call QApplication::exec() to start Qt's event loop.
+ Control passes to Qt until this function returns, at which point we return
+ the value we obtain from this function.
+
+ In each part of this tutorial, we provide an example that is written
+ entirely within a \c main() function. In more sophisticated examples, the
+ code to set up widgets and layouts is written in other parts of the
+ example. For example, the GUI for a main window may be set up in the
+ constructor of a QMainWindow subclass.
+
+ The \l{Qt Examples#Widgets}{Widgets examples} are a good place to look for
+ more complex and complete examples and applications.
+
+ \section1 Building Examples and Tutorials
+
+ If you obtained a binary package of Qt or compiled it yourself, the
+ examples described in this tutorial should already be ready to run.
+ However, if you may wish to modify them and recompile them, you need to
+ perform the following steps:
+
+ \list 1
+ \o At the command line, enter the directory containing the example you
+ wish to recompile.
+ \o Type \c qmake and press \key{Return}. If this doesn't work, make sure
+ that the executable is on your path, or enter its full location.
+ \o On Linux/Unix and Mac OS X, type \c make and press \key{Return};
+ on Windows with Visual Studio, type \c nmake and press \key{Return}.
+ \endlist
+
+ An executable file should have been created within the current directory.
+ On Windows, this file may be located within a \c debug or \c release
+ subdirectory. You can run this file to see the example code at work.
+*/
+
+/*!
+ \page widgets-tutorial-toplevel.html
+ \contentspage {Widgets Tutorial}{Contents}
+ \previouspage {Widgets Tutorial}
+ \nextpage {Widgets Tutorial - Child Widgets}
+ \example tutorials/widgets/toplevel
+ \title Widgets Tutorial - Creating a Window
If a widget is created without a parent, it is treated as a window, or
\e{top-level widget}, when it is shown. Since it has no parent object to
@@ -82,7 +138,7 @@
<table align="left" width="100%">
<tr class="qt-code"><td>
\endraw
- \snippet snippets/widgets-tutorial/toplevel/main.cpp create, resize and show
+ \snippet tutorials/widgets/toplevel/main.cpp main program
\raw HTML
</td><td align="right">
\endraw
@@ -92,15 +148,28 @@
</table>
\endraw
- We can add a child widget to this window by passing \c window as the
- parent to its constructor. In this case, we add a button to the window
- and place it in a specific location:
+ To create a real GUI, we need to place widgets inside the window. To do
+ this, we pass a QWidget instance to a widget's constructor, as we will
+ demonstrate in the next part of this tutorial.
+*/
+
+/*!
+ \page widgets-tutorial-childwidget.html
+ \contentspage {Widgets Tutorial}{Contents}
+ \previouspage {Widgets Tutorial - Creating a Window}
+ \nextpage {Widgets Tutorial - Using Layouts}
+ \example tutorials/widgets/childwidget
+ \title Widgets Tutorial - Child Widgets
+
+ We can add a child widget to the window created in the previous example by
+ passing \c window as the parent to its constructor. In this case, we add a
+ button to the window and place it in a specific location:
\raw HTML
<table align="left" width="100%">
<tr class="qt-code"><td>
\endraw
- \snippet snippets/widgets-tutorial/childwidget/main.cpp create, position and show
+ \snippet tutorials/widgets/childwidget/main.cpp main program
\raw HTML
</td><td align="right">
\endraw
@@ -112,9 +181,16 @@
The button is now a child of the window and will be deleted when the
window is destroyed. Note that hiding or closing the window does not
- automatically destroy it.
+ automatically destroy it. It will be destroyed when the example exits.
+*/
- \section1 Using Layouts
+/*!
+ \page widgets-tutorial-windowlayout.html
+ \contentspage {Widgets Tutorial}{Contents}
+ \previouspage {Widgets Tutorial - Child Widgets}
+ \nextpage {Widgets Tutorial - Nested Layouts}
+ \example tutorials/widgets/windowlayout
+ \title Widgets Tutorial - Using Layouts
Usually, child widgets are arranged inside a window using layout objects
rather than by specifying positions and sizes explicitly. Here, we
@@ -125,7 +201,7 @@
<table align="left" width="100%">
<tr class="qt-code"><td>
\endraw
- \snippet snippets/widgets-tutorial/windowlayout/main.cpp create, lay out widgets and show
+ \snippet tutorials/widgets/windowlayout/main.cpp main program
\raw HTML
</td><td align="right">
\endraw
@@ -149,17 +225,31 @@
manage the label and line edit and set the layout on the window, both the
widgets and the layout itself are ''reparented'' to become children of
the window.
+*/
+
+/*!
+ \page widgets-tutorial-nestedlayouts.html
+ \contentspage {Widgets Tutorial}{Contents}
+ \previouspage {Widgets Tutorial - Using Layouts}
+ \example tutorials/widgets/nestedlayouts
+ \title Widgets Tutorial - Nested Layouts
Just as widgets can contain other widgets, layouts can be used to provide
different levels of grouping for widgets. Here, we want to display a
label alongside a line edit at the top of a window, above a table view
showing the results of a query.
+ We achieve this by creating two layouts: \c{queryLayout} is a QHBoxLayout
+ that contains QLabel and QLineEdit widgets placed side-by-side;
+ \c{mainLayout} is a QVBoxLayout that contains \c{queryLayout} and a
+ QTableView arranged vertically.
+
\raw HTML
<table align="left" width="100%">
<tr class="qt-code"><td>
\endraw
- \snippet snippets/widgets-tutorial/nestedlayouts/main.cpp create, lay out widgets and show
+ \snippet tutorials/widgets/nestedlayouts/main.cpp first part
+ \snippet tutorials/widgets/nestedlayouts/main.cpp last part
\raw HTML
</td><td align="right">
\endraw
@@ -169,6 +259,26 @@
</table>
\endraw
+ Note that we call the \c{mainLayout}'s \l{QBoxLayout::}{addLayout()}
+ function to insert the \c{queryLayout} above the \c{resultView} table.
+
+ We have omitted the code that sets up the model containing the data shown
+ by the QTableView widget, \c resultView. For completeness, we show this below.
+
As well as QHBoxLayout and QVBoxLayout, Qt also provides QGridLayout
and QFormLayout classes to help with more complex user interfaces.
+ These can be seen if you run \l{Qt Designer}.
+
+ \section1 Setting up the Model
+
+ In the code above, we did not show where the table's data came from
+ because we wanted to concentrate on the use of layouts. Here, we see
+ that the model holds a number of items corresponding to rows, each of
+ which is set up to contain data for two columns.
+
+ \snippet tutorials/widgets/nestedlayouts/main.cpp set up the model
+
+ The use of models and views is covered in the
+ \l{Qt Examples#Item Views}{item view examples} and in the
+ \l{Model/View Programming} overview.
*/