Sophie

Sophie

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

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 6 - Building Blocks Galore!</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Qt Jambi Tutorial 6 - Building Blocks Galore!<br /><small></small></h1>
<p align="center"><img src="images/tutorial6-example.png" alt="Screenshot of Chapter 6" /></p><p>This example shows how to encapsulate two widgets into a new component and how easy it is to use many widgets. For the first time, we use a custom widget as a child widget.</p>
<pre>    public class BlocksGalore extends QWidget
    {
        public BlocksGalore()
        {
            QPushButton quit = new QPushButton(tr(&quot;Quit&quot;));
            quit.setFont(new QFont(&quot;Times&quot;, 18, QFont.Weight.Bold.value()));

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

            QGridLayout grid = new QGridLayout();
            QVBoxLayout layout = new QVBoxLayout();
            layout.addWidget(quit);
            layout.addLayout(grid);
            setLayout(layout);
            setWindowTitle(tr(&quot;Building Blocks Galore&quot;));

            for (int row = 0; row &lt; 3; ++row) {
                for (int column = 0; column &lt; 3; ++column) {
                    LCDRange lcdRange = new LCDRange();
                    grid.addWidget(lcdRange, row, column);
                }
            }
        }

        class LCDRange extends QWidget
        {
            public LCDRange()
            {
                QLCDNumber lcd = new QLCDNumber(2);
                lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Filled);

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

                slider.valueChanged.connect(lcd, &quot;display(int)&quot;);

                QVBoxLayout layout = new QVBoxLayout();
                layout.addWidget(lcd);
                layout.addWidget(slider);
                setLayout(layout);
            }
        }

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

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

            QApplication.exec();
        }
    }</pre>
<a name="line-by-line-walkthrough"></a>
<h2>Line by Line Walkthrough</h2>
<pre>        class LCDRange extends QWidget
        {</pre>
<p>The <tt>LCDRange</tt> widget is a widget without any API. It just has a constructor. This sort of widget is not very useful, so we'll add some API later.</p>
<pre>            public LCDRange()
            {
                QLCDNumber lcd = new QLCDNumber(2);
                lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Filled);

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

                slider.valueChanged.connect(lcd, &quot;display(int)&quot;);

                QVBoxLayout layout = new QVBoxLayout();
                layout.addWidget(lcd);
                layout.addWidget(slider);
                setLayout(layout);
            }</pre>
<p>The code in the constructor is lifted straight from the <tt>Blocks</tt> constructor in Chapter 5. The only differences are that the <b>Quit</b> button is left out and the class is renamed.</p>
<p>The <tt>BlocksGalore</tt> class, too, contains no API except a constructor:</p>
<pre>    public class BlocksGalore extends QWidget
    {
        public BlocksGalore()
        {
            QPushButton quit = new QPushButton(tr(&quot;Quit&quot;));
            quit.setFont(new QFont(&quot;Times&quot;, 18, QFont.Weight.Bold.value()));

            quit.clicked.connect(QApplication.instance(), &quot;quit()&quot;);</pre>
<p>The push button that used to be in what is now <tt>LCDRange</tt> has been separated so that we can have one <b>Quit</b> button and many <tt>LCDRange</tt> objects.</p>
<pre>            QGridLayout grid = new QGridLayout();
            QVBoxLayout layout = new QVBoxLayout();
            layout.addWidget(quit);
            layout.addLayout(grid);
            setLayout(layout);
            setWindowTitle(tr(&quot;Building Blocks Galore&quot;));</pre>
<p>We create a <a href="gui/QWidget.html"><tt>QWidget</tt></a> with a <a href="gui/QGridLayout.html"><tt>QGridLayout</tt></a> that will contain three columns. The <a href="gui/QGridLayout.html"><tt>QGridLayout</tt></a> automatically arranges its widgets in rows and columns; you can specify the row and column numbers when adding widgets to the layout, and <a href="gui/QGridLayout.html"><tt>QGridLayout</tt></a> will fit them into the grid.</p>
<p>We use a <a href="gui/QVBoxLayout.html"><tt>QVBoxLayout</tt></a> to lay out the <b>Quit</b> button and the grid layout. The <a href="gui/QWidget.html"><tt>QWidget</tt></a>.addLayout() method is similar to the <a href="gui/QWidget.html"><tt>QWidget</tt></a>.addWidget() method, making the given layout a child of the main layout.</p>
<p>We then proceed to create the LCDRange widgets:</p>
<pre>            for (int row = 0; row &lt; 3; ++row) {
                for (int column = 0; column &lt; 3; ++column) {
                    LCDRange lcdRange = new LCDRange();
                    grid.addWidget(lcdRange, row, column);
                }
            }
        }</pre>
<p>We create nine <tt>LCDRange</tt> widgets, and we arrange them in three rows and three columns. We set the parent of the widgets to the BlocksGalore instance in the constructor. If we didn't, the parent of the widgets would be set when the layout is installed on the widget with <a href="porting4.html#qwidget">setLayout()</a>. For this reason, it might be a good idea to always set a parent to a widget in its constructor.</p>
<p>That's all, folks!</p>
<a name="running-the-application"></a>
<h2>Running the Application</h2>
<p>This program shows how easy it is to use many widgets at a time. Each one behaves like the slider and LCD number in the previous chapter. Again, the difference lies in the implementation.</p>
<a name="exercises"></a>
<h2>Exercises</h2>
<p>Initialize each slider with a different/random value on startup.</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>