📄 t6.html
字号:
<!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 6: Building Blocks Galore!</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 6: Building Blocks Galore!</h1><br clear="all"><p><center><img src="t6.png" alt="Screenshot of tutorial six"></center><p>This example shows how to encapsulate two widgets into a new component andhow easy it is to use many widgets. For the first time, we use a customwidget as a child widget.<p><a name=main></a> <pre>/******************************************************************** Qt tutorial 6******************************************************************/#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a href="qslider-h.html">qslider.h</a>>#include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>>#include <<a href="qfont-h.html">qfont.h</a>>#include <<a href="qvbox-h.html">qvbox.h</a>>#include <<a href="qgrid-h.html">qgrid.h</a>>class LCDRange : public QVBox{public: LCDRange( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );};LCDRange::LCDRange( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qvbox.html">QVBox</a>( parent, name ){ <a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" ); <a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" ); slider-><a href="qrangecontrol.html#f2409c">setRange</a>( 0, 99 ); slider-><a href="qslider.html#a03234">setValue</a>( 0 ); <a href="qobject.html#fbde73">connect</a>( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) );}class MyWidget : public QVBox{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="qvbox.html">QVBox</a>( parent, name ){ <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" ); quit-><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()) ); <a href="qgrid.html">QGrid</a> *grid = new <a href="qgrid.html">QGrid</a>( 4, this ); for( int c = 0 ; c < 4 ; c++ ) for( int r = 0 ; r < 4 ; r++ ) (void)new LCDRange( grid );}int main( int argc, char **argv ){ <a href="qapplication.html">QApplication</a> a( argc, argv ); MyWidget w; a.<a href="qapplication.html#7ad759">setMainWidget</a>( &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 LCDRange : public QVBox { public: LCDRange( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 ); };</pre><p>The LCDRange widget is a widget without any API. It just has aconstructor. This sort of widget is not very useful, so later onwe'll add some API. <pre> LCDRange::LCDRange( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qvbox.html">QVBox</a>( parent, name ) { <a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" ); <a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" ); slider-><a href="qrangecontrol.html#f2409c">setRange</a>( 0, 99 ); slider-><a href="qslider.html#a03234">setValue</a>( 0 ); <a href="qobject.html#fbde73">connect</a>( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) ); }</pre><p>This is lifted straight from the <ahref="t5.html#constructor">MyWidget constructor</a> in chapter five.The only differences are that the button is left out and the classrenamed. <pre> class MyWidget : public QVBox { public: MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 ); };</pre><p>MyWidget, too, contains no API except a constructor. <pre> MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qvbox.html">QVBox</a>( parent, name ) { <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" ); quit-><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()) );</pre><p>The push button that used to be in what's now LCDRange has beenseparated, so that we can have one "Quit" button and many LCDRangeobjects. <pre> <a href="qgrid.html">QGrid</a> *grid = new <a href="qgrid.html">QGrid</a>( 4, this );</pre><p>We create a QGrid object with four columns. The QGRid widgetautomatically arranges its children in rows and columns; you canspecify the number of rows or of columns, and QGrid will discover itsnew children and fit them into the grid. <pre> for( int c = 0 ; c < 4 ; c++ ) for( int r = 0 ; r < 4 ; r++ ) (void)new LCDRange( grid );</pre><p>Four columns, for rows.<p>We create 4*4 LCDRanges, all of which children of the grid object.The QGrid widget will arrange them. <pre> }</pre><p>That's all.<p><h2>Behavior</h2><p>This program shows how easy it is to use many widgets at a time. Eachsingle one behaves like the slider and LCD number in the previouschapter. Again, the difference lies in the implementation.<p><h2>Exercises</h2><p>Initialize each slider with a different/random value on startup.<p>The source contains three occurrences of "4". What happens if youchange the one in the <a href="qgrid.html">QGrid</a> constructor call? What about the othertwo? Why is this?<p>You may now go on to <a href="t7.html">chapter seven.</a><p>[<a href="t5.html">Previous tutorial</a>][<a href="t7.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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -