t4.html

来自「qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人」· HTML 代码 · 共 176 行

HTML
176
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Tutorial - Chapter 4: Let There Be Widgets</title></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b>  <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><p><h1 align=center>Chapter 4: Let There Be Widgets</h1><br clear="all"><p><center><img src="t4.png" alt="Screenshot of tutorial four"></center><p>This example shows how to create your own widget, how to control theminimum and maximum sizes of a widget, and introduces widget names. <pre>/******************************************************************** Qt tutorial 4******************************************************************/#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;#include &lt;<a href="qfont-h.html">qfont.h</a>&gt;class MyWidget : public QWidget{public:    MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );};MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )        : <a href="qwidget.html">QWidget</a>( parent, name ){    <a href="qwidget.html#c0b5fb">setMinimumSize</a>( 200, 120 );    <a href="qwidget.html#c78dce">setMaximumSize</a>( 200, 120 );    <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );    quit-&gt;<a href="qpushbutton.html#c9b88e">setGeometry</a>( 62, 40, 75, 30 );    quit-&gt;<a href="qwidget.html#090d60">setFont</a>( <a href="qfont.html">QFont</a>( "Times", 18, QFont::Bold ) );    <a href="qobject.html#fbde73">connect</a>( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );}int main( int argc, char **argv ){    <a href="qapplication.html">QApplication</a> a( argc, argv );    MyWidget w;    w.<a href="qwidget.html#9ede68">setGeometry</a>( 100, 100, 200, 120 );    a.<a href="qapplication.html#7ad759">setMainWidget</a>( &amp;w );    w.<a href="qwidget.html#200ee5">show</a>();    return a.<a href="qapplication.html#84c7bf">exec</a>();}</pre><p><h2>Line by Line Walk-Through</h2>  <pre>    class MyWidget : public QWidget    {    public:        MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );    };</pre><p>Here we create a new class.  Since this class inherits from QWidget,the new class is a widget, and may be a top level window or a childwidget (like the push button in chapter three).<p>This class has only one member, a constructor (in addition to themembers it inherits from QWidget).  The constructor is a standard Qtwidget constructor; you should always include a similar constructorwhen you create widgets.<p>The first argument is its parent widget.  To create a top level windowyou specify a null pointer as the parent.  As you can see, this widgetdefaults to be a top level window.<p>The second argument is the widget's name.  This is <em>not</em> the textthat appears in the window's title bar or in the button.  It is a nameassociated with a widget to make it possible to <a href="qobject.html#b0bff0">look up</a> this widget later, and there isalso a <a href="qobject.html#c21cea">handy debugging function</a> that will list a complete widget hierarchy. <pre>    MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )            : <a href="qwidget.html">QWidget</a>( parent, name )</pre><p>The implementation of the constructor starts here.  Like most widgets,it just passes on the <code>parent</code> and <code>name</code> to the QWidgetconstructor. <pre>    {        <a href="qwidget.html#c0b5fb">setMinimumSize</a>( 200, 120 );        <a href="qwidget.html#c78dce">setMaximumSize</a>( 200, 120 );</pre><p>Since this widget doesn't know how to handle resizing, we fix its sizeby 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. <pre>        <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );        quit-&gt;<a href="qpushbutton.html#c9b88e">setGeometry</a>( 62, 40, 75, 30 );        quit-&gt;<a href="qwidget.html#090d60">setFont</a>( <a href="qfont.html">QFont</a>( "Times", 18, QFont::Bold ) );</pre><p>Here we create and set up a child widget of this widget (the new widget'sparent is <code>this)</code> which has the widget name "quit".  The widgetname has nothing to do with the button text, they just happen to besimilar in this case.<p>Note that <code>quit</code> is a local variable in the constructor.  MyWidgetdoes not keep track of it, but Qt does, and will by default delete itwhen MyWidget is deleted.  This is why MyWidget doesn't need adestructor.  (On the other hand, there is no harm in deleting a childwhen you choose to, the child will automatically tell Qt about itsimminent death.)<p>The setGeometry() call does the same as move() and resize() did in theprevious chapters. <pre>        <a href="qobject.html#fbde73">connect</a>( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );    }</pre><p>Since the MyWidget class doesn't know about the application object, ithas to connect to Qt's pointer to it, <code>qApp.</code><p>A widget is a software component and should know as little as possibleabout its environment in order to be as general and reusable aspossible.<p>Knowing the name of the application object would break this principle,so Qt offers an alias, qApp, for the cases where a component such asMyWidget needs to talk to the application object. <pre>    int main( int argc, char **argv )    {        <a href="qapplication.html">QApplication</a> a( argc, argv );            MyWidget w;        w.<a href="qwidget.html#9ede68">setGeometry</a>( 100, 100, 200, 120 );        a.<a href="qapplication.html#7ad759">setMainWidget</a>( &amp;w );        w.<a href="qwidget.html#200ee5">show</a>();        return a.<a href="qapplication.html#84c7bf">exec</a>();    }</pre><p>Here we instantiate our new baby, set it to be the main widget, andexecute the application.<p><h2>Behavior</h2><p>This program is very similar in behavior to the previous one.  Thedifference lies in the way we have implemented it.  It does behaveslightly different though.  Just try to resize it to see.<p><h2>Exercises</h2><p>Try to create another MyWidget object in main().  What happens?<p>Try to add more buttons, or put in widgets other than QPushButton.<p>You may now go on to <a href="t5.html">chapter five.</a><p>[<a href="t3.html">Previous tutorial</a>][<a href="t5.html">Next tutorial</a>][<a href="tutorial.html">Main tutorial page</a>]<p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?