<?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 3 - Family Values</title> <link href="classic.css" rel="stylesheet" type="text/css" /> </head> <body> <h1 align="center">Qt Jambi Tutorial 3 - Family Values<br /><small></small></h1> <p align="center"><img src="images/tutorial3-example.png" alt="Screenshot of Chapter 3" /></p><p>This example shows how to create parent and child widgets.</p> <p>We'll keep it simple and use just a single parent and a lone child.</p> <pre> public class FamilyValues { public static void main(String args[]) { QApplication.initialize(args); QWidget window = new QWidget(); window.resize(200, 120); QPushButton quit = new QPushButton("Quit", window); quit.setFont(new QFont("Times", 18, QFont.Weight.Bold.value())); quit.setGeometry(10, 40, 180, 40); quit.clicked.connect(QApplication.instance(), "quit()"); window.setWindowTitle("FamilyValues"); window.show(); QApplication.exec(); } }</pre> <a name="line-by-line-walkthrough"></a> <h2>Line by Line Walkthrough</h2> <pre> QWidget window = new QWidget();</pre> <p>Here we simply create a plain widget object. The <a href="gui/QWidget.html"><tt>QWidget</tt></a> class is the base class of all user interface objects. The widget is the atom of the user interface: It receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen. A widget is clipped by its parent and by the widgets in front of it.</p> <p>A widget that isn't embedded in a parent widget, like this particular widget, is called a window. Usually, windows have their own window frame and taskbar entry, provided by the window system. A widget without a parent widget is always an independent window. Its initial position on the screen is controlled by the window system.</p> <pre> window.resize(200, 120);</pre> <p>We set the window's width to 200 pixels and its height to 120 pixels.</p> <pre> QPushButton quit = new QPushButton("Quit", window);</pre> <p>A child is born. This <a href="gui/QPushButton.html"><tt>QPushButton</tt></a> is created with a parent widget <tt>(window)</tt>. A child widget is always displayed within its parent's area. When displayed, it is clipped by its parent's bounds. By default, it is rooted at the top-left corner of its parent, at position (0, 0).</p> <pre> quit.setGeometry(10, 40, 180, 40);</pre> <p>The <a href="gui/QWidget.html"><tt>QWidget</tt></a>.setGeometry() function takes four arguments: The first two arguments are the x and y coordinates of the button's top-left corner. The coordinates are relative to the parent widget. The last two arguments are the button's width and height. The result is a button that extends from position (10, 40) to position (190, 80).</p> <pre> window.show();</pre> <p>When a parent widget is shown, it will call show for all its children (except those that were explicitly hidden using <a href="gui/QWidget.html"><tt>QWidget</tt></a>.hide()).</p> <a name="running-the-application"></a> <h2>Running the Application</h2> <p>The button no longer fills the entire window. Instead, it stays at position (10, 40) within the window and with a size of (180, 40), because of the <a href="gui/QWidget.html"><tt>QWidget</tt></a>.setGeometry() call.</p> <a name="exercises"></a> <h2>Exercises</h2> <p>Try resizing the window. How does the button change? What happens to the button's height if you run the program with a bigger font? What happens if you try to make the window really small?</p> <p /><address><hr /><div align="center"> <table width="100%" cellspacing="0" border="0"><tr class="address"> <td width="30%">Copyright © 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>