Sophie

Sophie

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

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 4 - Let There Be Widgets</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Qt Jambi Tutorial 4 - Let There Be Widgets<br /><small></small></h1>
<p align="center"><img src="images/tutorial4-example.png" alt="Screenshot of Chapter 4" /></p><p>This example shows how to create your own widget, and describes how to control the minimum and maximum sizes of a widget.</p>
<pre>    public class Widgets extends QWidget
    {
        public Widgets()
        {
            setFixedSize(200, 120);

            QPushButton quit = new QPushButton(tr(&quot;Quit&quot;), this);
            quit.setGeometry(62, 40, 75, 30);
            quit.setFont(new QFont(&quot;Times&quot;, 18, QFont.Weight.Bold.value()));

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

            setWindowTitle(tr(&quot;Let There Be Widgets&quot;));
        }

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

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

            QApplication.exec();
        }
    }</pre>
<a name="line-by-line-walkthrough"></a>
<h2>Line by Line Walkthrough</h2>
<pre>    public class Widgets extends QWidget
    {</pre>
<p>Here we create a new class. Because this class inherits from <a href="gui/QWidget.html"><tt>QWidget</tt></a>, the new class is a widget and may be a top-level window or a child widget (like the <a href="gui/QPushButton.html"><tt>QPushButton</tt></a> in the previous chapter).</p>
<pre>        public Widgets()
        {</pre>
<p>This class has only one member, a constructor (in addition to the members it inherits from <a href="gui/QWidget.html"><tt>QWidget</tt></a>). This widget is created without a parent. Parentless widgets are top-level windows in Qt Jambi, i.e&#x2e;, they are shown in separate windows on the screen. You can set a parent in the constructor of <a href="gui/QWidget.html"><tt>QWidget</tt></a> or with the <a href="gui/QWidget.html"><tt>QWidget</tt></a>.setParent() method. A widget's parent is also set to the widget to which it is added.</p>
<pre>            setFixedSize(200, 120);</pre>
<p>Because this widget doesn't know how to handle resizing, we fix its size. In the next chapter, we will show how a widget can respond to resize event from the user.</p>
<pre>            QPushButton quit = new QPushButton(tr(&quot;Quit&quot;), this);
            quit.setGeometry(62, 40, 75, 30);
            quit.setFont(new QFont(&quot;Times&quot;, 18, QFont.Weight.Bold.value()));</pre>
<p>Here we create and set up a child widget of this widget (the new widget's parent is <tt>this</tt>, i.e&#x2e; the <tt>Widgets</tt> instance).</p>
<p>The tr() function call around the string literal &quot;Quit&quot; marks the string for translation, making it possible to change it at run-time based on the contents of a translation file. It is a good habit to use tr() around all user-visible strings, in case you decide later to translate your application to other languages.</p>
<p>The <a href="gui/QWidget.html"><tt>QWidget</tt></a>.setGeometry() call sets both the widget's screen position and the size. It is equivalent to calling <a href="gui/QWidget.html"><tt>QWidget</tt></a>.move() followed by <a href="gui/QWidget.html"><tt>QWidget</tt></a>.resize(). If we didn't set the widgets geometry, the parent widget will position it using the buttons <a href="porting4.html#qwidget">sizeHint()</a> method - all widgets are able to calculate their preferred size.</p>
<pre>        public static void main(String args[])
        {
            QApplication.initialize(args);

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

            QApplication.exec();
        }</pre>
<p>Here we instantiate a Widgets, show it, and execute the application.</p>
<a name="running-the-application"></a>
<h2>Running the Application</h2>
<p>This program is very similar in behavior to the previous one. The difference lies in the way we have implemented it. It does behave slightly differently, however. Just try to resize it to see.</p>
<a name="exercises"></a>
<h2>Exercises</h2>
<p>Try to create another <tt>Widgets</tt> object in <tt>main()</tt>. What happens?</p>
<p>Try to add more buttons or put in other widgets than <a href="gui/QPushButton.html"><tt>QPushButton</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>