📄 t1.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 1: Hello, World!</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 1: Hello, World!</h1><br clear="all"><p><center><img src="t1.png" alt="Screenshot of tutorial one"></center><p>This first program is a simple hello-world example. It contains onlythe bare minimum you need to get a Qt application up and running.The picture above is a snapshot of this program. <pre>/******************************************************************** Qt tutorial 1******************************************************************/#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>int main( int argc, char **argv ){ <a href="qapplication.html">QApplication</a> a( argc, argv ); <a href="qpushbutton.html">QPushButton</a> hello( "Hello world!", 0 ); hello.<a href="qpushbutton.html#13fb8e">resize</a>( 100, 30 ); a.<a href="qapplication.html#7ad759">setMainWidget</a>( &hello ); hello.<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> #include <<a href="qapplication-h.html">qapplication.h</a>></pre><p>This line includes the QApplication class definition. There has to beexactly one QApplication object in every application that uses Qt.QApplication manages various application-wide resources, such as thedefault font and cursor. <pre> #include <<a href="qpushbutton-h.html">qpushbutton.h</a>></pre><p>This line includes the QPushButton class definition. The <ahref="hierarchy.html">reference documentation</a> for each classmentions at the top which file needs to be included to use that class.<p>QPushButton is a classical GUI push button, which the user can pressand release. It manages its own look and feel, like every other <a href="qwidget.html">QWidget</a>. A widget is a user interface object which can process userinput and draw graphics. The programmer can change both the overall<a href="qapplication.html#e52522">look and feel</a> and many minorproperties of it such as color, as well as the widget's content. AQPushButton can show either a text or a <a href="qpixmap.html">QPixmap</a>. <pre> int main( int argc, char **argv ) {</pre><p>The <code>main()</code> function is the entry point to the program. Almost alwayswhen using Qt, main() only needs to perform some kind of initializationbefore passing the control to the Qt library, which then tells theprogram about the user's actions via events.<p><code>argc</code> is the number of command-line arguments and <code>argv</code> is the arrayof command-line arguments. This is a C/C++ feature. It is not Qt specific,however, Qt needs to process these arguments (see below). <pre> <a href="qapplication.html">QApplication</a> a( argc, argv );</pre><p><code>a</code> is this program's QApplication. Here it is created and processessome of the command-line arguments (such as -display under X11).Note that all command-line arguments recognized by Qt are removed from<code>argv</code> (and <code>argc</code> is decremented accordingly). See the <a href="qapplication.html#c35457">QApplication::argv()</a> documentation fordetails.<p><strong>Note:</strong> It is essential that the QApplication object iscreated before any window-system parts of Qt are used. <pre> <a href="qpushbutton.html">QPushButton</a> hello( "Hello world!", 0 );</pre><p>Here, <em>after</em> the QApplication, comes the first window-system code: Apush button is created.<p>The button is set up to display the text "Hello world!" and be awindow of its own (since the constructor specifies 0 for the parentwindow which the button should be inside). <pre> hello.<a href="qpushbutton.html#13fb8e">resize</a>( 100, 30 );</pre><p>The button is set up to be 100 pixels wide and 30 pixels high (plus thewindow system frame). In this case we don't care about the button'sposition and accept the default value. <pre> a.<a href="qapplication.html#7ad759">setMainWidget</a>( &hello );</pre><p>The push button is chosen as the main widget for the application. Ifthe user closes a main widget, the application exits.<p>You don't have to have a main widget, but most programs have one. <pre> hello.<a href="qwidget.html#200ee5">show</a>();</pre><p>A widget is never visible when you create it. You must call show() tomake it visible. <pre> return a.<a href="qapplication.html#84c7bf">exec</a>();</pre><p>This is where <code>main()</code> passes control to Qt. exec() will return whenthe application exits.<p>In exec(), Qt receives and processes user and system events and passesthese on to the appropriate widgets. <pre> }</pre><p><h2>Behavior</h2><p>You should now try to compile and run this program.<p>When you run it, you will see a small window filled with a singlebutton, and on it you can read the famous words, Hello World!<p><h2>Exercises</h2><p>Try to resize the window. Press the button. If you're running X11,try running the program with the -geometry option(e.g. -geometry 100x200+10+20)<p>You may now go on to <a href="t2.html">chapter two.</a><p>[<a href="t2.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 + -