    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Going GUI with Qt</title>
        <link>https://members.accu.org/index.php/journals/672</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 16, #3 - Jun 2004 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c102/">163</a>
                    (11)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c102-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c102+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Going GUI with Qt</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 05 June 2004 13:16:05 +01:00 or Sat, 05 June 2004 13:16:05 +01:00</p>
<p><strong>Summary:</strong>&nbsp;<p>Welcome to the first installment in a series of articles
exploring GUI programming with the Qt C++ toolkit.</p></p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e21" id="d0e21"></a></h2>
</div>
<p>Welcome to the first installment in a series of articles
exploring GUI programming with the Qt C++ toolkit. Applications
developed with Qt can be run on all the main computing platforms in
use today, including Windows 95-XP, Mac OS X, Linux, and most
versions of Unix with X11, making C++/Qt applications almost as
portable as standard C++ applications. Furthermore, Qt was designed
as an object oriented library from its inception, which makes it
very clean and pleasant to program with.</p>
<div><img src="/var/uploads/journals/resources/qt.png"></div>
<p>We're going to present and explain a very small &quot;Hello&quot;
application that gives some of the flavour of Qt programming. The
application will display a label, a line editor, and a Quit button,
laid out vertically one above the other. The label's text will
change whenever the user changes the text in the line editor, and
the program will terminate when the user presses the Quit
button.</p>
<p>We'll implement the application using three files: <tt class=
"filename">main.cpp</tt>, <tt class="filename">window.h</tt>, and
<tt class="filename">window.cpp</tt>. Let's start with main.cpp
which just contains a simple <tt class="function">main()</tt>
function:</p>
<pre class="programlisting">
#include &lt;qapplication.h&gt;
#include &quot;window.h&quot;

int main(int argc, char **argv) {
  QApplication app(argc, argv);

  Window *window = new Window;
  app.setMainWidget(window);
  window-&gt;show();

  return app.exec();
}
</pre>
<p>We begin by creating a <tt class="classname">QApplication</tt>
object on the stack. Only one of these objects is created in a Qt
application; it provides the GUI event loop and access to useful
global information such as the desktop's size. Next we create a
<tt class="classname">Window</tt> object - this is our custom main
window, which we'll look at in a moment. For simple applications
like this it is convenient to identify the main window as the
application's &quot;main widget&quot;; this ensures that the application will
automatically terminate when the main window is closed. Then we
show the window on screen, and finally we start off the event loop
with the <tt class="methodname">app.exec()</tt> call. Most Qt
applications have main() functions just as short and simple as the
one shown here.</p>
<p>Now we're ready to look at the implementation of our <tt class=
"classname">Window</tt> subclass, starting with the header.</p>
<pre class="programlisting">
#ifndef WINDOW_H
#define WINDOW_H
#include &lt;qdialog.h&gt;

class QLabel;

class Window : public QDialog {
  Q_OBJECT

public:
  Window(QWidget *parent=0);

private slots:
  void updateName(const QString &amp;name);

private:
  QLabel *label;
};

