﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-LIULIANG-随笔分类-QT</title><link>http://www.cppblog.com/LIULIANG/category/19059.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 19 Sep 2012 19:48:11 GMT</lastBuildDate><pubDate>Wed, 19 Sep 2012 19:48:11 GMT</pubDate><ttl>60</ttl><item><title>Qt学习之路(50): QString </title><link>http://www.cppblog.com/LIULIANG/archive/2012/09/15/190818.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Sat, 15 Sep 2012 14:47:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/09/15/190818.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/190818.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/09/15/190818.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/190818.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/190818.html</trackback:ping><description><![CDATA[<div><p>这段时间回家，一直没有来得及写，今天才发现博客的编辑器有了新版。还是先来试试新版编辑器的功能吧！</p> <p>今天要说的是QString。之所以把QString单独拿出来，是因为string是很常用的一个数据结构，甚至在很多语言中，比如JavaScript，都是把string作为一种同int等一样的基本数据结构来实现的。</p> <p>每一个GUI程序都需要string，这些string可以用在界面上的提示语，也可以用作一般的数据结构。C++语言提供了两种字符串的实现：C 风格的字符串，以'\0&#8216;结尾；std::string，即标准模版库中的类。Qt则提供了自己的字符串实现：QString。QString以16位 Uniode进行编码。我们平常用的ASCII等一些编码集都作为Unicode编码的子集提供。关于编码的问题，我们会到以后的时候再详细说明。</p> <p>在使用QString的时候，我们不需要担心内存分配以及关于'\0'结尾的这些注意事项。QString会把这些问题解决。通常，你可以把 QString看作是一个QChar的向量。另外，与C风格的字符串不同，QString中间是可以包含'\0'符号的，而length()函数则会返回 整个字符串的长度，而不仅仅是从开始到'\0'的长度。</p> <p>同Java的String类类似，QString也重载的+和+=运算符。这两个运算符可以把两个字符串连接到一起，正像Java里面的操作一样。QString可以自动的对占用内存空间进行扩充，这种连接操作是恨迅速的。下面是这两个操作符的使用：</p> <pre><ol><li><span>QString&nbsp;str&nbsp;=&nbsp;"User:&nbsp;"; &nbsp;</span></li><li>str&nbsp;+=&nbsp;userName&nbsp;+&nbsp;"\n";&nbsp;</li></ol></pre> <p>QString的append()函数则提供了类似的操作，例如：</p> <pre><ol><li><span>str&nbsp;=&nbsp;"User:&nbsp;"; &nbsp;</span></li><li>str.append(userName); &nbsp;</li><li>str.append("\n");&nbsp;</li></ol></pre> <p>C语言中有printf()函数作为格式化输出，QString则提供了一个sprintf()函数实现了相同的功能：</p> <pre><ol><li><span>str.sprintf("%s&nbsp;%.1f%%",&nbsp;"perfect&nbsp;competition",&nbsp;100.0);&nbsp;</span></li></ol></pre> <p>这句代码将输出：perfect competition 100.0%，同C语言的printf()一样。不过前面我们也见到了Qt提供的另一种格式化字符串输出的函数arg():</p> <pre><ol><li><span>str&nbsp;=&nbsp;QString("%1&nbsp;%2&nbsp;(%3s-%4s)") &nbsp;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.arg("permissive").arg("society").arg(1950).arg(1970);&nbsp;</li></ol></pre> <p>这段代码中，%1, %2, %3,  %4作为占位符，将被后面的arg()函数中的内容依次替换，比如%1将被替换成permissive，%2将被替换成society，%3将被替换成 1950，%4将被替换曾1970，最后，这句代码输出为：permissive society (1950s-1970s).  arg()函数比起sprintf()来是类型安全的，同时它也接受多种的数据类型作为参数，因此建议使用arg()函数而不是传统的 sprintf()。</p> <p>使用static的函数number()可以把数字转换成字符串。例如：</p> <pre><ol><li><span>QString&nbsp;str&nbsp;=&nbsp;QString::number(54.3);&nbsp;</span></li></ol></pre> <p>你也可以使用非static函数setNum()来实现相同的目的：</p> <pre><ol><li><span>QString&nbsp;str; &nbsp;</span></li><li>str.setNum(54.3);&nbsp;</li></ol></pre> <p>而一系列的to函数则可以将字符串转换成其他基本类型，例如toInt(), toDouble(), toLong()等。这些函数都接受一个bool指针作为参数，函数结束之后将根据是否转换成功设置为true或者false：</p> <pre><ol><li><span>bool&nbsp;ok; &nbsp;</span></li><li>double&nbsp;d&nbsp;=&nbsp;str.toDouble(&amp;ok); &nbsp;</li><li>if(ok) &nbsp;</li><li>{ &nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;do&nbsp;something... &nbsp;</li><li>}&nbsp;else&nbsp;{ &nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;do&nbsp;something... &nbsp;</li><li>}&nbsp;</li></ol></pre> <p>对于QString，Qt提供了很多操作函数，例如，使用mid()函数截取子串：</p> <pre><ol><li><span>QString&nbsp;x&nbsp;=&nbsp;"Nine&nbsp;pineapples"; &nbsp;</span></li><li>QString&nbsp;y&nbsp;=&nbsp;x.mid(5,&nbsp;4);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;y&nbsp;==&nbsp;"pine" &nbsp;</li><li>QString&nbsp;z&nbsp;=&nbsp;x.mid(5);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;z&nbsp;==&nbsp;"pineapples"&nbsp;</li></ol></pre> <p>mid()函数接受两个参数，第一个是起始位置，第二个是取串的长度。如果省略第二个参数，则会从起始位置截取到末尾。正如上面的例子显示的那样。</p> <p>函数left()和rigt()类似，都接受一个int类型的参数n，都是对字符串进行截取。不同之处在于，left()函数从左侧截取n个字符，而right()从右侧开始截取。下面是left()的例子：</p> <pre><ol><li><span>QString&nbsp;x&nbsp;=&nbsp;"Pineapple"; &nbsp;</span></li><li>QString&nbsp;y&nbsp;=&nbsp;x.left(4);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;y&nbsp;==&nbsp;"Pine"&nbsp;</li></ol></pre> <p>函数indexOf()返回字符串的位置，如：</p> <pre><ol><li><span>QString&nbsp;x&nbsp;=&nbsp;"sticky&nbsp;question"; &nbsp;</span></li><li>QString&nbsp;y&nbsp;=&nbsp;"sti"; &nbsp;</li><li>x.indexOf(y);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;returns&nbsp;0 &nbsp;</li><li>x.indexOf(y,&nbsp;1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;returns&nbsp;10 &nbsp;</li><li>x.indexOf(y,&nbsp;10);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;returns&nbsp;10 &nbsp;</li><li>x.indexOf(y,&nbsp;11);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;returns&nbsp;-1&nbsp;</li></ol></pre> <p>函数startsWith()和endsWith()可以检测字符串是不是以某个特定的串开始或结尾，例如：</p> <pre><ol><li><span>if&nbsp;(url.startsWith("http:")&nbsp;&amp;&amp;&nbsp;url.endsWith(".png")) &nbsp;</span></li><li>{ &nbsp;</li><li>}&nbsp;</li></ol></pre> <p>这段代码等价于</p> <pre><ol><li><span>if&nbsp;(url.left(5)&nbsp;==&nbsp;"http:"&nbsp;&amp;&amp;&nbsp;url.right(4)&nbsp;==&nbsp;".png") &nbsp;</span></li><li>{ &nbsp;</li><li>}&nbsp;</li></ol></pre> <p>不过，前者要比后者更加清楚简洁，并且性能也更快一些。</p> <p>QString还提供了replace()函数供实现字符串的替换功能；trimmed()函数去除字符串两侧的空白字符(注意，空白字符包括空 格、Tab以及换行符，而不仅仅是空格)；toLower()和toUpper()函数会将字符串转换成小写大写字符串；remove()和 insert()函数提供了删除和插入字符串的能力；simplified()函数可以将串中的所有连续的空白字符替换成一个，并且把两端的空白字符去 除，例如"&nbsp;&nbsp; \t&nbsp;&nbsp; &#8221;会返回一个空格" "。</p> <p>将const char *类型的C风格字符串转换成QString也是很常见的需求，简单来说，QString的+=即可完成这个功能：</p> <pre><ol><li><span>str&nbsp;+=&nbsp;"&nbsp;(1870)";&nbsp;</span></li></ol></pre> <p>这里，我们将const char * 类型的字符串"  (1870)"转换成为QString类型。如果需要显式的转换，可以使用QString的强制转换操作，或者是使用函数fromAscii()等。为了 将QString类型转成const char  *字符串，需要进行两步操作，一是使用toAscii()获得一个QByteArray类型对象，然后调用它的data()或者constData()函 数，例如：</p> <pre><ol><li><span>printf("User:&nbsp;%s\n",&nbsp;str.toAscii().data());&nbsp;</span></li></ol></pre> <p>为了方便使用，Qt提供了一个宏qPrintable()，这个宏等价于toAscii().constData()，例如：</p> <pre><ol><li><span>printf("User:&nbsp;%s\n",&nbsp;qPrintable(str));&nbsp;</span></li></ol></pre> <p>我们调用QByteArray类上面的data()或者constData()函数，将获得QByteArray 内部的一个const  char*类型的字符串，因此，我们不需要担心内存泄漏等的问题，Qt会替我们管理好内存。不过这也暗示我们，注意不要使用这个指针太长时间，因为如果 QByteArray被delete，那么这个指针也就成为野指针了。如果这个QByteArray对象没有被放在一个变量中，那么当语句结束 后，QbyteArray对象就会被delete，这个指针也就被delete 了。</p></div>转自：<div>http://devbean.blog.51cto.com/448512/275360/</div><img src ="http://www.cppblog.com/LIULIANG/aggbug/190818.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-09-15 22:47 <a href="http://www.cppblog.com/LIULIANG/archive/2012/09/15/190818.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ubuntu opera问题解决－－E: 软件包 opera 需要重新安装，但是我无法找到相应的安装文件。 </title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/15/175027.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Tue, 15 May 2012 14:43:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/15/175027.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/175027.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/15/175027.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/175027.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/175027.html</trackback:ping><description><![CDATA[<p>前几天因感受到ubuntu中firefox太不給力，于是下了个opera11.1的安装包向爽一下，结果美梦半路夭折，今天因安装gfortran困扰在opera的半成品上，不得不解决这个问题！！</p>
<p>步骤如下：</p>
<p>1。E: 软件包 opera 需要重新安装，但是我无法找到相应的安装文件。</p>
<p>用了很多办法都不理想，没有解决问题</p>
<p>2。ubuntu 清理缓存</p>
<p>&nbsp;&nbsp;&nbsp; apt-get autoclean<br />&nbsp;&nbsp;&nbsp; apt-get clean<br />&nbsp;&nbsp;&nbsp; apt-get autoremove</p>
<p><br />用了以上3条命令，无果，其中最后一条有反应了，忘了什么反应了。感叹下ubuntu 确实好用，可惜我还不够级别来使你臣服阿！</p>
<p>3。搜索关键此：ubuntu opera安装</p>
<p>想重新安装下，覆盖掉，不行阿，ubuntu 太严密了！</p>
<p>&nbsp;</p>
<p>4。打开新立德软件库，出现以下内容：E: 软件包 opera 需要重新安装，但是我无法找到相应的安装文件。 E: 打开高速缓存(1)时有内部错误。请报告。</p>
<p>找到最终解决方案：命令：sudo dpkg --remove --force-remove-reinstreq opera</p>
<p>当然这个要在关闭新历得软件库之后，不然会报错：dpkg: 另外一个进程已经为状态数据库加了锁。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>还好终于搞定了，真是的，我的主要任务可不是高opera！！fortran去了！</p><img src ="http://www.cppblog.com/LIULIANG/aggbug/175027.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-15 22:43 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/15/175027.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>无法获得锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable) </title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/15/175025.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Tue, 15 May 2012 14:43:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/15/175025.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/175025.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/15/175025.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/175025.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/175025.html</trackback:ping><description><![CDATA[刚刚装完Ubantu 10.04，配置好网络连接后，发现更新软件源时，会出现无法&#8220;reload&#8221;错误提示！同样在用&#8220;sudo apt-get update&#8221;更新软件源时，会出现"E: 无法获得锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)"错误提示！这时心头一惊，怎么会出现错误呢？我什么配置还都没修改呢！相信有许多好友也出现了同样的问题，现在来共享一下我的解决方案。（仅供参考） <br /><br />先解释一下问题出现的原因：刚装好的Ubantu系统，内部缺少很多软件源，这时，系统会自动启动软件源更新进程&#8220;apt-get&#8221;，并且它会一直存活。由于它在运行时，会占用软件源更新时的系统锁（以下称&#8220;系统更新锁&#8221;，此锁文件在&#8220;/var/lib/apt/lists/&#8221;目录下），而当有新的 apt-get进程生成时，就会因为得不到系统更新锁而出现"E: 无法获得锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)"错误提示！因此，我们只要将原先的apt-get进程杀死，从新激活新的apt-get进程，就可以让新立德软件管理器正常工作了！ <br /><br />这时，你可以按一下步骤来解决： <br />1、输入以下命令：&#8220;ps -aux&nbsp; &gt; temp.txt&#8221;.敲回车确定。这时会有&#8220;warning&#8221;出现，但不予理会。 <br />2、输入以下命令：&#8220;grep -n apt-get temp.txt&#8221;键入回车确定。而后从命令输出中找到 apt-get 进程的PID。 <br />3、输入以下命令：&#8220;sudo kill &lt;PID&gt;&#8221;。 <br />4、完成。 <br /><br />or: <br />如果提示错误:E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable) <br />输入 <br />sudo rm /var/lib/apt/lists/lock <br />即可  <img src ="http://www.cppblog.com/LIULIANG/aggbug/175025.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-15 22:43 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/15/175025.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt学习笔记（Qt3库）下</title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/13/174796.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Sun, 13 May 2012 15:23:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/13/174796.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/174796.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/13/174796.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/174796.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/174796.html</trackback:ping><description><![CDATA[<p><font style="font-size: 18px"><font style="font-size: 18px"></font>9-1.cpp</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qapplication.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qwidget.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qpainter.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">class MyMainWindow:public QWidget</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> void paintEvent(QPaintEvent *);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPainter *paint;</font></p>
<p align="left"><font style="font-size: 18px">};</font></p>
<p align="left"><font style="font-size: 18px">//此函数是由系统调用的，每当第一次生成或被挡住，它都会再次被调用</font></p>
<p align="left"><font style="font-size: 18px">void MyMainWindow::paintEvent(QPaintEvent *)</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //此句最好做成局部变量的定义，否则将导致重复申请动态内存</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint=new QPainter;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;begin(this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //设置边框颜色、线形和线的宽度</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;setPen(QPen(blue,10,QPen::SolidLine));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //设置边框内部颜色全部填充为红色</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;setBrush(QBrush(red,SolidPattern));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //绘制矩形</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;drawRect(20,20,160,160);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //绘图介绍</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;end();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">MyMainWindow::MyMainWindow()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //设置主窗口坐标位置、大小</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> setGeometry(100,100,200,200);</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">int main(int argc,char **argv)</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QApplication a(argc,argv);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow w;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.setMainWidget(&amp;w);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> w.show();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.exec();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">//**************************************************************</font></p>
<p align="left"><font style="font-size: 18px">对上一个例子做一个扩展，画一个4*4棋盘</font></p>
<p><font style="font-size: 18px">9-1-1.cpp</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qapplication.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qwidget.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qpainter.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">class MyMainWindow:public QWidget</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //绘图函数声明</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> void paintEvent(QPaintEvent *);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPainter *paint;</font></p>
<p align="left"><font style="font-size: 18px">};</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">void MyMainWindow::paintEvent(QPaintEvent *)</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> int i;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> int x=20,y=20;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint=new QPainter;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;begin(this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;setPen(QPen(blue,4,QPen::SolidLine));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;setBrush(QBrush(white,SolidPattern));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //画5条横线</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> for(i=0;i&lt;5;i++)</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> {</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;drawLine(20,y+80*i,340,y+80*i);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //画5条竖线</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> for(i=0;i&lt;5;i++)</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> {</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;drawLine(x+80*i,20,x+80*i,340);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 在棋盘正中做两个红棋子</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;setBrush(QBrush(red,SolidPattern));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;drawEllipse(100,100,80,80);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;drawEllipse(180,180,80,80);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 在棋盘正中做两个蓝棋子</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;setBrush(QBrush(green,SolidPattern));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;drawEllipse(180,100,80,80);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;drawEllipse(100,180,80,80);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> paint-&gt;end();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">MyMainWindow::MyMainWindow()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> setGeometry(100,100,400,400);</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">int main(int argc,char **argv)</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QApplication a(argc,argv);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow w;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.setMainWidget(&amp;w);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> w.show();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.exec();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">//**************************************************************</font></p>
<p align="left"><font style="font-size: 18px">在主窗口上加一个标签，在标签上做出一个动画。</font></p>
<p><font style="font-size: 18px">15-1.h</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qapplication.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qwidget.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qlabel.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qmovie.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qpushbutton.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qfont.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qlayout.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">class MyMainWindow:public QWidget</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //宏定义</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> Q_OBJECT;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow(char *);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QMovie movie;&nbsp;<wbr>&nbsp;<wbr> //定义动画的对象</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public slots:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> void UnPause(); //继续播放</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> void Pause();&nbsp;<wbr>&nbsp;<wbr> //暂停</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> void addSpeed();&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //加速动画的播放速度</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> void decSpeed();&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //减慢动画的播放速度</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> private:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> unsigned long num;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QLabel *label;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b1;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b2;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b3;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b4;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b5;</font></p>
<p align="left"><font style="font-size: 18px">};</font></p>
<p><font style="font-size: 18px">15-1.cpp</font></p>
<p align="left"><font style="font-size: 18px">#include "15-1.h"</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">void MyMainWindow::UnPause()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> movie.unpause();&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //继续播放</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">void MyMainWindow::Pause()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> movie.pause();&nbsp;<wbr> //暂停播放</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">void MyMainWindow::addSpeed()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> num+=20;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> movie.setSpeed(num);&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //以20%增加速度</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">void MyMainWindow::decSpeed()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> num-=20;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> movie.setSpeed(num);&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //以20%减少</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">//str里存放动画的文件名</font></p>
<p align="left"><font style="font-size: 18px">MyMainWindow::MyMainWindow(char *str):movie(str)&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> setGeometry(100,100,300,280);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> num=100;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //初始速度为100</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b1=new QPushButton("Continue!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b1-&gt;setGeometry(130,20,120,40);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b1-&gt;setMinimumSize(40,20);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b1-&gt;setFont(QFont("Times",18,QFont::Bold));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b2=new QPushButton("Pause!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b2-&gt;setGeometry(130,70,120,40);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b2-&gt;setMinimumSize(40,20);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b2-&gt;setFont(QFont("Times",18,QFont::Bold));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b3=new QPushButton("Hurry!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b3-&gt;setGeometry(130,120,120,40);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b3-&gt;setMinimumSize(40,20);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b3-&gt;setFont(QFont("Times",18,QFont::Bold));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b4=new QPushButton("Slow!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b4-&gt;setGeometry(130,170,120,40);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b4-&gt;setMinimumSize(40,20);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b4-&gt;setFont(QFont("Times",18,QFont::Bold));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b5=new QPushButton("Quit!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b5-&gt;setGeometry(130,220,120,40);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b5-&gt;setMinimumSize(40,20);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b5-&gt;setFont(QFont("Times",18,QFont::Bold));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> label=new QLabel(this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> label-&gt;setGeometry(10,10,120,60);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> label-&gt;setMovie(movie);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QVBoxLayout *vbox=new QVBoxLayout(this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> vbox-&gt;addWidget(label);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> vbox-&gt;addWidget(b1);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> vbox-&gt;addWidget(b2);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> vbox-&gt;addWidget(b3);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> vbox-&gt;addWidget(b4);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> vbox-&gt;addWidget(b5);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(b1,SIGNAL(clicked()),this,SLOT(UnPause()));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(b2,SIGNAL(clicked()),this,SLOT(Pause()));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(b3,SIGNAL(clicked()),this,SLOT(addSpeed()));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(b4,SIGNAL(clicked()),this,SLOT(decSpeed()));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(b5,SIGNAL(clicked()),qApp,SLOT(quit()));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">int main(int argc,char **argv)</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QApplication a(argc,argv);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr> //给构造函数发送动画名，并且文件必须使.gif格式</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow w("trolltech.gif");&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.setMainWidget(&amp;w);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> w.show();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.exec();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p><br /><br />转自：<a href="http://blog.sina.com.cn/s/blog_5eedcb760100cdp3.html">http://blog.sina.com.cn/s/blog_5eedcb760100cdp3.html</a><img src ="http://www.cppblog.com/LIULIANG/aggbug/174796.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-13 23:23 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/13/174796.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt学习笔记（Qt3库）上</title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/13/174795.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Sun, 13 May 2012 15:22:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/13/174795.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/174795.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/13/174795.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/174795.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/174795.html</trackback:ping><description><![CDATA[<p><font style="font-size: 18px"><font style="font-size: 18px"></font>Qt学习笔记（Qt3库）</font></p>
<p><font style="font-size: 18px">//**************************************************************</font></p>
<p><font style="font-size: 18px">3.1创建第一个主部件</font></p>
<p><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr> 创建主部件的方法是基于QWidget或QDialog类创建一个用户类。可以使用用户类通过公有继承派生于QWidget类，在其中调用一些成员函数来定义窗口外观。</font></p>
<p><font style="font-size: 18px">题目要求：创建一个空的窗口，并显示在屏幕上。大小为200*120.窗口大小不可调整。</font></p>
<p><font style="font-size: 18px">源程序:3-1.cpp</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qapplication.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qwidget.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qpushbutton.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qlabel.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">//在类的声明里，我们只</font></p>
<p align="left"><font style="font-size: 18px">//需要包含新的成员。其它的</font></p>
<p align="left"><font style="font-size: 18px">//方法都从QWidget类中继承来：</font></p>
<p align="left"><font style="font-size: 18px">class MyMainWindow:public QWidget</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QLabel *l;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow();</font></p>
<p align="left"><font style="font-size: 18px">};</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">MyMainWindow::MyMainWindow()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //设置窗口的最大、最小尺寸</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr> //我们不需要添加this指针，</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr> //因为C++默认是现在的类</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> setMinimumSize(400,300);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> setMaximumSize(400,300);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b=new QPushButton("hello world!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b-&gt;setGeometry(20,20,160,80);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> l=new QLabel("label",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> l-&gt;setGeometry(100,90,160,93);</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">int main(int argc,char **argv)</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //创建要求的应用类对象a</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QApplication a(argc,argv);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //创建MyMainWindow类的对象，</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //并设置其为主视图</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow w;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.setMainWidget(&amp;w);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //显示主窗口对象，并且</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //它的子视图也会显示到屏幕</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> w.show();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //等待用户命令</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.exec();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">//**************************************************************</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>QT库与用户的交互方式，例如按钮、滑块，是通过使用信号和槽。信号和槽是相互关联的函数</font></p>
<p align="left"><font style="font-size: 18px">题目要求：添加退出按钮</font></p>
<p><font style="font-size: 18px">3-5.cpp</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qapplication.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qwidget.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qpushbutton.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qlabel.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">//在类的声明里，我们只</font></p>
<p align="left"><font style="font-size: 18px">//需要一个新的构造函数。</font></p>
<p align="left"><font style="font-size: 18px">//其它的成员都是从类QWidget中继承来的:</font></p>
<p align="left"><font style="font-size: 18px">class MyMainWindow:public QWidget</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QLabel *l;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow();</font></p>
<p align="left"><font style="font-size: 18px">};</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">MyMainWindow::MyMainWindow()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> setGeometry(100,100,200,170);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //setMinimumSize(400,300);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //setMaximumSize(400,300);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b=new QPushButton("Quit!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b-&gt;setGeometry(20,20,160,80);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b-&gt;setFont(QFont("Times",18,QFont::Bold));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> l=new QLabel(this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> l-&gt;setGeometry(10,110,180,50);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> l-&gt;setText("If you click the button above,</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> \nthe whole program will exit");</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> l-&gt;setAlignment(AlignCenter);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //这里按钮b的clicked()信号与qApp的槽quit()链接，当点击按钮时，将产生QPushButton：：clicked()信号，导致qApp的quit()槽被执行，从而程序退出。</font></p>
<p align="left"><font style="font-size: 18px">那么，什么是qApp呢？qApp是Qt的一个内置指针。它总是指向程序中的QApplication对象（这里指a）。//</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(b,SIGNAL(clicked()),qApp,SLOT(quit()));</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">int main(int argc,char **argv)</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QApplication a(argc,argv);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow w;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.setMainWidget(&amp;w);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> w.show();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return a.exec();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">//**************************************************************</font></p>
<p align="left"><font style="font-size: 18px">信号和槽的使用</font></p>
<p align="left"><font style="font-size: 18px">题目要求：利用滑块来改变LCD显示的值</font></p>
<p><font style="font-size: 18px">4-1.cpp</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qapplication.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qwidget.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qpushbutton.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qfont.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qlcdnumber.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qslider.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">//在类的声明里，我们只</font></p>
<p align="left"><font style="font-size: 18px">//需要一个新的构造函数。</font></p>
<p align="left"><font style="font-size: 18px">//其它的成员都是从类QWidget中继承来的:</font></p>
<p align="left"><font style="font-size: 18px">class MyMainWindow:public QWidget</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QLCDNumber *lcd;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QSlider *slider;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow();</font></p>
<p align="left"><font style="font-size: 18px">};</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">MyMainWindow::MyMainWindow()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> setGeometry(100,100,300,200);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //setMinimumSize(400,300);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //setMaximumSize(400,300);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b=new QPushButton("Quit!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b-&gt;setGeometry(10,20,80,40);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b-&gt;setFont(QFont("Times",18,QFont::Bold));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> lcd=new QLCDNumber(2,this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> lcd-&gt;setGeometry(100,10,190,180);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> slider=new QSlider(Vertical,this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> slider-&gt;setGeometry(10,60,80,130);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //下面一行代码，使点击按键1，程序退出</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(b,SIGNAL(clicked()),qApp,SLOT(quit()));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //下面一行链接滑块和显示器，使数字随着滑块的移动而改变</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(slider,SIGNAL(valueChanged(int)),lcd,SLOT(display(int)));</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">int main(int argc,char **argv)</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QApplication a(argc,argv);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow w;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.setMainWidget(&amp;w);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> w.show();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return a.exec();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">//**************************************************************</font></p>
<p align="left"><font style="font-size: 18px">用户自定义槽，需另外把自定义类做成声明函数，类中成员函数的实现可放在源函数里。</font></p>
<p><font style="font-size: 18px">4-3.h（类的声明）</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;iostream.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qapplication.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qwidget.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qpushbutton.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;qfont.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">class MyMainWindow:public QWidget</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //这个宏定义是不可缺少的</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> Q_OBJECT;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //用户自定义槽</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public slots:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> void SetValue();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> void ChangeValue();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //用户自定义信号</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> signals:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> void ValueChanged();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr> //定义两个数据成员</font></p>
<p align="left"><font style="font-size: 18px">private:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b1;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QPushButton *b2;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //构造函数</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> public:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow();</font></p>
<p align="left"><font style="font-size: 18px">};</font></p>
<p><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p><font style="font-size: 18px">类的函数成员的实现在源程序中</font></p>
<p><font style="font-size: 18px">4-3.cpp（源程序）</font></p>
<p align="left"><font style="font-size: 18px">#include &lt;4-3.h&gt;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">void MyMainWindow::SetValue()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //向自定义信号发送信号</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> emit ValueChanged();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">void MyMainWindow::ChangeValue()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> cout&lt;&lt;"test!"&lt;&lt;endl;</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">MyMainWindow::MyMainWindow()</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //设置主窗口大小</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> setGeometry(100,100,300,200);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //按键1：设置其标识符Print,</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //和坐标位置、几何大小，</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //第三句设置字体:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b1=new QPushButton("Print!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b1-&gt;setGeometry(10,20,80,40);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b1-&gt;setFont(QFont("Times",18,QFont::Bold));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //按键1：设置其标识符Quit,</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //和坐标位置、几何大小，</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //第三句设置字体:</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b2=new QPushButton("Quit!",this);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b2-&gt;setGeometry(10,80,80,40);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> b2-&gt;setFont(QFont("Times",18,QFont::Bold));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //ValueChanged收到信号，就给自定义槽发送信号，ChangeValue则打印字符串</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(this,SIGNAL(ValueChanged()),this,SLOT(ChangeValue()));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //点击1按键，向自定义槽发送信号</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(b1,SIGNAL(clicked()),this,SLOT(SetValue()));</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //点击2按键，程序退出</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> connect(b2,SIGNAL(clicked()),qApp,SLOT(quit()));</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">int main(int argc,char **argv)</font></p>
<p align="left"><font style="font-size: 18px">{</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //定义一个应用类对象</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> QApplication a(argc,argv);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr> //定义一个应用类对象</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MyMainWindow w;</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr> //设置w为主窗口</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> a.setMainWidget(&amp;w);</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> //显示w</font></p>
<p align="left"><font style="font-size: 18px">w.show();</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> return a.exec();</font></p>
<p align="left"><font style="font-size: 18px">}</font></p>
<p align="left"><font style="font-size: 18px">&nbsp;<wbr></font></p>
<p align="left"><font style="font-size: 18px">//**************************************************************<br /><br />转自：<a href="http://blog.sina.com.cn/s/blog_5eedcb760100cdst.html">http://blog.sina.com.cn/s/blog_5eedcb760100cdst.html</a></font></p><img src ="http://www.cppblog.com/LIULIANG/aggbug/174795.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-13 23:22 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/13/174795.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QTableWidget的使用和美工总结 </title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/09/174372.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Wed, 09 May 2012 14:19:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/09/174372.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/174372.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/09/174372.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/174372.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/174372.html</trackback:ping><description><![CDATA[<p>FriendTable-&gt;setFrameShape(QFrame::NoFrame);&nbsp; //设置边框</p>
<p>FriendTable-&gt;setHorizontalHeaderLabels(HeadList);&nbsp; 设置表头</p>
<p>FriendTable-&gt;setSelectionMode(QAbstractItemView::SingleSelection);&nbsp;&nbsp; 设置选择的模式为单选择</p>
<p>FriendTable-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);&nbsp;&nbsp;&nbsp; 设置选择行为时每次选择一行</p>
<p>FriendTable-&gt;setShowGrid(false);&nbsp;&nbsp; 设置不显示格子线</p>
<p>FriendTable-&gt;setFont(font);&nbsp;&nbsp; 设置字体</p>
<p>设置表单背景或透明</p>
<p>QPalette pal = musicTable-&gt;palette();<br />&nbsp;&nbsp;&nbsp; pal.setBrush(this-&gt;backgroundRole(),QBrush(QPixmap("images/background.png")) );<br />&nbsp;&nbsp;&nbsp; musicTable-&gt;setPalette( pal );</p>
<p>设置它的背景图片，也可以将QBrush初始化为QColor来设置背景颜色</p>
<p>QPalette pll = musicTable-&gt;palette();</p>
<p>pll.setBrush(QPalette::Base,QBrush(QColor(255,255,255,0)));</p>
<p>musicTable-&gt;setPalette(pll);&nbsp; //和QTextEdit一样，都可以使用样式表QPalette来修改它的背景颜色和背景图片，这里我们把刷子设置为全透明的，就可以是透明的</p>
<p><br />在QTableWidget列表中添加图片的方法&nbsp;&nbsp; <br />QTableWidgetItem *cubesHeaderItem = new QTableWidgetItem(tr("Cubes"));<br />cubesHeaderItem-&gt;setIcon(QIcon(QPixmap("1.png")));<br />cubesHeaderItem-&gt;setTextAlignment(Qt::AlignVCenter);<br />&nbsp;musicTable-&gt;setItem(1,1,cubesHeaderItem);&nbsp;&nbsp; //在第一行第一列中显示图片</p>
<p>*******************表头的属性修改****************</p>
<p>musicTable-&gt;horizontalHeader()-&gt;resizeSection(0,150);&nbsp; //修改表头第一列的宽度为150</p>
<p>&nbsp;musicTable-&gt;horizontalHeader()-&gt;setFixedHeight(25);&nbsp; //修改表头合适的高度</p>
<p>musicTable-&gt;horizontalHeader()-&gt;setStyleSheet("QHeaderView::section {background-color:lightblue;color: black;padding-left: 4px;border: 1px solid #6c6c6c;}");&nbsp;&nbsp;&nbsp; //设置表头字体，颜色，模式</p>
<p>&nbsp;FriendTable-&gt;verticalHeader()-&gt;setStyleSheet("QHeaderView::section {&nbsp; background-color:skyblue;color: black;padding-left: 4px;border: 1px solid #6c6c6c}");&nbsp;&nbsp; //设置纵列的边框项的字体颜色模式等</p>
<p>本来想找找QT里有没有现成的API的，结果没有找到，只能自己写了。<br />实现也好实现，QTableWidgetItem里面有修改背景色的API，直接调用，然后用循环控制隔行换色即可。<br />实现代码：<br />void testtt::changeColor(QTableWidget *tablewidget){<br />for (int i = 0;i &lt; tablewidget-&gt;rowCount();i++)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i % 2 == 0)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int j = 0;j &lt; tablewidget-&gt;columnCount();j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QTableWidgetItem *item = tablewidget-&gt;item(i,j);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (item)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const QColor color = QColor(252,222,156);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; item-&gt;setBackgroundColor(color);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />}</p>
<p>使用</p>
<p>向表中插入一项</p>
<p>&nbsp; QTableWidgetItem *num=new QTableWidgetItem(QTableWidgetItem::Type);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; num-&gt;setCheckState(Qt::Unchecked);&nbsp;&nbsp; //加入复选框<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; num-&gt;setIcon(QIcon("images/fetion.png"));&nbsp; //加入ICon<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; num-&gt;setText(InfoList.at(i).name);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; num-&gt;setFont(font);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; num-&gt;setTextColor(color);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; num-&gt;setFlags(num-&gt;flags() ^ Qt::ItemIsEditable);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int currentRow=FriendTable-&gt;rowCount();&nbsp; //插入到最后<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FriendTable-&gt;insertRow(currentRow);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FriendTable-&gt;setItem(currentRow,0,num);&nbsp; //插入该Item<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FriendTable-&gt;selectRow(0);&nbsp;&nbsp; 选择第一行</p>
<p>删除某一行 列</p>
<p>FriendTable-&gt;removeRow(row);</p>
<p>FriendTable-&gt;removeColumn (column );</p>
<p>信号</p>
<p>void cellActivated ( int row, int column )<br />void cellChanged ( int row, int column )<br />void cellClicked ( int row, int column )<br />void cellDoubleClicked ( int row, int column )<br />void cellEntered ( int row, int column )<br />void cellPressed ( int row, int column )<br />void currentCellChanged ( int currentRow, int currentColumn, int previousRow, int previousColumn )<br />void currentItemChanged ( QTableWidgetItem * current, QTableWidgetItem * previous )&nbsp; 改变Item了<br />void itemActivated ( QTableWidgetItem * item )<br />void itemChanged ( QTableWidgetItem * item )<br />void itemClicked ( QTableWidgetItem * item )<br />void itemDoubleClicked ( QTableWidgetItem * item )<br />void itemEntered ( QTableWidgetItem * item )<br />void itemPressed ( QTableWidgetItem * item )<br />void itemSelectionChanged ()</p>
<p><br /></p>
<p>QT QTableWidget中去掉默认自带的行号 <br /></p>
<p>使用QToolBox自动拖出来的QTableWidget控件中是自带行号的，有时候需要去掉，去年在做到这个地方的时候没有找到，今天找到了相关的方法，特记录下来。</p>
<div style="text-align: left">&nbsp;</div>
<p><br /><span>如上，刚开始的时候左边默认自带序列号。</span></p>
<p style="text-align: left">&nbsp;</p>
<p><span>QHeaderView* headerView = table的名字-&gt;verticalHeader();<br />headerView-&gt;setHidden(true);</span></p>
<p><span>加上上面的代码，就可以去掉左边的行号了。</span></p>
<p><span>http://hi.baidu.com/buptyoyo/blog/item/bb8cab3d93817ec97d1e7143.html</span></p>
<p><span style="color: rgb(255,1,2)">设置行的默认高度</span><br /></p>
<p>tableWidget-&gt;verticalHeader()-&gt;setDefaultSectionSize(height)<br /><br /><br /><br />转自：<a href="http://blog.chinaunix.net/uid-24515821-id-2129109.html">http://blog.chinaunix.net/uid-24515821-id-2129109.html</a></p><img src ="http://www.cppblog.com/LIULIANG/aggbug/174372.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-09 22:19 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/09/174372.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>让QTreeWidget中的项目有复选框( </title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/09/174369.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Wed, 09 May 2012 14:16:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/09/174369.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/174369.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/09/174369.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/174369.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/174369.html</trackback:ping><description><![CDATA[<ol><li class="alt"><span>1，调整列宽：&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;header()-&gt;setResizeMode(QHeaderView::Interactive);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;setColumnWidth(&nbsp;0&nbsp;,&nbsp;200&nbsp;);&nbsp;</span><span class="comment">//第一列宽设为200 </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>2，单击tree&nbsp;widget&nbsp;的&nbsp;header&nbsp;自动重新排列各item，设置如下：&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;setSortingEnabled(</span><span class="keyword">true</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li><span>3，拖曳：&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;setDragEnabled(</span><span class="keyword">true</span><span>);&nbsp;&nbsp;&nbsp;</span><span class="comment">//允许拖曳某item </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;setAcceptDrops(</span><span class="keyword">true</span><span>);&nbsp;&nbsp;&nbsp;</span><span class="comment">//允许item接受拖曳过来的item </span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;setDragDropMode(QAbstractItemView::InternalMove);&nbsp;&nbsp;&nbsp;</span><span class="comment">//移动模式，某item拖走后原来的位置不再保留该item </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>4，激活编辑状态的动作：&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;setEditTriggers(&nbsp;QAbstractItemView::SelectedClicked&nbsp;);&nbsp;</span><span class="comment">//&nbsp;QAbstractItemView::SelectedClicked&nbsp; </span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="comment">//表示当该item呈被选择状态时再被单击就可编辑该项，类似windows资源管理器中重命名文件 </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>像上图这样的效果，其实现很简单，代码如下：&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;QTreeWidget&nbsp;*legendView&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;QTreeWidget(legend);　</span><span class="comment">//创建一个QTreeWidget对象 </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;legend-&gt;setWidget(legendView);　</span><span class="comment">//将该对象设为以前创建的停靠窗口legend的部件 </span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;legendView-&gt;setSortingEnabled(</span><span class="keyword">false</span><span>);　</span><span class="comment">//不自动排序 </span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;legendView-&gt;setDragEnabled(</span><span class="keyword">false</span><span>);　</span><span class="comment">//不可拖放 </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;legendView-&gt;setAutoScroll(</span><span class="keyword">true</span><span>);&nbsp;&nbsp;　</span><span class="comment">//可以卷动 </span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;legendView-&gt;setColumnCount(1);　</span><span class="comment">//列数 </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;legendView-&gt;header()-&gt;setHidden(1);　</span><span class="comment">//隐藏最顶上的表头 </span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;legendView-&gt;setRootIsDecorated(</span><span class="keyword">true</span><span>);　</span><span class="comment">//让根项目前面也有树状虚线 </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;QTreeWidgetItem&nbsp;*&nbsp;item1&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;QTreeWidgetItem();&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;item1-&gt;setText(0,</span><span class="string">"item1"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;item1-&gt;setFlags(Qt::ItemIsEditable&nbsp;|&nbsp;Qt::ItemIsUserCheckable&nbsp;|&nbsp;Qt::ItemIsEnabled&nbsp;|&nbsp;Qt::ItemIsSelectable);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;item1-&gt;setCheckState&nbsp;(0,&nbsp;Qt::Checked);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QIcon&nbsp;myIcon(QgsApplication::themePath()+</span><span class="string">"/mActionFolder.png"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;item1-&gt;setIcon(0,&nbsp;myIcon);　</span><span class="comment">//还可以为item自定义图标 </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;legendView-&gt;addTopLevelItem(item1);　</span><span class="comment">//把item添加到QTreeWidget中 </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;QTreeWidgetItem&nbsp;*&nbsp;item2&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;QTreeWidgetItem(item1);　</span><span class="comment">//为item1添加子项目，这样item1前面会出现&#8220;+&#8221;， </span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;item2-&gt;setText(0,</span><span class="string">"item2"</span><span>);&nbsp;&nbsp;</span></li><li><span></span></li><li><span></span></span></li></ol><img src ="http://www.cppblog.com/LIULIANG/aggbug/174369.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-09 22:16 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/09/174369.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT QStringList 用法 </title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/09/174367.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Wed, 09 May 2012 14:16:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/09/174367.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/174367.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/09/174367.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/174367.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/174367.html</trackback:ping><description><![CDATA[<p>QStringList类提供了一个字符串列表<br />从QList &lt;QString&gt;继承而来，它提供快速索引为基础的接入以及快速插入和清除<br />其成员函数用于操作这个字符串列表如<br />append()，insert(), replace(), removeAll(), removeAt(), removeFirst(), removeLast(), and removeOne()等<br />1. 增加字符串<br />&nbsp;&nbsp; 可以通过append()，或使用&lt;&lt;，如<br />&nbsp;&nbsp; QStringList fonts;<br />&nbsp;&nbsp; fonts &lt;&lt; "Arial" &lt;&lt; "Helvetica" &lt;&lt; "Times" &lt;&lt; "Courier";<br />&nbsp;&nbsp; // fonts:[ "Arial" ,"Helvetica", "Times" , "Courier"]<br />2. 合并字符串使用join( )<br />&nbsp;&nbsp; QString str = fonts.join(",");<br />&nbsp;&nbsp; // str == "Arial,Helvetica,Times,Courier"<br />3. 拆分字符串<br />&nbsp;&nbsp; QString str = " Arial,Helvetica, ,Times,Courier ";<br />&nbsp;&nbsp; QStringList list1 = str.split(",");<br />&nbsp;&nbsp; // list1: [ " Arial ", " Helvetica ",&#8221; &#8220;, " Times ", " Courier " ]<br />&nbsp;&nbsp; QStringList list2 = str.split(",", QString::SkipEmptyParts);<br />&nbsp;&nbsp; // list2: [ " Arial ", " Helvetica ", " Times ", " Courier " ]<br />&nbsp;&nbsp; 也就是说如果有QString：：SkipEmptyParts，空项不会出现在结果。默认情况下，空项被保留<br />4. 索引<br />&nbsp;&nbsp; IndexOf（）函数返回给定字符串的第一个出现的索引。<br />&nbsp;&nbsp; 而lastIndexOf（）函数，返回字符串的最后一次出现的索引。<br />5. 替换replaceInStrings（）<br />&nbsp;&nbsp; QStringList files;<br />&nbsp;&nbsp; files &lt;&lt; "$QTDIR/src/moc/moc.y"<br />&nbsp;&nbsp; &lt;&lt; "$QTDIR/src/moc/moc.l"<br />&nbsp;&nbsp; &lt;&lt; "$QTDIR/include/qconfig.h";<br />&nbsp;&nbsp; files.replaceInStrings("$QTDIR", "/usr/lib/qt");<br />&nbsp;&nbsp; // files: [ "/usr/lib/qt/src/moc/moc.y", ...]<br />6. 过滤filter（）<br />&nbsp;&nbsp; 可以让你提取一个新的列表只包含这些字符串包含一个特定的字符串（或匹配特定正则表达式）：<br />&nbsp;&nbsp; QStringList list;<br />&nbsp;&nbsp; list &lt;&lt; "Bill Murray" &lt;&lt; "John Doe" &lt;&lt; "Bill Clinton";<br />&nbsp;&nbsp; QStringList result;<br />&nbsp;&nbsp; result = list.filter("Bill");<br />&nbsp;&nbsp; // result: ["Bill Murray", "Bill Clinton"]</p>
<p>7.遍历</p>
<p>&nbsp;&nbsp;QList&lt;QString&gt;::Iterator it = user.begin(),itend = user.end();<br />&nbsp;&nbsp;int i = 0;<br />&nbsp;&nbsp;for (;it != itend; it++,i++){<br />&nbsp;&nbsp;&nbsp;if (*it == pFindLine-&gt;text()){//找到，高亮显示<br />&nbsp;&nbsp;&nbsp;&nbsp;QModelIndex index = model-&gt;index(i);<br />&nbsp;&nbsp;&nbsp;&nbsp;m_pTabList-&gt;setCurrentIndex(index);<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;}</p><br /><br />转载：<a href="http://blog.csdn.net/zhangxiaonanwin/article/details/6088681">http://blog.csdn.net/zhangxiaonanwin/article/details/6088681</a><img src ="http://www.cppblog.com/LIULIANG/aggbug/174367.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-09 22:16 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/09/174367.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT arcTo函数简析 </title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/07/173903.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Mon, 07 May 2012 06:59:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/07/173903.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/173903.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/07/173903.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/173903.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/173903.html</trackback:ping><description><![CDATA[<p style="text-align: left; line-height: 180%; margin: 0cm 0cm 3pt;background: white" align="left"><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'"><a href="http://www.cnblogs.com/yanjieee/archive/2012/02/03/2336987.html"><span style="color: #223355; text-decoration: none; text-underline: none">QT arcTo</span><span style="font-family: 宋体; color: #223355; text-decoration: none; text-underline: none">函数简析</span></a> </span></strong></p>
<div style="border-bottom: #aaaaaa 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 0cm; padding-right: 0cm;background: white; margin-left: 18pt; border-top: medium none; margin-right: 22.5pt; border-right: medium none; padding-top: 0cm">
<p style="border-bottom: medium none; text-align: left; border-left: medium none; padding-bottom: 0cm; line-height: 180%; padding-left: 0cm; padding-right: 0cm;background: white; border-top: medium none; border-right: medium none; padding-top: 0cm" align="left"><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'">void QPainterPath::arcTo ( const&nbsp;<a href="http://www.cnblogs.com/yanjieee/admin/qrectf.html"><span style="color: #1d58d1; text-decoration: none; text-underline: none">QRectF</span></a>&nbsp;&amp;&nbsp;<em>rectangle</em>,&nbsp;<a href="http://www.cnblogs.com/yanjieee/admin/qtglobal.html#qreal-typedef"><span style="color: #1d58d1; text-decoration: none; text-underline: none">qreal</span></a>&nbsp;<em>startAngle</em>,&nbsp;<a href="http://www.cnblogs.com/yanjieee/admin/qtglobal.html#qreal-typedef"><span style="color: #1d58d1; text-decoration: none; text-underline: none">qreal</span></a>&nbsp;<em>sweepLength</em>&nbsp;)</span></strong></p>
<p style="border-bottom: medium none; text-align: left; border-left: medium none; padding-bottom: 0cm; line-height: 180%; padding-left: 0cm; padding-right: 0cm;background: white; border-top: medium none; border-right: medium none; padding-top: 0cm" align="left"><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'">void QPainterPath::arcTo (&nbsp;</span></strong><a href="http://www.cnblogs.com/yanjieee/admin/qtglobal.html#qreal-typedef"><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'; color: #1d58d1; text-decoration: none; text-underline: none">qreal</strong></a></span><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'">&nbsp;<em>x</em>,&nbsp;</span></strong><a href="http://www.cnblogs.com/yanjieee/admin/qtglobal.html#qreal-typedef"><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'; color: #1d58d1; text-decoration: none; text-underline: none">qreal</strong></a></span><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'">&nbsp;<em>y</em>,&nbsp;</span></strong><a href="http://www.cnblogs.com/yanjieee/admin/qtglobal.html#qreal-typedef"><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'; color: #1d58d1; text-decoration: none; text-underline: none">qreal</strong></a></span><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'">&nbsp;<em>width</em>,&nbsp;</span></strong><a href="http://www.cnblogs.com/yanjieee/admin/qtglobal.html#qreal-typedef"><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'; color: #1d58d1; text-decoration: none; text-underline: none">qreal</strong></a></span><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'">&nbsp;<em>height</em>,&nbsp;</span></strong><a href="http://www.cnblogs.com/yanjieee/admin/qtglobal.html#qreal-typedef"><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'; color: #1d58d1; text-decoration: none; text-underline: none">qreal</strong></a></span><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'">&nbsp;<em>startAngle</em>,&nbsp;</span></strong><a href="http://www.cnblogs.com/yanjieee/admin/qtglobal.html#qreal-typedef"><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'; color: #1d58d1; text-decoration: none; text-underline: none">qreal</strong></a></span><strong><span style="line-height: 180%; font-family: 'Arial','sans-serif'">&nbsp;<em>sweepLength</em>&nbsp;)</span></strong></p></div>
<p style="text-align: left; line-height: 180%; margin: 3.75pt 0cm;background: white" align="left">&nbsp;</p>
<p style="text-align: left; line-height: 180%; margin: 3.75pt 0cm;background: white" align="left"><span style="line-height: 180%; font-family: 宋体">根据给定的参数（一个矩形）画弧度，或者说画圆形。</span></p>
<p style="text-align: left; line-height: 180%; margin: 3.75pt 0cm;background: white" align="left"><span style="line-height: 180%; font-family: 宋体">下面给一副图就可以很容易的理解这个函数了。</p>
<p style="text-align: left; line-height: 180%; margin: 3.75pt 0cm;background: white" align="left"><span style="line-height: 180%; font-family: 'Arial','sans-serif'">rectangle </span><span style="line-height: 180%; font-family: 宋体">就是</span><span style="line-height: 180%; font-family: 'Arial','sans-serif'">x,y,width,height</span><span style="line-height: 180%; font-family: 宋体">组成的一个矩形，如图。</span><span style="line-height: 180%; font-family: 'Arial','sans-serif'"></span></p>
<p style="text-align: left; line-height: 180%; margin: 3.75pt 0cm;background: white" align="left"><span style="line-height: 180%; font-family: 'Arial','sans-serif'">startAngle</span><span style="line-height: 180%; font-family: 宋体">就是开始的角度，角度如图所示，你也可以是自己设定的角度，如</span><span style="line-height: 180%; font-family: 'Arial','sans-serif'">20/30</span><span style="line-height: 180%; font-family: 宋体">什么的。</span></p>
<p style="text-align: left; line-height: 180%; margin: 3.75pt 0cm;background: white" align="left"><span style="line-height: 180%; font-family: 'Arial','sans-serif'">sweepAngle</span><span style="line-height: 180%; font-family: 宋体">顾名思义就是扫取的角度，按图中所示，它是顺时针的哦。</span></p>
<p style="text-align: left; line-height: 180%; margin: 3.75pt 0cm;background: white" align="left">&nbsp;</p>
<p style="text-align: left; line-height: 180%; margin: 3.75pt 0cm 0pt;background: white" align="left"><span style="line-height: 180%; font-family: 宋体">也就是说如果你要画一个右半圆就可以这样：</span></p>
<p style="text-align: left; line-height: 180%; margin: 3.75pt 0cm;background: white" align="left">
<table style="border-bottom: medium none; border-left: medium none; border-collapse: collapse; border-top: medium none; border-right: medium none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="border-bottom: silver 1pt solid; border-left: silver 1pt solid; padding-bottom: 2.25pt; background-color: transparent; padding-left: 2.25pt; padding-right: 2.25pt; border-top: silver 1pt solid; border-right: silver 1pt solid; padding-top: 2.25pt">
<p style="text-align: left; margin: 0cm 0cm 0pt 16.5pt" align="left"><span style="font-family: 宋体">path.arcTo(0, 0, R*2, R*2, 0, 180);</span><span style="font-family: 宋体">　　//其中R是半径</span></p></td></tr></tbody></table><br /><br /><br /></span></p><img src ="http://www.cppblog.com/LIULIANG/aggbug/173903.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-07 14:59 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/07/173903.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT类型转换</title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/07/173902.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Mon, 07 May 2012 06:58:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/07/173902.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/173902.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/07/173902.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/173902.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/173902.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Qt数据类型与强制转换（整理）变量（Variable）和对象（Object）在python中的变量不需要声明，因为python是动态语言。python中所有的东西都是对象。&nbsp;数字（Number）和字符（String）在python中包括几种（整数、浮点数、长整数）数字类型和两种字符类型。&nbsp;整数（Integer）和长整数（Long Integer）...&nbsp;&nbsp;<a href='http://www.cppblog.com/LIULIANG/archive/2012/05/07/173902.html'>阅读全文</a><img src ="http://www.cppblog.com/LIULIANG/aggbug/173902.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-07 14:58 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/07/173902.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT安装时出现的问题</title><link>http://www.cppblog.com/LIULIANG/archive/2012/05/05/173740.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Sat, 05 May 2012 06:52:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/05/05/173740.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/173740.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/05/05/173740.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/173740.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/173740.html</trackback:ping><description><![CDATA[<div>在安装QT时，会提示没有安装C＋＋／c编译器，（就是下面的这句英文）<br />You need a C++ compiler. Please install it using the System Package Management tools.<br />所以我们要手动安装一个C＋＋/C编译器，否则就会出现出现不能新建桌面应用程序，所以在安装QT之前我们要<br />首先安装一个C＋＋/C编译器，这个很简单，使用下面的命令就可以安装<br />&nbsp;sudo apt-get install build-essential &nbsp;<br />安装了该软件包，编译c/c++所需要的软件包也都会被安装。因此如果想在Ubuntu中编译c/c++程序，只需要安装该软件包就可以了。<br />然后在安装QT就行了，<br /><div>1. 下载Qt_SDK_Lin32_offline_v1_1_4_en.run<br /><br />2、chmod u+x Qt_SDK_Lin32_offline_v1_1_4_en.run<br /><br />3、sudo ./Qt_SDK_Lin32_offline_v1_1_4_en.run<br /><br />注意别搞错权限了就打不开了<br />这些命令要到Qt_SDK_Lin32_offline_v1_1_4_en.run所在路径下执行<br />再安装好后。老是出现Unable to locate theme engine in module_path: "pixmap"毛病<br />解决Unable to locate theme engine in module_path: "pixmap"<br />Gtk-WARNING **: Unable to locate theme engine in module_path: "pixmap"<br />纳闷老是弹这东东出来，虽然。。。貌似不影响什么，可是看着不爽<br />装下那个engines就好了，就那么一行。。。<br />sudo apt-get install gtk2-engines-pixbuf <br />（在ubuntu 11.10下没发现有gtk3的。。不过装了这一样再没提示了）</div><br /></div><img src ="http://www.cppblog.com/LIULIANG/aggbug/173740.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-05-05 14:52 <a href="http://www.cppblog.com/LIULIANG/archive/2012/05/05/173740.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT QTableWidget 用法总结</title><link>http://www.cppblog.com/LIULIANG/archive/2012/04/16/171669.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Mon, 16 Apr 2012 15:34:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/04/16/171669.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/171669.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/04/16/171669.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/171669.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/171669.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: QTableWidget是QT程序中常用的显示数据表格的空间，很类似于VC、C#中的DataGrid。说到QTableWidget，就必须讲一下它跟QTabelView的区别了。QTableWidget是QTableView的子类，主要的区别是QTableView可以使用自定义的数据模型来显示内容(也就是先要通过setModel来绑定数据源)，而QTableWidget则只能使用标准的数据模型，并...&nbsp;&nbsp;<a href='http://www.cppblog.com/LIULIANG/archive/2012/04/16/171669.html'>阅读全文</a><img src ="http://www.cppblog.com/LIULIANG/aggbug/171669.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-04-16 23:34 <a href="http://www.cppblog.com/LIULIANG/archive/2012/04/16/171669.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt 窗体布局  </title><link>http://www.cppblog.com/LIULIANG/archive/2012/04/10/170834.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Tue, 10 Apr 2012 15:25:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/04/10/170834.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/170834.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/04/10/170834.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/170834.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/170834.html</trackback:ping><description><![CDATA[&nbsp;<span style="line-height: normal"><font style="line-height: normal" size="3">布局相关对象及简介</font></span> 
<p style="line-height: normal"><span style="line-height: normal; font-size: 10pt">窗体上的所有的控件必须有一个合适的尺寸和位置。</span><span style="line-height: normal">Qt</span><span style="line-height: normal; font-size: 10pt">提供了一些类负责排列窗体上的控件，主要有：</span><span style="line-height: normal">QHBoxLayout</span><span style="line-height: normal; font-size: 10pt">，</span><span style="line-height: normal">QVBoxLayout</span><span style="line-height: normal; font-size: 10pt">，</span><span style="line-height: normal">QGridLayout</span><span style="line-height: normal; font-size: 10pt">，</span><span style="line-height: normal">QStackLayout</span><span style="line-height: normal; font-size: 10pt">。（布局管理类）这些类简单易用，无论在代码中还是用</span><span style="line-height: normal">Qt Designer</span><span style="line-height: normal; font-size: 10pt">开发程序都能用到。</span></p><span style="line-height: normal">
<p style="line-height: normal"><span style="line-height: normal"><img style="line-height: normal" border="0" alt="Qt 窗体布局 - 黑黑的大鲨鱼 - 黑黑的大鲨鱼" src="http://img.blog.163.com/photo/HSVgayvtd0rrP4AIxVCUuA==/5758133598570509093.jpg" __1334071452801__="ev_8177833374" small="0" /></span></p><span style="line-height: normal">
<p style="text-align: left; line-height: normal; background-color: white; text-indent: -36pt; margin: 0cm 0cm 0pt 36pt; background-origin: initial; background-clip: initial" align="left"><strong style="line-height: normal"><span style="line-height: normal"><span style="line-height: normal">1）<span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">布局类简介</span></strong><strong style="line-height: normal"><span style="line-height: normal"></span></strong></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><strong style="line-height: normal"><span style="line-height: normal">QHBoxLayout</span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">：水平布局</span></strong><strong style="line-height: normal"><span style="line-height: normal"></span></strong></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><strong style="line-height: normal"><span style="line-height: normal">QVBoxLayout</span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">：垂直布局</span></strong><strong style="line-height: normal"><span style="line-height: normal"></span></strong></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><strong style="line-height: normal"><span style="line-height: normal">QGridLayout</span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">：</span></strong><strong style="line-height: normal"><span style="line-height: normal">&nbsp;</span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">表格布局</span></strong><strong style="line-height: normal"><span style="line-height: normal"></span></strong></p>
<p style="line-height: normal; text-indent: 25pt; margin: 0cm 0cm 0pt"><span style="line-height: normal">QGridLayout::addWidget()</span><span style="line-height: normal; font-size: 10pt">语法</span><span style="line-height: normal"></span></p>
<p style="line-height: normal; text-indent: 25pt; margin: 0cm 0cm 0pt"><span style="line-height: normal">layout-&gt;addWidget(widget, row, column, rowSpan, columnSpan);</span></p>
<p style="line-height: normal; text-indent: 25pt; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 10pt">参数</span><span style="line-height: normal">widget</span><span style="line-height: normal; font-size: 10pt">：为插入到这个布局的子控件；</span><span style="line-height: normal"></span></p>
<p style="line-height: normal; text-indent: 25pt; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 10pt">参数（</span><span style="line-height: normal">row</span><span style="line-height: normal; font-size: 10pt">，</span><span style="line-height: normal">column</span><span style="line-height: normal; font-size: 10pt">）为控件占据的左上角单元格位置；</span><span style="line-height: normal"></span></p>
<p style="line-height: normal; text-indent: 25pt; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 10pt">参数</span><span style="line-height: normal">rowSpan</span><span style="line-height: normal; font-size: 10pt">是控件占据的行数，</span><span style="line-height: normal"></span></p>
<p style="line-height: normal; text-indent: 25pt; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 10pt">参数</span><span style="line-height: normal">colunmSpan</span><span style="line-height: normal; font-size: 10pt">是控件占据的列的个数。</span><span style="line-height: normal"></span></p>
<p style="line-height: normal; text-indent: 25pt; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 10pt">（</span><span style="line-height: normal">rowSpan</span><span style="line-height: normal; font-size: 10pt">和</span><span style="line-height: normal">colunmSpan</span><span style="line-height: normal; font-size: 10pt">默认值为</span><span style="line-height: normal">1</span><span style="line-height: normal; font-size: 10pt">）</span><span style="line-height: normal"></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><strong style="line-height: normal"><span style="line-height: normal">Stacked Layouts</span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">：分组布局</span></strong><strong style="line-height: normal"><span style="line-height: normal"></span></strong></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>QStackedLayout</span><span style="line-height: normal; font-size: 10pt">类把子控件进行分组或者分页，一次只显示一组或者一页，隐藏其他组或者页上的控件。</span><span style="line-height: normal"></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal"></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 10pt">使用这些</span><span style="line-height: normal">Qt</span><span style="line-height: normal; font-size: 10pt">布局管理类的另一个原因是，在程序、系统改变字体，语言或者在不同的平台上运行时，布局管理器能够自动调整窗体里所有控件的大小和尺寸。</span><span style="line-height: normal"></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal"></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 10pt">其他可进行布局管理的类：这些类的共同特点是提供了更加灵活的布局管理，在一定程度上用户能够控制窗体内控件的大小。</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><strong style="line-height: normal"><span style="line-height: normal">QSplitter</span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">，</span></strong><strong style="line-height: normal"><span style="line-height: normal">QScrollArea</span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">，</span></strong><strong style="line-height: normal"><span style="line-height: normal">QMainWindow</span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">，</span></strong><strong style="line-height: normal"><span style="line-height: normal">QWorkspace</span></strong><span style="line-height: normal; font-size: 10pt">（对多文档的支持）</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt 36pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt 36pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt 36pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; text-indent: -36pt; margin: 0cm 0cm 0pt 36pt; background-origin: initial; background-clip: initial" align="left"><strong style="line-height: normal"><span style="line-height: normal"><span style="line-height: normal">2）<span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">布局管理中结合控件的</span></strong><strong style="line-height: normal"><span style="line-height: normal">sizePolicy</span></strong><strong style="line-height: normal"><span style="line-height: normal; font-size: 10pt">属性，进行调整</span></strong><strong style="line-height: normal"><span style="line-height: normal"></span></strong></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: normal; font-size: 10pt">结合控件的</span><span style="line-height: normal">SizePolicy</span><span style="line-height: normal; font-size: 10pt">属性，来控制布局管理中的控件的尺寸自适应方式。</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; text-indent: 10pt; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal; font-size: 10pt">控件的</span><span style="line-height: normal">sizePolicy</span><span style="line-height: normal; font-size: 10pt">说明控件在布局管理中的缩放方式。</span><span style="line-height: normal">Qt</span><span style="line-height: normal; font-size: 10pt">提供的控件都有一个合理的缺省</span><span style="line-height: normal">sizePolicy</span><span style="line-height: normal; font-size: 10pt">，但是这个缺省值有时不能适合所有的布局，开发人员经常需要改变窗体上的某些控件的</span><span style="line-height: normal">sizePolicy</span><span style="line-height: normal; font-size: 10pt">。一个</span><span style="line-height: normal">QSizePolicy</span><span style="line-height: normal; font-size: 10pt">的所有变量对水平方向和垂直方向都适用。下面列举了一些最长用的值：</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt -0.95pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal">A.</span><span style="line-height: normal; font-size: 7pt"><font style="line-height: normal" face="Calibri">&nbsp;</font></span><span style="line-height: normal">Fixed</span><span style="line-height: normal; font-size: 10pt">：控件不能放大或者缩小，控件的大小就是它的</span><span style="line-height: normal">sizeHint</span><span style="line-height: normal; font-size: 10pt">。</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; text-indent: -1pt; margin: 0cm 0cm 0pt 0.05pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal">B.</span><span style="line-height: normal; font-size: 7pt"><font style="line-height: normal" face="Calibri">&nbsp;</font></span><span style="line-height: normal">Minimum</span><span style="line-height: normal; font-size: 10pt">：控件的</span><span style="line-height: normal">sizeHint</span><span style="line-height: normal; font-size: 10pt">为控件的最小尺寸。控件不能小于这个</span><span style="line-height: normal">sizeHint</span><span style="line-height: normal; font-size: 10pt">，但是可以</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; text-indent: -1pt; margin: 0cm 0cm 0pt 0.05pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal; font-size: 10pt">放大。</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; text-indent: -1pt; margin: 0cm 0cm 0pt 0.05pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal">C.</span><span style="line-height: normal; font-size: 7pt"><font style="line-height: normal" face="Calibri">&nbsp;</font></span><span style="line-height: normal">Maximum</span><span style="line-height: normal; font-size: 10pt">：控件的</span><span style="line-height: normal">sizeHint</span><span style="line-height: normal; font-size: 10pt">为控件的最大尺寸，控件不能放大，但是可以缩小到它的最小</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; text-indent: 15pt; margin: 0cm 0cm 0pt -0.95pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal; font-size: 10pt">的允许尺寸。</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; text-indent: -1pt; margin: 0cm 0cm 0pt 0.05pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal">D.</span><span style="line-height: normal; font-size: 7pt"><font style="line-height: normal" face="Calibri">&nbsp;</font></span><span style="line-height: normal">Preferred</span><span style="line-height: normal; font-size: 10pt">：控件的</span><span style="line-height: normal">sizeHint</span><span style="line-height: normal; font-size: 10pt">是它的</span><span style="line-height: normal">sizeHint</span><span style="line-height: normal; font-size: 10pt">，但是可以放大或者缩小</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal">E.</span><span style="line-height: normal; font-size: 7pt"><font style="line-height: normal" face="Calibri">&nbsp;</font></span><span style="line-height: normal">Expandint</span><span style="line-height: normal; font-size: 10pt">：控件可以自行增大或者缩小</span><span style="line-height: normal"></span></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal"><span style="line-height: normal"></span></span><span style="line-height: normal; color: red; font-size: 10pt">注：</span><span style="line-height: normal">sizeHint</span><span style="line-height: normal; font-size: 10pt">（布局管理中的控件默认尺寸，如果控件不在布局管理中就为无效的值）</span></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal; font-size: 10pt"><br /></span></p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><span style="line-height: normal; font-size: 10pt"><span style="line-height: 20px; font-size: 18px">
<h3 style="line-height: normal; margin: 13pt 10.5pt 13pt 49.65pt"><span style="line-height: normal"><span style="line-height: normal"><font style="line-height: normal" size="3" face="Calibri">1.1.1.</font><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><font style="line-height: normal" size="3"><span style="line-height: normal">布局管理的三种方式</span></font></h3>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">Qt</span><span style="line-height: normal; font-size: 10pt">中有三种方式对窗体上的控件进行布局管理：绝对位置定位（</span><span style="line-height: normal">absolute positioning</span><span style="line-height: normal; font-size: 10pt">），手工布局（</span><span style="line-height: normal">manual layout</span><span style="line-height: normal; font-size: 10pt">），布局管理器（</span><span style="line-height: normal">layout managers</span><span style="line-height: normal; font-size: 10pt">）</span></p>
<h4 style="line-height: normal; margin: 14pt 0cm 14.5pt 63.85pt"><span style="line-height: normal"><span style="line-height: normal"><font style="line-height: normal" face="Cambria">1.1.1.1.</font><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="line-height: normal">绝对位置定位（控件布局是固定位置，没有自适应功能）</span></h4>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><strong style="line-height: normal"><span style="line-height: normal; font-size: 9pt">例子：</span></strong></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="line-height: normal"><font style="line-height: normal" size="2">QWidget *pWidget = new QWidget;</font></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="line-height: normal"><font style="line-height: normal" size="2">QLabel label(pWidget);</font></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><font style="line-height: normal" size="2"><span style="line-height: normal">label.setText(QObject::tr("</span><span style="line-height: normal; font-size: 8pt">姓名：</span><span style="line-height: normal">"));</span></font></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="line-height: normal"><font style="line-height: normal" size="2">label.setGeometry(10,10,20,20);</font></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><font style="line-height: normal" size="2"><span style="line-height: normal">QLineEdit namedLineEdit("</span><span style="line-height: normal; font-size: 8pt">小明</span><span style="line-height: normal">",pWidget);</span></font></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="line-height: normal"><font style="line-height: normal" size="2">namedLineEdit.setGeometry(35,10,50,20);</font></span></p>
<p style="text-align: left; line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt" align="left"></p>
<p style="text-align: left; line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt" align="left"><font style="line-height: normal" size="2"><span style="line-height: normal">QPushButton</span><span style="line-height: normal">&nbsp;*<span style="line-height: normal; color: rgb(1,0,1)">btn</span>&nbsp;=&nbsp;<span style="line-height: normal; color: blue">new</span>&nbsp;<span style="line-height: normal; color: rgb(1,0,1)">QPushButton</span>(<span style="line-height: normal; color: rgb(1,0,1)">QObject</span>::<span style="line-height: normal; color: rgb(1,0,1)">tr</span>(<span style="line-height: normal; color: rgb(163,21,21)">"</span></span><span style="line-height: normal">关闭<span style="line-height: normal">"</span></span><span style="line-height: normal">),<span style="line-height: normal; color: rgb(1,0,1)">pWidget</span>);</span></font></p>
<p style="line-height: normal"><span style="line-height: normal"><font style="line-height: normal" size="2"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; color: rgb(1,0,1)">btn</span>-&gt;<span style="line-height: normal; color: rgb(1,0,1)">setGeometry</span>(90,10,40,20);</font></span></p><span style="line-height: normal">
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><strong style="line-height: normal"><span style="line-height: normal; font-size: 9pt">图例：</span></strong></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><strong style="line-height: normal"><span style="line-height: normal"><span style="line-height: normal"><img style="line-height: normal" border="0" alt="Qt 窗体布局 - 黑黑的大鲨鱼 - 黑黑的大鲨鱼" src="http://img.blog.163.com/photo/x5XIDZ9Q25G9a6Rr9HrVqg==/602074975185581924.jpg" __1334071452801__="ev_4938414497" small="0" /></span></span></strong></p>
<h4 style="line-height: normal; margin: 14pt 0cm 14.5pt 63.85pt"><span style="line-height: normal"><span style="line-height: normal"><font style="line-height: normal" face="Cambria">1.1.1.1.</font><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="line-height: normal">手工布局</span></h4>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="line-height: normal; font-size: 10pt">给出控件的绝对位置，但是他们的尺寸根据窗口的大小确定，可以通过重写窗体控件的</span><span style="line-height: normal">resizeEvent()</span><span style="line-height: normal">实现对子控件的大小设置。</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"></p><span style="line-height: normal">
<h4 style="line-height: normal; margin: 14pt 0cm 14.5pt 63.85pt"><span style="line-height: normal"><span style="line-height: normal"><font style="line-height: normal" face="Cambria">1.1.1.1.</font><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="line-height: normal">布局管理器</span></h4>
<p style="line-height: normal; margin: 0cm 0cm 0pt"></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="line-height: normal; font-size: 10pt">例子</span><span style="line-height: normal">1</span><span style="line-height: normal; font-size: 10pt">：</span><span style="line-height: normal">运用</span><span style="line-height: normal">QHBoxLayout</span><span style="line-height: normal">、</span><span style="line-height: normal">QVBoxLayout</span><span style="line-height: normal">、</span><span style="line-height: normal">QGridLayout</span><span style="line-height: normal">布局</span></p><span style="line-height: normal">
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="line-height: normal"><img style="line-height: normal" border="0" alt="Qt 窗体布局 - 黑黑的大鲨鱼 - 黑黑的大鲨鱼" src="http://img.blog.163.com/photo/-pg8s3hVGSfAdKL2nr783g==/602074975185581925.jpg" __1334071452801__="ev_5243220600" small="0" /><br style="line-height: normal" /></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"></p><span style="line-height: normal"><span style="line-height: normal">
<p style="line-height: normal"></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt 36pt"></p>
<p style="line-height: normal; text-indent: 15.75pt; margin: 0cm 0cm 0pt"><span style="line-height: normal; color: red; font-size: 8pt">//leftLayout</span><span style="line-height: normal; color: red; font-size: 8pt">布局设置（表格布局）</span></p>
<p style="line-height: normal; text-indent: 15.75pt; margin: 0cm 0cm 0pt"><span style="line-height: normal; color: red; font-size: 8pt">/*</span><span style="line-height: normal; font-size: 8pt">&nbsp;QGridLayout:&nbsp;</span><span style="line-height: normal; font-size: 10pt">二维的单元格</span><span style="line-height: normal; color: red; font-size: 8pt">*/</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>QGridLayout *leftLayout = new QGridLayout;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>leftLayout-&gt;addWidget(&amp;namedLabel, 0, 0); //</span><span style="line-height: normal; font-size: 8pt">起始（<span style="line-height: normal">0</span>行，<span style="line-height: normal">0</span>列），尺寸（<span style="line-height: normal">1</span>行，<span style="line-height: normal">1</span>列）</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>leftLayout-&gt;addWidget(&amp;namedLineEdit, 0, 1);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>leftLayout-&gt;addWidget(&amp;lookInLabel, 1, 0);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>leftLayout-&gt;addWidget(&amp;lookInLineEdit, 1, 1);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>leftLayout-&gt;addWidget(&amp;subDirCheckBox, 2, 0, 1, 2);//</span><span style="line-height: normal; font-size: 8pt">起始（<span style="line-height: normal">3</span>行，<span style="line-height: normal">0</span>列），尺寸（<span style="line-height: normal">1</span>行，<span style="line-height: normal">2</span>列）</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>leftLayout-&gt;addWidget(&amp;tableWidget, 3, 0, 1, 2);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>leftLayout-&gt;addWidget(&amp;messageLabel, 4, 0, 1, 2);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; color: red">//rightLayout</span></span><span style="line-height: normal; color: red; font-size: 8pt">布局设置（垂直布局）</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>QVBoxLayout *rightLayout = new QVBoxLayout;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>rightLayout-&gt;addWidget(&amp;findButton);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>rightLayout-&gt;addWidget(&amp;stopButton);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>rightLayout-&gt;addWidget(&amp;closeButton);</span></p>
<p style="line-height: normal; text-indent: 8pt; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt">//</span><span style="line-height: normal; font-size: 8pt">布局管理器在指定的位置留出一块空间（</span><span style="line-height: normal">Qt Designer</span><span style="line-height: normal; font-size: 8pt">中，可以加入一个</span><span style="line-height: normal">spacer</span><span style="line-height: normal; font-size: 8pt">实现这一功能）</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>rightLayout-&gt;addStretch();</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>rightLayout-&gt;addWidget(&amp;helpButton);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; color: red">//mainLayout</span></span><span style="line-height: normal; color: red; font-size: 8pt">布局设置（水平布局）</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>QHBoxLayout *mainLayout = new QHBoxLayout;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>mainLayout-&gt;addLayout(leftLayout);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>mainLayout-&gt;addLayout(rightLayout);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; color: red">pWidget-&gt;setLayout(mainLayout)</span>;//</span><span style="line-height: normal; font-size: 8pt">设置<span style="line-height: normal">Widget</span>窗口控件的布局风格</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; color: red">pWidget-&gt;setWindowTitle(QObject::tr("</span></span><span style="line-height: normal">查找文件及文件夹<span style="line-height: normal">"));</span></span></p>
<p style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p style="text-align: left; line-height: normal; background-color: white; margin: 0cm 0cm 0pt; background-origin: initial; background-clip: initial" align="left"><strong style="line-height: normal"><span style="line-height: normal; font-size: 9pt">例子<span style="line-height: normal">1</span>图列：<span style="line-height: normal"><span style="line-height: normal"></span></span></span></strong></p>
<p style="line-height: normal"><span style="line-height: normal"><img style="line-height: normal" border="0" alt="Qt 窗体布局 - 黑黑的大鲨鱼 - 黑黑的大鲨鱼" src="http://img.blog.163.com/photo/XbRGCwUmwzNp9r0ZCNqBCg==/602074975185581926.jpg" __1334071452801__="ev_6074542693" small="0" /><br style="line-height: normal" /></span></p>
<p style="line-height: normal"><span style="line-height: normal"><br /></span></p>
<p style="line-height: normal"><span style="line-height: normal"><span style="line-height: 20px">
<h5 style="line-height: normal; margin: 14pt 0cm 14.5pt 78.05pt"><span style="line-height: normal"><span style="line-height: normal"><font style="line-height: normal" size="3" face="Calibri">1.1.1.1.1.</font><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><font style="line-height: normal" size="3"><span style="line-height: normal">例子</span><span style="line-height: normal"><font style="line-height: normal" face="Calibri">2</font></span><span style="line-height: normal">：运用</span><span style="line-height: normal"><font style="line-height: normal" face="Calibri">Stacked Layouts</font></span><span style="line-height: normal">：分组布局</span></font></h5>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><strong style="line-height: normal"><span style="line-height: normal; font-size: 8pt">例子：</span></strong></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">//</span><span style="line-height: normal; font-size: 8pt">创建</span><span style="line-height: normal">QWidget</span><span style="line-height: normal; font-size: 8pt">类的一个对象</span><span style="line-height: normal">pWidget</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">QWidget *pWidget = new QWidget;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">//</span><span style="line-height: normal; font-size: 8pt">创建一个布局管理器类</span><span style="line-height: normal">&nbsp;layout</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">QHBoxLayout *manLayout = new QHBoxLayout;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">//</span><span style="line-height: normal; font-size: 8pt">左边的列表控件</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">QListWidget *listWidget = new QListWidget(pWidget);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">listWidget-&gt;addItem(QObject::tr("</span><span style="line-height: normal; font-size: 8pt">外观设置</span><span style="line-height: normal">"));</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">listWidget-&gt;addItem(QObject::tr("</span><span style="line-height: normal; font-size: 8pt">网页浏览</span><span style="line-height: normal">"));</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal"><span style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>...<span style="line-height: normal">&nbsp;&nbsp;&nbsp;</span></span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"></p>
<p style="line-height: normal; text-indent: 4pt; margin: 0cm 0cm 0pt"><span style="line-height: normal">//</span><span style="line-height: normal; font-size: 8pt">右边的分页控件</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">QWidget *page1 = new QWidget(pWidget);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">&lt;&#8230;page1</span><span style="line-height: normal; font-size: 8pt">上增加控件</span><span style="line-height: normal">&gt;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">QWidget *page2 = new QWidget(pWidget);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">&lt;&#8230;page2</span><span style="line-height: normal; font-size: 8pt">上增加控件</span><span style="line-height: normal">&gt;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">&lt;</span><span style="line-height: normal; font-size: 8pt">设置布局管理</span><span style="line-height: normal">&gt;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">QVBoxLayout *leftLayout = new QVBoxLayout;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">leftLayout-&gt;addWidget(listWidget);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal; font-size: 8pt">．．．．</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">//</span><span style="line-height: normal; font-size: 8pt">右边分页布局（两页窗口）</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">QStackedLayout *stackedLayout = new QStackedLayout;</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">stackedLayout-&gt;addWidget(page1);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">stackedLayout-&gt;addWidget(page2);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">//</span><span style="line-height: normal; font-size: 8pt">通过调用</span><span style="line-height: normal">QWidget::setLayout()</span><span style="line-height: normal; font-size: 8pt">函数</span><span style="line-height: normal">,</span><span style="line-height: normal; font-size: 8pt">将</span><span style="line-height: normal">layout</span><span style="line-height: normal; font-size: 8pt">布局管理器类添加到窗口部件对象</span><span style="line-height: normal">pWidget</span><span style="line-height: normal; font-size: 8pt">中</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">manLayout-&gt;addLayout(leftLayout);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">manLayout-&gt;addLayout(stackedLayout);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">pWidget-&gt;setLayout(manLayout);</span></p>
<p style="text-align: left; line-height: normal; margin: 0cm 0cm 0pt" align="left"></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">listWidget-&gt;setCurrentRow(0);</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">//</span><span style="line-height: normal; font-size: 8pt">关联：列表与分组布局</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">QObject::connect(listWidget, SIGNAL(currentRowChanged(int)),</span></p>
<p style="line-height: normal; margin: 0cm 0cm 0pt"><span style="line-height: normal">stackedLayout, SLOT(setCurrentIndex(int)));</span></p>
<p style="line-height: normal"><span style="line-height: normal">pWidget-&gt;show();</span></p><span style="line-height: normal">
<p style="line-height: normal"><span style="line-height: normal"><strong style="line-height: normal">图例：</strong></span></p>
<p style="line-height: normal"><span style="line-height: normal"><strong style="line-height: normal"><span style="line-height: normal"><img style="line-height: normal" border="0" alt="Qt 窗体布局 - 黑黑的大鲨鱼 - 黑黑的大鲨鱼" src="http://img.blog.163.com/photo/6rdRweHISXfXRqSO4uVVwA==/5758133598570509144.jpg" __1334071452801__="ev_7603543297" small="0" /></span></strong></span></p>
<p style="line-height: normal"><span style="line-height: normal"><strong style="line-height: normal"><span style="line-height: normal"><span style="line-height: normal"><img style="line-height: normal" border="0" alt="Qt 窗体布局 - 黑黑的大鲨鱼 - 黑黑的大鲨鱼" src="http://img.blog.163.com/photo/L8LNFAzB4jFQldBRnl-yeQ==/5758133598570509145.jpg" __1334071452801__="ev_6704550959" small="0" /><br style="line-height: normal" /></span></span></strong></span></p></span></span></span>
<p>&nbsp;</p>
<div><span style="line-height: normal"><br /><br />转载：<a href="http://wangjiajun53880.blog.163.com/blog/static/11700139420110304514373/">http://wangjiajun53880.blog.163.com/blog/static/11700139420110304514373/</a><br /></span></div></span><span style="line-height: normal"></span></span></span></span></span></span></span>
<p>&nbsp;</p></span></span><img src ="http://www.cppblog.com/LIULIANG/aggbug/170834.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-04-10 23:25 <a href="http://www.cppblog.com/LIULIANG/archive/2012/04/10/170834.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>