⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t7.html

📁 qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人员可以用来资料查询.
💻 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 7: One Thing Leads to Another</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 7: One Thing Leads to Another</h1><br clear="all"><p><center><img src="t7.png" alt="Screenshot of tutorial seven"></center><p>This example shows how to create custom widgets with signals andslots, and how to connect them together in more complex ways.  For thefirst time, the source is split among several files.<p><ul><li><a href="t7-lcdrange-h.html">lcdrange.h</a> contains the LCDRangeclass definition<li><a href="t7-lcdrange-cpp.html">lcdrange.cpp</a> contains the LCDRangeimplementation<li><a href="t7-main-cpp.html">main.cpp</a> contains MyWidget and main.<li><a href="t7-makefile.html">Makefile</a> contains some rules forgenerating the meta object information necessary for <ahref="signalsandslots.html">signal/slot creation.</a></ul><p><h2>Line by Line Walk-Through</h2><p><h3><a href="t7-lcdrange-h.html">lcdrange.h</a></h3><p>This file is mainly lifted from <a href="t6.html#main">main.cpp</a> inchapter 6 and only the changes are noted here.   <pre>    #ifndef LCDRANGE_H    #define LCDRANGE_H</pre><p>This is the classic C construction to avoid errors if a header filehappens to be included more than once.  If you don't use it already:It is a very good habit.  The #ifndef should enclose <em>all</em> of theheader file. <pre>    #include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;</pre><p><code>qvbox.h</code> is included.  LCDRange inherits QVBox, and the header fileof a parent class must always be included.  We cheated a bit in theprevious chapters, and let <code>qwidget.h</code> be included indirectly viaother header files like <code>qpushbutton.h.</code> <pre>    class QSlider;</pre><p>This is another classic trick, but one that's much less used.  Sincewe don't need QSlider in the <em>interface</em> of the class, only in theimplementation, we use a forward declaration of the class in theheader file, and #include the header file for QSlider in the .cppfile.<p>This makes compilation of big projects much faster, since when aheader file has changed, fewer files need to be recompiled.  It canoften speed up big compilations by a factor of two or more.  <pre>    class LCDRange : public QVBox    {        Q_OBJECT    public:        LCDRange( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );</pre><p>Note the Q_OBJECT.  This macro must be included in <em>all</em> classes thatcontain signals and/or slots.  If you are curious, it defines thefunctions that are implemented in the<a href="metaobjects.html">meta object file.</a> <pre>        int value() const;    public slots:        void setValue( int );        signals:        void valueChanged( int );</pre><p>These three members make up an interface between this widget and othercomponents in a program.  Until now, LCDRange didn't really have aninterface at all.<p>value() is a public function for accessing the value of the LCDRange.setValue() is our first custom slot and valueChanged() is our firstcustom signal.<p>Slots must be implemented in the normal way (remember, a slot is alsoa C++ member function).  Signals are automatically implemented in the<a href="signalsandslots.html">meta object</a> file.  Signals follow theaccess rules of protected C++ functions, i.e.  they can only be emittedby the class they are defined in or by classes inheriting from it.<p>The signal valueChanged() is used when the LCDRange's value haschanged - just as you guessed from the name.  This is not the lastsignal you'll see called <i>something</i>Changed().<p><h3><a href="t7-lcdrange-cpp.html">lcdrange.cpp</a></h3> <p>This file is mainly lifted from <a href="t6.html#main">t6/main.cpp</a> andonly the changes are noted here.  <pre>        <a href="qobject.html#fbde73">connect</a>( slider, SIGNAL(valueChanged(int)),                 lcd, SLOT(display(int)) );        <a href="qobject.html#fbde73">connect</a>( slider, SIGNAL(valueChanged(int)),                 SIGNAL(valueChanged(int)) );</pre><p>This code is from the LCDRange constructor.<p>The first connect is the same you've seen in the previous chapter.The second is new: It connects slider's valueChanged() signal to thisobject's valueChanged <em>signal.</em>  connect() with 3 arguments alwaysconnect to signals or slots in <code>this</code> object.<p>Yes, that's right.  Signals can be connected to other signals.  Whenthe first is emitted, the second signal is also emitted.<p>Let's look at what happens when the user operates the slider: Theslider sees that its value has changed, and emits the valueChanged()signal.  That signal is connected both to the display() slot of theQLCDNumber and to the valueChanged() signal of the LCDRange.<p>Thus, when the signal is emitted, LCDRange emits its ownvalueChanged() signal.  In addition, QLCDNumber::display() is calledand shows the new number.<p>Note that you're not guaranteed any particular order of execution -LCDRange::valueChanged() may be emitted before or afterQLCDNumber::display(), entirely arbitrarily.  <pre>    int LCDRange::value() const    {        return slider-&gt;value();    }</pre><p>The implementation of value() is straight forward, it simply returnsthe slider's value. <pre>    void LCDRange::setValue( int value )    {        slider-&gt;setValue( value );    }</pre><p>The implementation of setValue() is equally straight forward.  Notethat since the slider and LCD number are connected, setting theslider's value automatically updates the LCD number as well.  Inaddition, the slider will automatically adjust the value if it isoutside its legal range.<p><h3><a href="t7-main-cpp.html">main.cpp</a></h3>   <pre>        LCDRange *previous = 0;        for( int r = 0 ; r &lt; 4 ; r++ ) {            for( int c = 0 ; c &lt; 4 ; c++ ) {                LCDRange* lr = new LCDRange( grid );                if ( previous )                    <a href="qobject.html#fbde73">connect</a>( lr, SIGNAL(valueChanged(int)),                             previous, SLOT(setValue(int)) );                previous = lr;            }        }</pre><p>All of main.cpp is copied from the previous chapter, except that inthe constructor for MyWidget, when we create the 16 LCDRange object wenow connect them together using the <a href="signalsandslots.html">signal/slot</a> mechanism.  Each one has its valueChanged() signalconnected to the setValue() slot in the previous one.  Since LCDRangeemits the signal valueChanged() when its value changes (surprise!), weare here creating a "chain" of signals and slots.<p><h2>Behavior</h2><p>On startup, the program's appearance is identical to the previous one.Try operating the slider to the bottom right...<p><h2>Exercises</h2><p>Use the bottom right slider to set all LCDs to 50.  Then set the tophalf to 40 by clicking once to the left of the slider handle.  Now,use the one to the left of the last one operated to set the firstseven LCDs back to 50.<p>Click to the left of the handle on the bottom right slider.  Whathappens?  Why is this the correct behavior?<p>You may now go on to <a href="t8.html">chapter eight.</a><p>[<a href="t6.html">Previous tutorial</a>][<a href="t8.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 + -