#endif
</pre>
<p>The first unusual thing to notice about the header is the
<tt class="literal">Q_OBJECT</tt> macro. This signifies that the
class has some Qt extensions to C++ which we'll discuss later. The
<tt class="literal">private slots</tt> are private methods that can
also be called by Qt's signals and slots mechanism. Like most GUI
toolkits Qt provides low-level access to events such as mouse
presses and releases, and so on. But very often we want to work
with higher level concepts: we don't care <span class=
"emphasis"><em>how</em></span> a button was pressed (whether by a
mouse click, tabbed to then Spacebar, or Alt+underlined letter), we
only care <span class="emphasis"><em>that</em></span> it was
pressed. Qt's signals and slots mechanism is a solution to
this.</p>
<p>Qt widgets emit &quot;signals&quot; when state changes take place, for
example when the text changes in a line edit, or when a new item is
selected in a list box. These signals can be &quot;connected&quot; to slots
(methods), so that when a signal is emitted any slots it is
connected to are called. Widgets that are connected to one another
are independent components since they don't need to know anything
about each other.</p>
<p>We're now ready to look at window.cpp, starting with the Window
constructor.</p>
<pre class="programlisting">
Window::Window(QWidget *parent)
             : QDialog(parent) {
  setCaption(&quot;Hello&quot;);
  label = new QLabel(&quot;Hello CVu!&quot;, this);
  QLineEdit *edit = new QLineEdit(this);
  QPushButton *button
             = new QPushButton(&quot;&amp;Quit&quot;, this);

  QVBoxLayout *vbox
             = new QVBoxLayout(this, 5, 10);
  vbox-&gt;addWidget(label);
  vbox-&gt;addWidget(edit);
  vbox-&gt;addWidget(button);

  connect(edit,
          SIGNAL(textChanged(const QString&amp;)),
          this,
          SLOT(updateName(const QString&amp;)));
  connect(button, SIGNAL(clicked()), this,
          SLOT(accept()));
  resize(120, 90);
}
</pre>
<p>In Qt, every widget (&quot;control&quot; in Windows-speak) has a parent.
If the parent is <tt class="literal">0</tt>, the widget is a
top-level window. A top-level window has any number of child
widgets, nested to any extent. It is very rare to write destructors
or to call <tt class="literal">delete</tt> on widgets in Qt,
because when a top-level window is removed, it recursively deletes
all its children automatically.</p>
<p>We set the widget's caption; for a top-level widget this is the
window title. We then create the child widgets we need. In our case
we are going to refer to the label in another method, so we've kept
a private pointer to it. Since we want the widgets to appear
vertically one above the other we create a vertical box layout and
add each widget to it. The extra parameters (5, 10), are used to
specify the layout's margins and spacing. Qt also provides a
horizontal box layout and a grid layout; layouts and widgets can be
arbitrarily nested. Qt also supports absolute positioning, but
layouts are more convenient since they automatically adapt to the
widgets they contain; for example labels will grow to accommodate
longer text.</p>
<p>The Qt <tt class="methodname">connect()</tt> method takes a
<tt class="classname">QObject</tt> (all Qt widgets are <tt class=
"classname">QObject</tt>s), and associates one of its signals with
a slot in another <tt class="classname">QObject</tt>. In our case
we've said that whenever the line editor's text is changed our
updateName() slot should be called. The <tt class=
"classname">QDialog</tt> base class provides an <tt class=
"methodname">accept()</tt> slot which is called when the window is
closed and accepted, and a <tt class="methodname">reject()</tt>
slot which is called when the window is closed and rejected, for
example when the user clicks the window's X button, or presses Esc.
Our second connection tells Qt to call the <tt class=
"methodname">accept()</tt> slot when the Quit button is &quot;clicked&quot;
(by whatever means). When the window is accepted (or rejected),
because of the <tt class="methodname">setMainWidget()</tt> call in
<tt class="function">main()</tt>, the application will
terminate.</p>
<pre class="programlisting">
void Window::updateName(const QString &amp;name) {
  if(!name.isEmpty())
    label-&gt;setText(&quot;Hello &quot; + name);
}
</pre>
<p>The <tt class="methodname">updateName()</tt> slot is trivial.
And that completes our GUI application - in just 72 lines of
code.</p>
<p>But what about <tt class="literal">Q_OBJECT</tt>, <tt class=
"literal">public slots</tt>, and other Qt keywords?</p>
<p>The C preprocessor turns them all into pure C++ so the compiler
never sees them. The machinery to handle Qt's extensions is
generated by the moc (the Meta Object Compiler) which reads your
source files and generates some additional source files to
implement what you've used. Adding the rules for moc to a makefile
isn't very difficult, but the majority of Qt users use Qt project
files (<tt class="filename">.pro</tt> files). For this example we
would put <tt class="filename">main.cpp</tt>, <tt class=
"filename">window.h</tt>, and <tt class="filename">window.cpp</tt>
in a directory of their own; then we'd run qmake <tt class=
"literal">-project</tt> to generate the project file, then
<span class="application">qmake</span> to generate the makefile,
then make as usual.</p>
<p>In later installments we'll explore Qt further, seeing how to
create <tt class="classname">QObject</tt> subclasses with our own
signals and slots, applications with menus and toolbars, and how to
create custom widgets.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
