You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
8.6 KiB
183 lines
8.6 KiB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
|
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial.doc:378 -->
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>TQt Tutorial - Chapter 4: Let There Be Widgets</title>
|
|
<style type="text/css"><!--
|
|
fn { margin-left: 1cm; text-indent: -1cm; }
|
|
a:link { color: #004faf; text-decoration: none }
|
|
a:visited { color: #672967; text-decoration: none }
|
|
body { background: #ffffff; color: black; }
|
|
--></style>
|
|
</head>
|
|
<body>
|
|
|
|
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
|
<tr bgcolor="#E5E5E5">
|
|
<td valign=center>
|
|
<a href="index.html">
|
|
<font color="#004faf">Home</font></a>
|
|
| <a href="classes.html">
|
|
<font color="#004faf">All Classes</font></a>
|
|
| <a href="mainclasses.html">
|
|
<font color="#004faf">Main Classes</font></a>
|
|
| <a href="annotated.html">
|
|
<font color="#004faf">Annotated</font></a>
|
|
| <a href="groups.html">
|
|
<font color="#004faf">Grouped Classes</font></a>
|
|
| <a href="functions.html">
|
|
<font color="#004faf">Functions</font></a>
|
|
</td>
|
|
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>TQt Tutorial - Chapter 4: Let There Be Widgets</h1>
|
|
|
|
|
|
<p> <center><img src="t4.png" alt="Screenshot of tutorial four"></center>
|
|
<p> This example shows how to create your own widget, describes how to control the
|
|
minimum and maximum sizes of a widget, and introduces widget names.
|
|
<p> <pre>/****************************************************************
|
|
**
|
|
** TQt tutorial 4
|
|
**
|
|
****************************************************************/
|
|
|
|
#include <<a href="qapplication-h.html">ntqapplication.h</a>>
|
|
#include <<a href="tqpushbutton-h.html">tqpushbutton.h</a>>
|
|
#include <<a href="tqfont-h.html">tqfont.h</a>>
|
|
|
|
|
|
class MyWidget : public <a href="tqwidget.html">TQWidget</a>
|
|
{
|
|
public:
|
|
MyWidget( <a href="tqwidget.html">TQWidget</a> *parent=0, const char *name=0 );
|
|
};
|
|
|
|
|
|
<a name="f552"></a>MyWidget::MyWidget( <a href="tqwidget.html">TQWidget</a> *parent, const char *name )
|
|
: <a href="tqwidget.html">TQWidget</a>( parent, name )
|
|
{
|
|
<a href="tqwidget.html#setMinimumSize">setMinimumSize</a>( 200, 120 );
|
|
<a href="tqwidget.html#setMaximumSize">setMaximumSize</a>( 200, 120 );
|
|
|
|
<a href="tqpushbutton.html">TQPushButton</a> *quit = new <a href="tqpushbutton.html">TQPushButton</a>( "Quit", this, "quit" );
|
|
quit-><a href="tqwidget.html#setGeometry">setGeometry</a>( 62, 40, 75, 30 );
|
|
quit-><a href="tqwidget.html#setFont">setFont</a>( TQFont( "Times", 18, TQFont::Bold ) );
|
|
|
|
<a href="tqobject.html#connect">connect</a>( quit, TQ_SIGNAL(<a href="tqbutton.html#clicked">clicked</a>()), tqApp, TQ_SLOT(<a href="ntqapplication.html#quit">quit</a>()) );
|
|
}
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
{
|
|
<a href="ntqapplication.html">TQApplication</a> a( argc, argv );
|
|
|
|
MyWidget w;
|
|
w.<a href="tqwidget.html#setGeometry">setGeometry</a>( 100, 100, 200, 120 );
|
|
a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>( &w );
|
|
w.<a href="tqwidget.html#show">show</a>();
|
|
return a.<a href="ntqapplication.html#exec">exec</a>();
|
|
}
|
|
</pre>
|
|
|
|
|
|
|
|
<p> <h2> Line-by-line Walkthrough
|
|
</h2>
|
|
<a name="1"></a><p> <pre> class MyWidget : public <a href="tqwidget.html">TQWidget</a>
|
|
{
|
|
public:
|
|
MyWidget( <a href="tqwidget.html">TQWidget</a> *parent=0, const char *name=0 );
|
|
};
|
|
</pre>
|
|
<p> Here we create a new class. Because this class inherits from <a href="tqwidget.html">TQWidget</a>,
|
|
the new class is a widget and may be a top level window or a child
|
|
widget (like the push button in Chapter 3).
|
|
<p> This class has only one member, a constructor (in addition to the
|
|
members it inherits from TQWidget). The constructor is a standard TQt
|
|
widget constructor; you should always include a similar constructor
|
|
when you create widgets.
|
|
<p> The first argument is its parent widget. To create a top-level window
|
|
you specify a null pointer as the parent. As you can see, this widget
|
|
defaults to be a top-level window.
|
|
<p> The second argument is the widget's name. This is <em>not</em> the text
|
|
that appears in the window's title bar or in the button. It is a name
|
|
associated with a widget to make it possible to <a href="tqobject.html#queryList">look up</a> this widget later, and there is
|
|
also a <a href="tqobject.html#dumpObjectTree">handy debugging function</a> that will list a complete widget hierarchy.
|
|
<p> <pre> MyWidget::MyWidget( <a href="tqwidget.html">TQWidget</a> *parent, const char *name )
|
|
: <a href="tqwidget.html">TQWidget</a>( parent, name )
|
|
</pre>
|
|
<p> The implementation of the constructor starts here. Like most widgets,
|
|
it just passes on the <tt>parent</tt> and <tt>name</tt> to the <a href="tqwidget.html">TQWidget</a>
|
|
constructor.
|
|
<p> <pre> {
|
|
<a href="tqwidget.html#setMinimumSize">setMinimumSize</a>( 200, 120 );
|
|
<a href="tqwidget.html#setMaximumSize">setMaximumSize</a>( 200, 120 );
|
|
</pre>
|
|
<p> Because this widget doesn't know how to handle resizing, we fix its size
|
|
by setting the minimum and maximum to be equal. In the next chapter
|
|
we will show how a widget can respond to resize event from the user.
|
|
<p> <pre> <a href="tqpushbutton.html">TQPushButton</a> *quit = new <a href="tqpushbutton.html">TQPushButton</a>( "Quit", this, "quit" );
|
|
<a name="x2308"></a> quit-><a href="tqwidget.html#setGeometry">setGeometry</a>( 62, 40, 75, 30 );
|
|
<a name="x2307"></a> quit-><a href="tqwidget.html#setFont">setFont</a>( TQFont( "Times", 18, TQFont::Bold ) );
|
|
</pre>
|
|
<p> Here we create and set up a child widget of this widget (the new widget's
|
|
parent is <tt>this</tt>) which has the widget name "quit". The widget
|
|
name has nothing to do with the button text; it just happens to be
|
|
similar in this case.
|
|
<p> Note that <tt>quit</tt> is a local variable in the constructor. MyWidget
|
|
does not keep track of it, but TQt does, and will by default delete it
|
|
when MyWidget is deleted. This is why MyWidget doesn't need a
|
|
destructor. (On the other hand, there is no harm in deleting a child
|
|
when you choose to, the child will automatically tell TQt about its
|
|
imminent death.)
|
|
<p> The setGeometry() call does the same as move() and resize() did in the
|
|
previous chapters.
|
|
<p> <pre> <a name="x2306"></a><a name="x2304"></a> <a href="tqobject.html#connect">connect</a>( quit, TQ_SIGNAL(<a href="tqbutton.html#clicked">clicked</a>()), tqApp, TQ_SLOT(<a href="ntqapplication.html#quit">quit</a>()) );
|
|
}
|
|
</pre>
|
|
<p> Because the MyWidget class doesn't know about the application object, it
|
|
has to connect to TQt's pointer to it, <tt>tqApp</tt>.
|
|
<p> A widget is a software component and should know as little as possible
|
|
about its environment in order to be as general and reusable as
|
|
possible.
|
|
<p> Knowing the name of the application object would break this principle,
|
|
so TQt offers an alias, tqApp, for the cases in which a component such as
|
|
MyWidget needs to talk to the application object.
|
|
<p> <pre> int main( int argc, char **argv )
|
|
{
|
|
<a href="ntqapplication.html">TQApplication</a> a( argc, argv );
|
|
|
|
MyWidget w;
|
|
w.<a href="tqwidget.html#setGeometry">setGeometry</a>( 100, 100, 200, 120 );
|
|
<a name="x2305"></a> a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>( &w );
|
|
<a name="x2309"></a> w.<a href="tqwidget.html#show">show</a>();
|
|
<a name="x2303"></a> return a.<a href="ntqapplication.html#exec">exec</a>();
|
|
}
|
|
</pre>
|
|
<p> Here we instantiate our new child, set it to be the main widget, and
|
|
execute the application.
|
|
<p> <h2> Behavior
|
|
</h2>
|
|
<a name="2"></a><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> (See <a href="tutorial1-01.html#compiling">Compiling</a> for how to create a
|
|
makefile and build the application.)
|
|
<p> <h2> Exercises
|
|
</h2>
|
|
<a name="3"></a><p> Try to create another MyWidget object in main(). What happens?
|
|
<p> Try to add more buttons or put in widgets other than <a href="tqpushbutton.html">TQPushButton</a>.
|
|
<p> You're now ready for <a href="tutorial1-05.html">Chapter 5.</a>
|
|
<p> [<a href="tutorial1-03.html">Previous tutorial</a>]
|
|
[<a href="tutorial1-05.html">Next tutorial</a>]
|
|
[<a href="tutorial.html">Main tutorial page</a>]
|
|
<p>
|
|
<!-- eof -->
|
|
<p><address><hr><div align=center>
|
|
<table width=100% cellspacing=0 border=0><tr>
|
|
<td>Copyright © 2007
|
|
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
|
<td align=right><div align=right>TQt 3.3.8</div>
|
|
</table></div></address></body>
|
|
</html>
|