Sophie

Sophie

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

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 5 - Building Blocks</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Qt Jambi Tutorial 5 - Building Blocks<br /><small></small></h1>
<p align="center"><img src="images/tutorial5-example.png" alt="Screenshot of Chapter 5" /></p><p>This example shows how to create and connect together several widgets by using signals and slots, and how to handle resizes.</p>
<pre>    public class Blocks extends QWidget
    {
        public Blocks()
        {
            QPushButton quit = new QPushButton(tr(&quot;Quit&quot;));
            quit.setFont(new QFont(&quot;Times&quot;, 18, QFont.Weight.Bold.value()));

            QLCDNumber lcd = new QLCDNumber(2);
            lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Filled);

            QSlider slider = new QSlider(Qt.Orientation.Horizontal);
            slider.setRange(0, 99);
            slider.setValue(0);

            quit.clicked.connect(QApplication.instance(), &quot;quit()&quot;);
            slider.valueChanged.connect(lcd, &quot;display(int)&quot;);

            QVBoxLayout layout = new QVBoxLayout();
            layout.addWidget(quit);
            layout.addWidget(lcd);
            layout.addWidget(slider);
            setLayout(layout);
            setWindowTitle(tr(&quot;Building Blocks&quot;));
        }

        public static void main(String args[])
        {
            QApplication.initialize(args);

            Blocks widget = new Blocks();
            widget.show();

            QApplication.exec();
        }
    }</pre>
<a name="line-by-line-walkthrough"></a>
<h2>Line by Line Walkthrough</h2>
<pre>        public Blocks()
        {
            QPushButton quit = new QPushButton(tr(&quot;Quit&quot;));
            quit.setFont(new QFont(&quot;Times&quot;, 18, QFont.Weight.Bold.value()));

            QLCDNumber lcd = new QLCDNumber(2);
            lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Filled);</pre>
<p><tt>lcd</tt> is a <a href="gui/QLCDNumber.html"><tt>QLCDNumber</tt></a>, a widget that displays numbers in an LCD-like fashion. This instance is set up to display two digits. We set the <a href="gui/gui/QLCDNumber.html#segmentStyle()"><tt>QLCDNumber.segmentStyle</tt></a> property to <tt>QLCDNumber.SegmentStyle.Filled</tt> to make the LCDs more readable.</p>
<pre>            QSlider slider = new QSlider(Qt.Orientation.Horizontal);
            slider.setRange(0, 99);
            slider.setValue(0);</pre>
<p>The user can use the <a href="gui/QSlider.html"><tt>QSlider</tt></a> widget to adjust an integer value in a range. Here we create a horizontal one, set its minimum value to 0, its maximum value to 99, and its initial value to 0.</p>
<pre>            slider.valueChanged.connect(lcd, &quot;display(int)&quot;);</pre>
<p>Here we use the <a href="qtjambi-signalsandslots.html">signals and slots</tt></a> mechanism to connect the slider's <a href="porting4.html#qslider">valueChanged</a> signal to the LCD number's display() slot.</p>
<p>Whenever the slider's value changes it broadcasts the new value by emitting the <a href="porting4.html#qslider">valueChanged</a> signal. Because that signal is connected to the LCD number's display() slot, the slot is called when the signal is broadcast. Neither of the objects knows about the other. This is essential in component programming.</p>
<p>Slots are otherwise normal Java member methods and follow the normal Java access rules.</p>
<pre>            QVBoxLayout layout = new QVBoxLayout();
            layout.addWidget(quit);
            layout.addWidget(lcd);
            layout.addWidget(slider);
            setLayout(layout);</pre>
<p><tt>Blocks</tt> uses a <a href="gui/QVBoxLayout.html"><tt>QVBoxLayout</tt></a> to manage the geometry of its child widgets. For that reason, we don't need to specify the screen coordinates for each widget like we did in Chapter 4. In addition, using a layout ensures that the child widgets are resized when the window is resized. Then we add the <tt>quit</tt>, <tt>lcd</tt> and <tt>slider</tt> widgets to the layout using <a href="gui/QBoxLayout.html"><tt>QBoxLayout</tt></a>.addWidget().</p>
<p>The <a href="gui/QWidget.html"><tt>QWidget</tt></a>.setLayout() function installs the layout on <tt>Blocks</tt>. The call to <a href="porting4.html#qwidget">setLayout()</a> automatically sets the parent of the widgets in the layout so that they are children of <tt>Blocks</tt>. Because of this, we didn't need to specify <tt>this</tt> as the parent for the <tt>quit</tt>, <tt>lcd</tt> and <tt>slider</tt> widgets.</p>
<p>In Qt Jambi, widgets are either children of other widgets (e.g&#x2e; <tt>this</tt>), or they have no parent. A widget can be <i>added</i> to a layout, in which case the layout becomes responsible for managing the geometry of that widget, but the layout can never act as a parent itself. Indeed, <a href="gui/QWidget.html"><tt>QWidget</tt></a>'s constructor takes a <a href="gui/QWidget.html"><tt>QWidget</tt></a> pointer for the parent, and <a href="gui/QLayout.html"><tt>QLayout</tt></a> doesn't inherit from <a href="gui/QWidget.html"><tt>QWidget</tt></a>.</p>
<a name="running-the-application"></a>
<h2>Running the Application</h2>
<p>The LCD number reflects everything you do to the slider, and the widget handles resizing well. Notice that the LCD number widget changes in size when the window is resized (because it can), but the others stay about the same (because otherwise they would look strange).</p>
<a name="exercises"></a>
<h2>Exercises</h2>
<p>Try changing the LCD number to add more digits or to change mode (<a href="gui/QLCDNumber.html"><tt>QLCDNumber</tt></a>.setMode()). You can even add four push buttons to set the number base.</p>
<p>You can also change the slider's range.</p>
<p>Perhaps it would have been better to use a <a href="gui/QSpinBox.html"><tt>QSpinBox</tt></a> than a slider?</p>
<p>Try to make the application quit when the LCD number overflows.</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>