Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > a6711891ce757817bba854bf3f25205a > files > 2392

qtjambi-doc-4.3.3-3mdv2008.1.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /home/gvatteka/dev/qtjambi/4.3/scripts/../doc/src/examples/tutorial.qdoc -->
<head>
  <title>Qt Jambi Tutorial 2 - Calling it Quits</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Qt Jambi Tutorial 2 - Calling it Quits<br /><small></small></h1>
<p align="center"><img src="images/tutorial2-example.png" alt="Screenshot of Chapter 2" /></p><p>Having created a window in Chapter 1,</tt> we will now go on to make the application quit properly when the user tells it to.</p>
<p>We will also use a font that is more exciting than the default one. We give the entire source code of the application:</p>
<pre>    public class Quit
    {
        public static void main(String args[])
        {
            QApplication.initialize(args);

            QPushButton quit = new QPushButton(&quot;Quit&quot;);
            quit.resize(80, 40);
            quit.setFont(new QFont(&quot;Times&quot;, 18, QFont.Weight.Bold.value()));

            quit.clicked.connect(QApplication.instance(), &quot;quit()&quot;);

            quit.setWindowTitle(&quot;Calling It Quits&quot;);
            quit.show();
            QApplication.exec();
        }
    }</pre>
<a name="line-by-line-walkthrough"></a>
<h2>Line by Line Walkthrough</h2>
<pre>            QPushButton quit = new QPushButton(&quot;Quit&quot;);</pre>
<p>This time, the button says <b>Quit</b> and that's exactly what the program will do when the user clicks the button.</p>
<pre>            quit.resize(80, 40);</pre>
<p>We've chosen another size for the button since the text is a bit shorter than &quot;Hello world!&quot;. We could also have used <a href="gui/QFontMetrics.html"><tt>QFontMetrics</tt></a> to set right size, or let <a href="gui/QPushButton.html"><tt>QPushButton</tt></a> choose a reasonable default.</p>
<pre>            quit.setFont(new QFont(&quot;Times&quot;, 18, QFont.Weight.Bold.value()));</pre>
<p>Here we choose a new font for the button, an 18-point bold font from the Times family. It is also possible to change the default font for the entire application, using QApplication::setFont(). We fetch the value from the Weight enum as the weight of the font is given as an int in the constructor.</p>
<pre>            quit.clicked.connect(QApplication.instance(), &quot;quit()&quot;);</pre>
<p>We connect the clicked signal to the <tt>quit()</tt> slot in <a href="gui/QApplication.html"><tt>QApplication</tt></a> (<a href="porting4.html#qapplication"><tt>QApplication.</tt></a>instance() returns the application's unique <a href="gui/QApplication.html"><tt>QApplication</tt></a> instance). <tt>clicked</tt> is an instance of the Signal0 class and <tt>quit()</tt> is a regular method in <a href="gui/QApplication.html"><tt>QApplication</tt></a> that quits the application. When <tt>connect()</tt> is invoked a one-way connection between the two QtJambiObjects is established. After the slot is connected to the signal the quit() method is invoked when a method on the signal is invoked; this is called emitting the signal. In this case, the application will exit when the user clicks on the button with the mouse.</p>
<p>Every Qt Jambi object can have both <tt>signals</tt> (to send messages) and <tt>slots</tt> (to receive messages). All widgets are Qt Jambi objects, since they inherit <a href="gui/QWidget.html"><tt>QWidget</tt></a>, which indirectly inherits QtJambiObject.</p>
<p>This signal and slot mechanism is perhaps the most central feature of Qt Jambi. The <a href="qtjambi-signalsandslots.html">Signals and Slots</tt></a> documentation describes this topic in detail.</p>
<a name="running-the-application"></a>
<h2>Running the Application</h2>
<p>When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.</p>
<p>See Chapter 1</tt> for how to compile and run the application.</p>
<a name="exercises"></a>
<h2>Exercises</h2>
<p>Try to resize the window. Press the button to close the application.</p>
<p>Are there any other signals in <a href="gui/QPushButton.html"><tt>QPushButton</tt></a> you can connect to quit? [Hint: The <a href="gui/QPushButton.html"><tt>QPushButton</tt></a> inherits most of its functionality from <a href="gui/QAbstractButton.html"><tt>QAbstractButton</tt></a>.]</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2007 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Jambi </div></td>
</tr></table></div></address></body>
</html>