﻿<?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++博客-暗金装备-随笔分类-Qt自定义Widget</title><link>http://www.cppblog.com/biao/category/10673.html</link><description> </description><language>zh-cn</language><lastBuildDate>Mon, 14 Sep 2009 00:13:08 GMT</lastBuildDate><pubDate>Mon, 14 Sep 2009 00:13:08 GMT</pubDate><ttl>60</ttl><item><title>Qt自定义Widget: 用QT创建新风格</title><link>http://www.cppblog.com/biao/archive/2009/09/14/96081.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Sun, 13 Sep 2009 23:00:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/09/14/96081.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/96081.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/09/14/96081.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/96081.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/96081.html</trackback:ping><description><![CDATA[<span  style="color: rgb(51, 51, 51); font-family: Tahoma, Arial, Helvetica, snas-serif; font-size: 14px; line-height: 25px; "><strong style="word-break: break-all; line-height: normal; ">1．Qt的风格</strong><br style="word-break: break-all; line-height: normal; ">　　a) Qt简介<br style="word-break: break-all; line-height: normal; ">　　Qt是一个跨平台的C++图形用户界面应用程序开发库，使用Qt可以开发出高质量的图形用户接口，它是完全面向对象的、易于扩展且允许真正的组件编程。Qt获得了很大的成功，特别是它的信号-槽机制是非常值得研究的通信机制，它也是Linux发行版标准组件KDE(K Desktop Enviroment)的基础。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　b) 风格机制<br style="word-break: break-all; line-height: normal; ">　　Qt的风格机制实现了不同平台上的图形用户接口（GUI）的观感（look and feel）,例如Windows平台上通常使用Windows或Windows-xp风格，而Unix平台上通常使用Motif、CDE风格。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　下图显示了Qt中与风格相关的类的继承关系<br style="word-break: break-all; line-height: normal; ">　　　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/200505261452007056.gif" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; "><br style="word-break: break-all; line-height: normal; ">　　QStyle是所有风格类的基类，它控制着所有的部件（widget即windows编程中的控件）的界面风格或观感，它定义了大量的枚举类型和十几个函数。枚举类型表示界面上的不同元素（如组合框中的按钮，按钮的边框等）；函数控制图形用户界面的绘制，但大多数函数基本上只是一些声明而没有函数实现，他们的实现在QCommonStyle、QWindowStyle、QMotifStyle及其子类中。QStyle只实现了3个函数drawItem()," itemRect(), visualRect()。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　drawItem(): 负责绘制文本和象素图。<br style="word-break: break-all; line-height: normal; ">　　itemRect(): 返回文本或图像所占的区域。<br style="word-break: break-all; line-height: normal; ">　　visualRect(): 返回逻辑坐标，这个函数使Qt实现right-to-left风格（阿文、维文传统是文本从右向左显示，因此控件布局也是从右向左）。如下图所示：<br style="word-break: break-all; line-height: normal; ">　　　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/200505261452005334.gif" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; "><br style="word-break: break-all; line-height: normal; ">　　可以看到菜单、工具条是右对齐、单选框的按钮在右边<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　c)" 创建新风格的步骤<br style="word-break: break-all; line-height: normal; ">　　在Qt中实现一种新风格的步骤很简单：只需选择一个风格类（如QCommonStyle或QStyle）作为父类，然后实现感兴趣的函数即可。难点在于函数的实现。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　选择父类：可以选择QStyle, QCommonStyle, QWindowStyle, QMotifStyle以及他们的子类的任意一个作为父类。通常可以选择QWindowsStyle或QMotifStyle，也可以选择QCommonStyle甚至是QStyle，但是工作量会比较大，因为很多界面的细节需要自己实现。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　重新实现必要的函数：想修改界面风格的哪部分，就重新实现与绘制那部分相关的函数，下面解释一下我们要重载的QStyle中的几个函数，这几个函数控制着图形用户界面上不同元素的布局和观感。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　 1）void drawPrimitive( PrimitiveElement pe,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　 QPainter *p,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　const QRect &amp; r,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　const QColorGroup &amp; cg,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　SFlags flags = Style_Default,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　const QStyleOption &amp;opt = QStyleOption::Default ) ;<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　<br style="word-break: break-all; line-height: normal; ">　　功能：绘制基本图形元素，如QSpinBox中的带箭头的按钮　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/200505261452005795.gif" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; ">等。<br style="word-break: break-all; line-height: normal; ">　　参数：" PrimitiveElement pe: 这个枚举型变量表示将要绘制的基本图形界面元素（这里说的基本图形用户界面元素指GUI中不可再分的一个原子元素，如组合框　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/200505261452002895.gif" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; ">中的这个绘有黑色三角形的按钮，spinBox中的按钮　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/200505261452003019.gif%22" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; "><br style="word-break: break-all; line-height: normal; ">　　QPainter *p：指向QPainter类的指针，Qt中的所有绘制操作不管是绘制文本、图形还是图像都由这个类来处理。<br style="word-break: break-all; line-height: normal; ">　　QRect &amp;r: 表示一个矩形区域，Qt在这个区域中绘制基本界面元素（PrimitiveElement）.<br style="word-break: break-all; line-height: normal; ">　　QColorGroup &amp;cg: QColorGroup表示一个部件(widget)的颜色组（color group），color group含有部件绘制自己时使用的各种颜色，譬如前景色背景色等。下图展示了color group中的各种颜色属性<br style="word-break: break-all; line-height: normal; ">　　　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/200505261452007748.gif" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; "><br style="word-break: break-all; line-height: normal; ">　　SFlags" flags: 控制如何绘制图形界面元素的标志。<br style="word-break: break-all; line-height: normal; ">　　QStyleOption &amp;opt: 绘制不同的部件(widget)时会需要不同的参数，如绘制面板（panel）可能需要线宽作为额外参数而绘制焦点矩形（focus rect）可能需要背景色作为额外参数，所以Qt专门提供了一个类QStyleOption来封装不同的widget可能需要的不同的参数，opt指向这样一个类的对象。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　2）void drawComplexControl( ComplexControl control,<br style="word-break: break-all; line-height: normal; ">　　QPainter *p,<br style="word-break: break-all; line-height: normal; ">　　const QWidget *widget,<br style="word-break: break-all; line-height: normal; ">　　const QRect &amp;r,<br style="word-break: break-all; line-height: normal; ">　　const QColorGroup &amp;cg,<br style="word-break: break-all; line-height: normal; ">　　SFlags flags = Style_Default,<br style="word-break: break-all; line-height: normal; ">　　SCFlags controls = QStyle::SC_All,<br style="word-break: break-all; line-height: normal; ">　　SCFlags active = QStyle::SC_None,<br style="word-break: break-all; line-height: normal; ">　　const QStyleOption&amp; opt = QStyleOption::Default)<br style="word-break: break-all; line-height: normal; ">　　　　　&nbsp;<br style="word-break: break-all; line-height: normal; ">　　功能：绘制复杂控制部件（widget）如SpinWidget，comboBox，slider，listView等<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　参数：<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　ComplexControl control：是一个枚举量，表示将要绘制的复杂控制部件（widget）如组合框、列表框等。<br style="word-break: break-all; line-height: normal; ">　　QPainter *p：指向QPainter的指针，Qt中的所有绘制操作不管是绘制文本、图形还是图像都由这个类来处理。<br style="word-break: break-all; line-height: normal; ">　　QWidget *widget：指向QWdget或其子类的指针，可以根据上面control的值转变（cast）成合适的类型，例如如果要绘制QSpinWidget，那么control取值为CC_SpinWidget,而widget指向一个QSpinWidget(QWidget的子类)的实例（instance）。使用这个变量可以访问QSpinWidget的成员函数和成员变量，譬如可以调用QSpinWidget的sizeHint函数获得这个部件的缺省大小（一个矩形空间）。<br style="word-break: break-all; line-height: normal; ">　　QRect &amp;r: 表示一个矩形区域，Qt在这个区域中绘制控件或其子部件。<br style="word-break: break-all; line-height: normal; ">　　QColorGroup &amp;cg: QColorGroup表示一个部件(widget)的颜色组（color group），color group含有部件绘制自己时使用的各种颜色，譬如前景色背景色等。<br style="word-break: break-all; line-height: normal; ">　　SFlags flags: 控制如何绘制图形界面元素的标志<br style="word-break: break-all; line-height: normal; ">　　SCFlags controls表示绘制复杂控制部件control的哪个子部件，缺省为SC_All,即绘制整个control而不是其某个子部件（注意control, controls是两个不同的参数）<br style="word-break: break-all; line-height: normal; ">　　QStyleOption &amp;opt: 在绘制不同的部件时可能需要不同的额外的参数，这个变量在绘制不同的widget时提供不同的信息。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　3) QRect querySubControlMetrics(ComplexControl control,<br style="word-break: break-all; line-height: normal; ">　　　　const QWidget* widget,<br style="word-break: break-all; line-height: normal; ">　　SubControl sc,<br style="word-break: break-all; line-height: normal; ">　　const QStyleOption&amp; = QStyleOption::Default)<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　功能：获取子部件的坐标和尺寸信息。这个函数控制着一个复杂控件的布局，重载这个函数可以使的组合框的下拉按钮绘制在左边 而不是默认的右边。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　参数：<br style="word-break: break-all; line-height: normal; ">　　ComplexControl control: 枚举量，表示将要绘制的复杂控制部件（widget）如组合框、列表框等。<br style="word-break: break-all; line-height: normal; ">　　QWidget *widget：指向QWidget或其子类的指针，可以根据上面control的值转变（cast）成合适的类型，例如如果要绘制QSpinWidget，那么control取值为CC_SpinWidget,而widget指向一个QSpinWidget(QWidget的子类)的实例。使用这个变量可以访问QSpinWidget的成员函数和成员变量，譬如可以调用QSpinWidget的sizeHint函数获得这个部件的缺省大小（一个矩形空间）。<br style="word-break: break-all; line-height: normal; ">　　SubControl sc：枚举量，一个复杂部件可能由多个的子部件组成，使用sc变量说明要获取那个子部件的坐标和尺寸信息。<br style="word-break: break-all; line-height: normal; ">　　QStyleOption &amp;opt: 计算不同部件的尺寸时可能需要不同的额外信息,QStyleOption封装了这些信息。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　<strong style="word-break: break-all; line-height: normal; ">2．创建新风格</strong><br style="word-break: break-all; line-height: normal; ">　　下面用一个例子来介绍一下创建新风格的整个过程，在编程之前，先看一下最终的结果是什么样的。(在Qt内部QSpinBox类是通过QSpinWidget实现的)<br style="word-break: break-all; line-height: normal; ">　　默认风格的效果：　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/20050526145200140.gif" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; ">使用新风格的效果：　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/200505261452007607.gif%22" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; "><br style="word-break: break-all; line-height: normal; ">　　可以看到在新风格中我们的SpinBox有了垂直显示的效果。下面我们按上面说明的步骤来创建一种新的风格。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　1）选择基类：我们选择QWindowsStyle类作为我们新风格类的基类，当然也可以选择QMotifStyle，在这个例子种也可以选择QCommonStyle。一般不建议选择QCommonStyle作为基类，因为QCommonStyle只实现了一部分界面部件，如果要实现一个完整的风格类，我们需要重新写很多代码。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　2）重载相关的函数：在这个例程中我们只修改了spinBox的风格，实现这个部件（widget）只与QStyle类的三个函数drawPrimitive, drawComplexControl, qureySubControlMerics相关，所以我们只需重载这三个函数的相关部分代码.下面对代码中的关键部分做一下注释，不重要的部分省略了。详细的代码可以从后面下载。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　绘制spinbox中按钮的函数：<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　void CustomStyle::drawPrimitive( PrimitiveElement pe,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　QPainter * p,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　　　const QRect &amp; r,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　　　const QColorGroup &amp; cg,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　　　SFlags flags,<br style="word-break: break-all; line-height: normal; ">　　　　　　　　　　　　　　　　const QStyleOption &amp; opt ) const<br style="word-break: break-all; line-height: normal; ">　　{<br style="word-break: break-all; line-height: normal; ">　　/*PE_SpinWidgetUp,PE_SpinWidgetDown表示spinBox中的下按钮和上按钮，下面的代码使得这两个按钮中的三角形分别向左和向右*/<br style="word-break: break-all; line-height: normal; ">　　if ((pe == PE_SpinWidgetUp) || (pe == PE_SpinWidgetDown)){<br style="word-break: break-all; line-height: normal; ">　　int fw = pixelMetric( PM_DefaultFrameWidth, 0 );//fw表示边框宽度，默认为2<br style="word-break: break-all; line-height: normal; ">　　QRect br;　//spinBox上按钮的边界矩形不是spinBox的边界矩形。<br style="word-break: break-all; line-height: normal; ">　　br.setRect( r.x() + fw, r.y() + fw, r.width() - fw*2,<br style="word-break: break-all; line-height: normal; ">　　　　r.height() - fw*2 );<br style="word-break: break-all; line-height: normal; ">　　p-&gt;fillRect( br, cg.brush( QColorGroup::Button ) );<br style="word-break: break-all; line-height: normal; ">　　int x = r.x(), y = r.y(), w = r.width(), h = r.height();<br style="word-break: break-all; line-height: normal; ">　　int sw = w-4;<br style="word-break: break-all; line-height: normal; ">　　int sh = sw/2 + 2;　　　// Must have empty row at foot of arrow<br style="word-break: break-all; line-height: normal; ">　　int sx = x + w / 2 - sw / 2 - 1;<br style="word-break: break-all; line-height: normal; ">　　int sy = y + h / 2 - sh / 2 - 1;<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　QPointArray a;<br style="word-break: break-all; line-height: normal; ">　　/* 设置三角形的三个点的坐标，修改这三个点可以使得QSpinBox上按钮里的三角型呈现任意的形状，下面的设置使得三角形表示的箭头分别向左和向右。*/<br style="word-break: break-all; line-height: normal; ">　　if ( pe == PE_SpinWidgetDown )<br style="word-break: break-all; line-height: normal; ">　　　　a.setPoints( 3,　0, sh/2,　sw-1, 1,　sw-1, sh-1 );<br style="word-break: break-all; line-height: normal; ">　　　else<br style="word-break: break-all; line-height: normal; ">　　　　a.setPoints( 3,　0, 1,　0, sh-1,　sw-1, sh/2 );<br style="word-break: break-all; line-height: normal; ">　　...........<br style="word-break: break-all; line-height: normal; ">　　p-&gt;drawPolygon( a );　　//绘制三角形<br style="word-break: break-all; line-height: normal; ">　　}else if((pe == PE_ButtonBevel) || (pe == PE_ButtonCommand) || (pe == PE_ButtonTool) || (pe == PE_ButtonDropDown) || (pe == PE_HeaderSection))<br style="word-break: break-all; line-height: normal; ">　　　　{ //绘制按钮的各种效果使得看起来凸起或凹下。<br style="word-break: break-all; line-height: normal; ">　　　　qDrawShadePanel(p, r, cg, flags &amp; (Style_Sunken | Style_Down | Style_On),&nbsp;<br style="word-break: break-all; line-height: normal; ">　　1, &amp;cg.brush(QColorGroup::Button));<br style="word-break: break-all; line-height: normal; ">　　　　}else{&nbsp;<br style="word-break: break-all; line-height: normal; ">　　/*对于其他基本图形元素(PrimitiveElement)的绘制我们不作处理只是简单的调用父类的函数。*/<br style="word-break: break-all; line-height: normal; ">　　QWindowsStyle::drawPrimitive( pe, p, r, cg, flags, opt);<br style="word-break: break-all; line-height: normal; ">　　　　}<br style="word-break: break-all; line-height: normal; ">　　}<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　绘制整个spinBox的函数：<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　void CustomStyle::drawComplexControl( ComplexControl control,<br style="word-break: break-all; line-height: normal; ">　　　　　 QPainter *p,<br style="word-break: break-all; line-height: normal; ">　　　　　 const QWidget *widget,<br style="word-break: break-all; line-height: normal; ">　　　　　 const QRect &amp;r,<br style="word-break: break-all; line-height: normal; ">　　　　　 const QColorGroup &amp;cg,<br style="word-break: break-all; line-height: normal; ">　　　　　 SFlags flags,<br style="word-break: break-all; line-height: normal; ">　　　　　 SCFlags controls,<br style="word-break: break-all; line-height: normal; ">　　　　　 SCFlags active,<br style="word-break: break-all; line-height: normal; ">　　　　　 const QStyleOption&amp; opt ) const<br style="word-break: break-all; line-height: normal; ">　　{　　<br style="word-break: break-all; line-height: normal; ">　　//下面的代码使得spinWidget呈现垂直显示的风格而不是通常的水平显示<br style="word-break: break-all; line-height: normal; ">　　if (control == CC_SpinWidget) {<br style="word-break: break-all; line-height: normal; ">　　const QSpinWidget * sw = (const QSpinWidget *) widget;<br style="word-break: break-all; line-height: normal; ">　　//绘制向上按钮部分，controls默认为SC_All，即绘制整个spinwidget<br style="word-break: break-all; line-height: normal; ">　　if ( controls &amp; SC_SpinWidgetUp ) {<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　　　if ( sw-&gt;buttonSymbols() == QSpinWidget::PlusMinus )<br style="word-break: break-all; line-height: normal; ">　　pe = PE_SpinWidgetPlus;　// 使用加减号　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/200505261452008145.gif" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; "><br style="word-break: break-all; line-height: normal; ">　　　　else<br style="word-break: break-all; line-height: normal; ">　　pe" = PE_SpinWidgetUp;　 // 使用三角形　<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/200505261452007091.gif" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; "><br style="word-break: break-all; line-height: normal; ">　　　　QRect" re = sw-&gt;upRect();<br style="word-break: break-all; line-height: normal; ">　　　　QColorGroup ucg = sw-&gt;isUpEnabled() ? cg : sw-&gt;palette().disabled();<br style="word-break: break-all; line-height: normal; ">　　　　drawPrimitive(PE_ButtonBevel, p, re, ucg, flags); //绘制按钮的边框<br style="word-break: break-all; line-height: normal; ">　　drawPrimitive(pe, p, re, ucg, flags); //绘制按钮<br style="word-break: break-all; line-height: normal; ">　　}<br style="word-break: break-all; line-height: normal; ">　　//绘制向左按钮部分。<br style="word-break: break-all; line-height: normal; ">　　if ( controls &amp; SC_SpinWidgetDown ) {<br style="word-break: break-all; line-height: normal; ">　　　/*与绘制向下按钮相似*/<br style="word-break: break-all; line-height: normal; ">　　}<br style="word-break: break-all; line-height: normal; ">　　}else{//不处理spinbox之外的其他复杂控制部件，调用父类函数处理<br style="word-break: break-all; line-height: normal; ">　　QWindowsStyle::drawComplexControl(control, p, widget, r, cg, flags, controls, active, opt);<br style="word-break: break-all; line-height: normal; ">　　　　}<br style="word-break: break-all; line-height: normal; ">　　}<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　获取部件（widget）中各个子部件布局信息的函数，这个函数控制着一个widget的外观<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　QRect CustomStyle::querySubControlMetrics( ComplexControl control,<br style="word-break: break-all; line-height: normal; ">　　　　const QWidget *widget,<br style="word-break: break-all; line-height: normal; ">　　　　SubControl sc,<br style="word-break: break-all; line-height: normal; ">　　　　const QStyleOption &amp;opt ) const<br style="word-break: break-all; line-height: normal; ">　　{<br style="word-break: break-all; line-height: normal; ">　　　　if(control == CC_SpinWidget){<br style="word-break: break-all; line-height: normal; ">　　　　int fw = pixelMetric( PM_SpinBoxFrameWidth, widget);<br style="word-break: break-all; line-height: normal; ">　　/*QSize 定义二维对象的大小,也就是宽和高. 坐标类型是QCOORD定义为int)*/<br style="word-break: break-all; line-height: normal; ">　　QSize bs;　//此处bs表示每个按钮的大小,因为有两个按钮所以下面除以2.<br style="word-break: break-all; line-height: normal; ">　　bs.setWidth(widget-&gt;width()/2 -fw);<br style="word-break: break-all; line-height: normal; ">　　if(bs.width() &lt; 8) bs.setWidth(8);<br style="word-break: break-all; line-height: normal; ">　　/*按钮高度设置为QMIN{按钮宽度的1.6倍, 部件高度的四分之一}<br style="word-break: break-all; line-height: normal; ">　　bs.setHeight(　QMIN(bs.width()*8/5, widget-&gt;height() / 4) );&nbsp;<br style="word-break: break-all; line-height: normal; ">　　bs = bs.expandedTo( QApplication::globalStrut() );<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　int x = fw;<br style="word-break: break-all; line-height: normal; ">　　int y, ly, ry;<br style="word-break: break-all; line-height: normal; ">　　y = widget-&gt;height() - x - bs.height();<br style="word-break: break-all; line-height: normal; ">　　ly = fw;<br style="word-break: break-all; line-height: normal; ">　　ry = y - fw;<br style="word-break: break-all; line-height: normal; ">　　//下面定义了QSpinWidget的各个子部件的坐标位置.<br style="word-break: break-all; line-height: normal; ">　　switch ( sc ) {<br style="word-break: break-all; line-height: normal; ">　　case SC_SpinWidgetUp:<br style="word-break: break-all; line-height: normal; ">　　//返回向右按钮的坐标信息<br style="word-break: break-all; line-height: normal; ">　　　　return QRect(x + bs.width(), y, bs.width(), bs.height());<br style="word-break: break-all; line-height: normal; ">　　case SC_SpinWidgetDown:<br style="word-break: break-all; line-height: normal; ">　　//返回向左按钮的坐标信息<br style="word-break: break-all; line-height: normal; ">　　　　return QRect(x, y, bs.width(), bs.height());<br style="word-break: break-all; line-height: normal; ">　　case SC_SpinWidgetButtonField:<br style="word-break: break-all; line-height: normal; ">　　//返回两个按钮的总区域大小<br style="word-break: break-all; line-height: normal; ">　　　　return QRect(x, y, widget-&gt;width() - 2*fw, bs.height());<br style="word-break: break-all; line-height: normal; ">　　case SC_SpinWidgetEditField:<br style="word-break: break-all; line-height: normal; ">　　/*返回可编辑框的坐标信息*/<br style="word-break: break-all; line-height: normal; ">　　　　return QRect(fw, ly, widget-&gt;width() - 2*fw, ry);<br style="word-break: break-all; line-height: normal; ">　　case SC_SpinWidgetFrame:<br style="word-break: break-all; line-height: normal; ">　　//返回整个spinBox的坐标信息<br style="word-break: break-all; line-height: normal; ">　　　　return widget-&gt;rect();<br style="word-break: break-all; line-height: normal; ">　　default:<br style="word-break: break-all; line-height: normal; ">　　　　break;<br style="word-break: break-all; line-height: normal; ">　　}<br style="word-break: break-all; line-height: normal; ">　　　　}else{//其它部件的布局信息调用父类的函数来处理。<br style="word-break: break-all; line-height: normal; ">　　return QWindowsStyle::querySubControlMetrics(control,widget,sc,opt );<br style="word-break: break-all; line-height: normal; ">　　　　}<br style="word-break: break-all; line-height: normal; ">　　　　return QRect();<br style="word-break: break-all; line-height: normal; ">　　}<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　<strong style="word-break: break-all; line-height: normal; ">3．使用新风格</strong><br style="word-break: break-all; line-height: normal; ">　　有两种方法使用新风格，一种是作为插件，一种是在应用程序里直接使用。作为插件的风格可以在不用修改代码、不用重新编译的情况下使用新风格。由于本文着重介绍如何创建风格所以我们使用第一种方法。这种方法很简单，只需在应用程序中包含相应风格类的头文件，然后把main（）函数第一句可执行代码设置为QApplication::setStyle(new MyStyle())即可。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　下面我们用一个小例子来看看效果。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　#include&nbsp;<qapplication.h style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; ">　　#include&nbsp;<qspinbox.h style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; ">　　#include "customstyle.h"<br style="word-break: break-all; line-height: normal; ">　　int main( int argc, char **argv )<br style="word-break: break-all; line-height: normal; ">　　{<br style="word-break: break-all; line-height: normal; ">　　　　QApplication::setStyle(new CustomStyle()); //使用新风格类来绘制界面。<br style="word-break: break-all; line-height: normal; ">　　　　QApplication a( argc, argv );<br style="word-break: break-all; line-height: normal; ">　　　　QSpinBox spin( 0, 15 );<br style="word-break: break-all; line-height: normal; ">　　　　spin.resize( 20, 100 );<br style="word-break: break-all; line-height: normal; ">　　　　a.setMainWidget( &amp;spin );<br style="word-break: break-all; line-height: normal; ">　　　　spin.show();<br style="word-break: break-all; line-height: normal; ">　　　　return a.exec();<br style="word-break: break-all; line-height: normal; ">　　}<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　然后编译运行即可看到效果。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　Ps. qt中编译使用qmake，步骤为<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　创建源程序&nbsp;<br style="word-break: break-all; line-height: normal; ">　　同一目录下运行qmake -project&nbsp;<br style="word-break: break-all; line-height: normal; ">　　qmake&nbsp;<br style="word-break: break-all; line-height: normal; ">　　make&nbsp;<br style="word-break: break-all; line-height: normal; ">　　运行可执行程序。&nbsp;<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　<strong style="word-break: break-all; line-height: normal; ">4．进一步工作</strong><br style="word-break: break-all; line-height: normal; ">　　1）默认大小：细心的朋友可能看到上面的代码中有一句：spin.resize( 20, 100 )，这一句设置spinbox的长度为20象素，宽度为100个象素。如果没有这一句的话，显示的结果会一团糟，两个按钮几乎看不到<img src="http://www.chinaitpower.com/A-A-A/2005/05/26/20050526145200453.gif" onclick="javascript:window.open(this.src);" onload="return imgzoom(this,550)" title="点击图片可在新窗口打开" style="word-break: break-all; line-height: normal; max-width: 500px; cursor: pointer; ">，因为qt默认的显示是水平显示而根本没有考虑垂直显示的情况。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　如果想让spinbox在默认情况下看起来长度窄而宽度高需要修改QSpinBox类中的sizeHint函数，这个函数功能是设置部件(widget)的默认尺寸。在qt中几乎每个GUI部件类都有sizeHint这个函数来设置它自己的默认的长和宽。<br style="word-break: break-all; line-height: normal; ">　　<br style="word-break: break-all; line-height: normal; ">　　文本垂直显示：在此例中虽然控件spinbox达到了垂直显示的效果，但是文本仍旧是水平显示的，因此要达到真正的垂直显示需要了解qt的文本显示机制。这些工作是很有意义的，因为有些民族（如蒙文）的语言传统就是垂直显示的，而现在没有一个真正满足这种需求的系统。笔者现在正在看qt-x11-free-3.2.2的源码，目前对文本显示机制只有初步了解，还没有真正弄清，非常希望和感兴趣的朋友相互交流、学习。"</qspinbox.h></qapplication.h></span>

<div><font  color="#333333" face="Tahoma, Arial, Helvetica, snas-serif" size="4"><span  style="font-size: 14px;"><br></span></font></div><div><font  color="#333333" face="Tahoma, Arial, Helvetica, snas-serif" size="4"><span  style="font-size: 14px;">转自:&nbsp;http://www.lupaworld.com/?action-viewstutorial-itemid-7178</span></font></div><img src ="http://www.cppblog.com/biao/aggbug/96081.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-09-14 07:00 <a href="http://www.cppblog.com/biao/archive/2009/09/14/96081.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: 利用Qt4.5新特性实现酷炫透明窗体</title><link>http://www.cppblog.com/biao/archive/2009/06/12/87508.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Fri, 12 Jun 2009 09:27:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/06/12/87508.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/87508.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/06/12/87508.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/87508.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/87508.html</trackback:ping><description><![CDATA[<span  style="font-family: Arial, Verdana, sans-serif; font-size: 14px; "><div class="storycontent"><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">Qt4.2引入了<span style="color: rgb(0, 0, 128); ">QWidget::setWindowOpacity</span>函数， 可以为窗体设置透明度， 从0.0到1.0之间， 值越小越透明。 经过设置的窗体可以整体呈现透明的效果。 但这种设置比较粗糙， 只能设一个整体的效果， 大概只有比如像拖动的时候能用一下， 大多数时候都不太实用。 在Qt4.5里引入了新的窗体透明特性， 是个Widget的Attribute， 叫做<span style="color: rgb(0, 0, 128); ">Qt::WA_TranslucentBackground</span>。 这个属性可以为每个QWidget单独设置， 并且透明程度可以用绘制的颜色或图片的Alpha Channel值来控制。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">笔者写了一个例子演示其奇妙的效果。 先看一个截图：<br><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/06/translucent.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-341" title="translucent" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/06/translucent.jpg" alt="translucent" width="800" height="300" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">这个是笔者例子运行出来的效果， 背景是www.cuteqt.com雷人的主页。&nbsp; 下面简单介绍一下代码的实现。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><strong>TranslucentBackground控制窗体透明属性</strong><br>例子主界面用QWidget， 其上放置四个控件， 上面两个是自定义的QWidget子类， 用在paintEvent中绘制了一幅透明底色的图片， 上书&#8220;<span style="color: rgb(0, 128, 0); ">CuteQt</span>&#8221;几个大字； 下面两个是标准的QLabel控件， 但显示出两种不同的效果。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">透明的控件的TranslucentBackground属性为true （继承了parent的属性）， 而非透明的控件则在代码中强制将TranslucentBackground设为了false， 这样就造就了有意思的结果。 代码片段如下：<br>label = new QLabel(&#8221;www.cuteqt.com&#8221;);<br>label-&gt;setAttribute(Qt::WA_TranslucentBackground, false);<br>label-&gt;setAutoFillBackground(true);</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><strong>Alpha Channel控制透明度</strong><br>将这个例子稍稍改动， 修改一下窗体背景色的Alpha值， 使之展现不同的透明度。 实现的方法是设置窗体的palette属性， 为Background这个ColorRole的颜色设置了alpha值， 代码片段如下：<br>QPalette pal = palette();<br>pal.setColor(QPalette::Background, QColor(255,0,0,<span style="color: rgb(0, 0, 255); ">200</span>));<br>setPalette(pal);</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">下图所示为alpha值100和200的不同显示效果。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/06/translucent1.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-342" title="translucent1" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/06/translucent1.jpg" alt="translucent1" width="400" height="260" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/06/translucent2.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-343" title="translucent2" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/06/translucent2.jpg" alt="translucent2" width="400" height="260" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">怎么样， 这个例子挺有意思吧？ 赶快下载完整的代码学习一下吧～ 有任何不明白blog或bbs留言～</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/06/translucenttar.gz" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; ">translucent.tar.gz</a></p></div><div class="copyright" style="color: rgb(0, 0, 128); font-size: 12px; ">本站所有文章由本站和原作者保留一切权力，仅在保留本版权信息、原文链接、原文作者的情况下允许转载，转载请勿删改原文内容， 并不得用于商业用途。 谢谢合作。</div><div class="index" style="font-size: 12px; ">原文链接:<a href="http://www.cuteqt.com/blog/?p=340" style="color: rgb(0, 0, 0); text-decoration: none; font-weight: bold; ">http://www.cuteqt.com/blog/?p=340</a></div></span>
<img src ="http://www.cppblog.com/biao/aggbug/87508.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-06-12 17:27 <a href="http://www.cppblog.com/biao/archive/2009/06/12/87508.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: 自定义Model</title><link>http://www.cppblog.com/biao/archive/2009/06/01/86346.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Mon, 01 Jun 2009 01:16:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/06/01/86346.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/86346.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/06/01/86346.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/86346.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/86346.html</trackback:ping><description><![CDATA[<span  style="font-family: Arial; font-size: 14px; "><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">一直觉得Qt里的Model-View概念极其神秘， 因为看过很多一知半解的source code， 却总是咋看咋不懂，急了满头大汗之余不禁感叹 — 老了，脑子不够用了！</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">这两天因为在写rssreader的关系，用到了MVC， 总算有点压力学习学习ModelView的奥秘，而且也小有收获。 谨以此文献给MVC未入门的学弟学妹， 共勉！</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">先来讲一些必备的背景知识。 在讲MVC时有三个重要且基本的概念贯穿整个学习过程：Index， Data和Role。 就从Index开始。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">我们见过的View有单列的List结构， 有树状的层次结构，还有两维的表格结构， 归根结底，其实这些都是层次结构的变体。 比如下面的图：</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/modelview-models.png" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-219" title="modelview-models" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/modelview-models.png" alt="modelview-models" width="618" height="332" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">从这张图可以清楚的理解上文的观点。 在这几种结构中，都有一个隐含的根节点及与根节点联系的层次结构。 任何一种结构中都存在这样一个定式， 通过一个父节点及一组横纵座标（row,column)即可唯一的确定一个子节点， 这个规律在后面会经常用到。Index可以简单的理解成节点的指针， 前面说过通过三个要素即可唯一的确定一个节点， 所以Model提供的获得节点index函数亦即接受row，column和parentindex三个参数， 我们在写model时首先需要实现这样一个函数；</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">第二个概念Data就更简单了，View要显示数据， 就要从Model中去获取需要显示的数据， 传什么参数呢？ 不用动脑子也想的到咯，Index肯定算一个。 但仅仅Index并不够， 因为View要显示的可能不止一项数据，比如我的数据包含文本， 包含图标，包含链接甚至一些二进制数据， 我怎么知道View想要的是哪个呢？ 这里就用到另外一个概念了 — Role， Role就用来表示View向Model索取哪个类型的数据。 View告诉Model：&#8220;我想要A节点下的N行M列数据的显示文本； 我想要A节点下的N行M列数据的图标&#8230;&#8221;， 这样Model就清楚的知道应该返回什么数据了。 data()函数在这里就充当了返回数据的责任，需要我们在实现Model的时候重点实现这个函数。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">目前定义好的Role可以参考下面的图（图中只标出了一部分Role， 其他的参见文档DisplayRole相关章节）：</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/modelview-roles.png" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-220" title="modelview-roles" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/modelview-roles.png" alt="modelview-roles" width="379" height="265" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">&nbsp;</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">作为Model必须决定为View提供多少数据，提供哪些类型的数据， 可以去满足View的请求，也可以忽略它， 有很大的自主权。 最简单的实现是不管什么Role都给它返回个字符串就好了。呵呵。 当然作为Model也不能太独断专行，因为毕竟要和View一起工作， 一定要与View的需求相配合才行。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">好， 有了这些知识做基础， 写个Model出来其实是非常简单的， 稍微用点心就能应付了， 首先要选对参考文档， 如果是以写代码为目的， 推荐这一篇：</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><a title="Creating New Models" href="http://doc.trolltech.com/4.5/model-view-creating-models.html" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; ">Creating New Models</a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">要写code的话这篇最实用， 前面的N多篇都在讲一些概念性的内容， 大把大把的蚂蚁样的英文看了就头大， 还是直接看这篇比较有效。 简单来说分成几步来做：</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">第一、分析需求，确定基类</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">先要确定你的数据是列表结构还是层次结构， 需要显示什么样的数据， 需不需要支持增删或编辑功能等。 根据需求来确定从哪个Model的基类派生，如一个显示字符串列表的Model可以采用QAbstractListModel， 树状层次就只能从QAbstractItemModel开始了。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">第二、分析需求，确定需要实现哪些函数</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">根据需求的不同，需要实现的函数也不尽相同。&nbsp;</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">最简单的只读的列表结构只需要实现两个基本的函数：rowCount(), data()， 也就是只需要知道一共有多少行，每行都显示什么样的数据即可， 十分明了吧？ 多列的情况下要实现columnCount()， 需要显示header的要去实现headerData()， 这些规则都太容易理解了。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">其次，如果是层次列表，则需要确定节点之间的层次关系，就需要实现index()和parent()两个函数， 一个是通过父指针和row,column座标确定一个子节点，一个是通过子节点知道它的父指针。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">再次，如果需要修改数据， 先要通知View我的Model数据是可以被编辑的， 就是要实现flags()这个函数， 此函数返回数据的属性，如可编辑、可被选中等； 编辑之后需要一个函数将编辑完成的数据传递给Model， 所以还要实现一个setData方法。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">再再次， 需要增删数据的Model还要告诉Model的底层：&#8220;我要增删数据了！&#8221;， &#8220;我要增删的数据是。。。&#8221;， 还有&#8220;我增删的操作已经做完了！&#8221;， 这些分别对应：调用beginInsertRows()和endInsertRows()。 根据笔者的经验，这部分不太好理解，而且容易出错。 文档里写的是加数据的时候调用insertRows()，不过没有提到说其实在QAbstractItemModel类里这个函数只是个空架子，根本就没有实现， 所以你如果按照文档去调用这个函数通知Model数据加进来了，只能得到一个return false， 不会有任何实际的作用， 很让人困惑。 正确的做法是在你增删数据的前后加上beginInsertRows和endInsertRows的调用，这样底层就能正确处理数据的变化， 并且将变化及时的反应到View中。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">&nbsp;</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">上面提到的函数在Creating New Models这篇文章中都有具体的例子代码可供参考，相信照着例子做一定难不倒大家。 btw，实现函数的时候要注意， 函数的声明必须和文档中所描述的一模一样才能被调到， 这也是初学者经常不注意的地方。 在Qt文档中， 下一篇就该学习如何使用View和创建自己的View了，这部分还有待研究。 等研究好了再撰文汇报！</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><font  size="3"><span  style="font-size: 12px; line-height: normal;"><br></span></font></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><span  style="font-size: 12px; line-height: normal; ">原文链接:<a href="http://www.cuteqt.com/blog/?p=218" style="color: rgb(0, 0, 0); text-decoration: none; font-weight: bold; ">http://www.cuteqt.com/blog/?p=218</a></span></p></span>
<img src ="http://www.cppblog.com/biao/aggbug/86346.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-06-01 09:16 <a href="http://www.cppblog.com/biao/archive/2009/06/01/86346.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: 设置QTextEdit的各种颜色</title><link>http://www.cppblog.com/biao/archive/2009/05/28/86014.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Thu, 28 May 2009 08:14:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/05/28/86014.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/86014.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/05/28/86014.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/86014.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/86014.html</trackback:ping><description><![CDATA[<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><img src="http://www.cppblog.com/images/cppblog_com/biao/ColorRole.png" id="" vspace="0" hspace="0" border="" align="baseline" alt="" longdesc=""><br></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">Widget::Widget(QWidget *parent)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; : QWidget(parent), ui(<span style="color: #aa0d91">new</span> Ui::Widget) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; ui-&gt;setupUi(<span style="color: #aa0d91">this</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; connect(ui-&gt;button, SIGNAL(clicked()), <span style="color: #aa0d91">this</span>, SLOT(setColor()));</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; QPalette p = palette();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; p.setColor(QPalette::Text, QColor(<span style="color: #1c00cf">0</span>, <span style="color: #1c00cf">255</span>, <span style="color: #1c00cf">0</span>));</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; setPalette(p); // 注意这里, 修改了widget的调色板, 子窗口ui-&gt;lineEdit的颜色变化.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">Widget::~Widget() {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; <span style="color: #aa0d91">delete</span> ui;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><span style="color: #aa0d91">void</span> Widget::setColor() {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; QPalette palette = ui-&gt;lineEdit-&gt;palette();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; <span style="color: #aa0d91">const</span> QColor &amp;color = QColorDialog::getColor(palette.color(QPalette::Background), <span style="color: #aa0d91">this</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; <span style="color: #aa0d91">if</span> (color.isValid()) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; palette.setColor(QPalette::Highlight, color);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; ui-&gt;lineEdit-&gt;setPalette(palette);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">}</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><br></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><span  style="font-family: Verdana; font-size: 13px; line-height: 16px; "><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; ">palette.setColor(QPalette::Highlight, color);</p><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; ">此处:</p><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; "><span class="Apple-tab-span" style="line-height: 15px; font-size: 13px; white-space: pre; ">	</span>QPalette::Highlight // 被选中后文字的背景色.</p><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; "><span class="Apple-tab-span" style="line-height: 15px; font-size: 13px; white-space: pre; ">	</span>QPalette::HighlightText // 被选中后文字的前景色.</p><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; "><span class="Apple-tab-span" style="line-height: 15px; font-size: 13px; white-space: pre; ">	</span>QPalette::Text // 文字的前景色</p><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; "><span class="Apple-tab-span" style="line-height: 15px; font-size: 13px; white-space: pre; ">	</span>QPalette::Base // QTextEdit的背景色, 默认是白色的.</p><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; "><br></p><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; ">Role: Qt中会有一组数据, 当取得这些数据时, 使用同样的方法去获取, 为了指明是取得哪种数据, 所以就定义了Role这种概念, 来指明要获取的数据的类型, 例如在View中, 如有显示的文字, 图标, 真正有用的数据(可能没有显示出来, 而是在内部存储着), 取得这些数据都是用同一个方法data(), 通过指明数据的角色来获得.</p><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; "><br></p><p style="font-size: 13px; line-height: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-indent: 20px; font: normal normal normal 14px/normal Courier; ">所有Qt窗口部件都拥有一个QPalette, 并使用它绘制自己. 应用程序还有一个调色板, 通过QApplication::palette()来获得. 修改一个窗口部件的调色板, 只影响这个窗口部件以及子窗口部件(不包含子窗口, 如只是以他作为父对象来管理内存, 而不在他里面显示), 而修改应用程序的调色板, 会影响到该应用程序的所有窗口部件</p></span></p><div><font  face="Courier" size="4"><span  style="font-size: 14px;"><br></span></font></div>
<img src ="http://www.cppblog.com/biao/aggbug/86014.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-05-28 16:14 <a href="http://www.cppblog.com/biao/archive/2009/05/28/86014.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt: 晒晒Pimp My Widgets大赛的优胜作品</title><link>http://www.cppblog.com/biao/archive/2009/05/26/85777.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Tue, 26 May 2009 03:45:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/05/26/85777.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/85777.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/05/26/85777.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/85777.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/85777.html</trackback:ping><description><![CDATA[<span  style="font-family: Arial; font-size: 14px; "><div class="storycontent"><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><span  style="font-size: 12px; line-height: normal; ">原文链接:<a href="http://www.cuteqt.com/blog/?p=232" style="color: rgb(0, 0, 0); text-decoration: none; font-weight: bold; ">http://www.cuteqt.com/blog/?p=232</a></span></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">大家对Trolltech挪威举办的Pimp My Widgets大赛还有印象不？ 这个比赛尘埃落定已经有一段时间了， 一直想做个记录， 和大家分享一下这几个有意思的程序， 今天终于有时间来写写， 走过路过不要错过哦～<br><img class="alignnone" title="Pimp my Widgets" src="http://www.qtsoftware.com/resources-cn/widget-competition/copy_of_Qt_PMW_Logo_cn_v2.png/image_mini" alt="" width="200" height="186"></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">首先可能有些同学对这个比赛还不了解， 简单介绍一下。 Pimp My Widgets大赛主旨是&#8220;做出最酷的Widget&#8221;， 评分标准有五个方面：<br>* Best Use of Qt<br>* Usefulness<br>* Coding Creativity<br>* Portability<br>* Bling Factor<br>翻译成中文就是要最大限度的利用Qt的功能、写出来的程序非常实用、代码极具创造性、可移植（跨平台）、酷炫！ 这次比赛从08年下半年开始至年底共历时约两个半月的时间， 世界各地的Qt爱好者参与了该比赛。</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">下面就来逐个看看优胜者吧， 按照中国人的惯例， 最好的留在最后， 先从一个我觉得比较一般的开始：<br>第一个要介绍的项目叫<strong>WebCarousel</strong>， 作者是<strong><span style="color: rgb(0, 0, 255); ">Matt Broadstone</span></strong>。 从外表来看是一个界面简洁的浏览器， html解析和显示用的Qt自带的Webkit。 其实这个项目的重点是页面切换的功能采用了一个Web Page Carousel， 是一个可旋转的滑动选单， 光说大家不好理解， 看一个项目截图就明了了：<br><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/webcarousel.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-233" title="webcarousel" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/webcarousel.jpg" alt="webcarousel" width="491" height="675" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">为什么我说这个项目一般呢， 那是因为这个功能不算新， 在Qtopia4版本里已经有个一模一样的东西了， 我都怀疑这个是不是抄的Qtopia的设计。 不过肯定两者的实现差别还是很大的。 这个项目用了GraphicsView来实现动画效果， 还用了个第三方的FadeWidget。 如果你是第一个见到有人用Qt写出这样的效果来， 还是会觉得挺炫的！</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">项目源码下载在：<a href="http://www.qtsoftware.com/files/pimpmywidgets/pimp-my-widgets-webcarousel" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; ">&nbsp;http://www.qtsoftware.com/files/pimpmywidgets/pimp-my-widgets-webcarousel</a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">第二个项目叫<strong>Clocks</strong>， 作者<strong><span style="color: rgb(0, 0, 255); ">Thomas Moenicke</span></strong>。 这个项目非常好看， 用GraphicsView实现了好几种不同效果的时钟， 还有淡进淡出的效果， 程序好还得是美工做的好！ 看几个截图：<br><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/clocks_astroclock-small.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-234" title="clocks_astroclock-small" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/clocks_astroclock-small.jpg" alt="clocks_astroclock-small" width="720" height="450" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/clocks_digitalclock-small.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-235" title="clocks_digitalclock-small" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/clocks_digitalclock-small.jpg" alt="clocks_digitalclock-small" width="720" height="450" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/clocks_simpleclock-small.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-236" title="clocks_simpleclock-small" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/clocks_simpleclock-small.jpg" alt="clocks_simpleclock-small" width="720" height="450" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/clocks_worldclock-small.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-237" title="clocks_worldclock-small" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/clocks_worldclock-small.jpg" alt="clocks_worldclock-small" width="720" height="450" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">项目源码下载在：<a href="http://www.qtsoftware.com/files/pimpmywidgets/pimp-my-widgets-clocks" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; ">http://www.qtsoftware.com/files/pimpmywidgets/pimp-my-widgets-clocks</a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">第三个项目叫<strong>AnalogPad</strong>， 作者<strong><span style="color: rgb(0, 0, 255); ">Kaj Groenholm</span></strong>， 笔者比较喜欢这个程序， 它实现了一个类似手机键盘的五向操作盘， 在桌面上显示的效果很有意思， 有点灵活的动画效果， 像个活泼的小精灵， 很Cute， 呵呵。 估计截图看不出这种效果， 还是需要下载下来自己实验一下。 截图先来一张， 详情下载：<br><a href="http://www.qtsoftware.com/files/pimpmywidgets/pimp-my-widgets-analogpad" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; ">http://www.qtsoftware.com/files/pimpmywidgets/pimp-my-widgets-analogpad</a><br><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/analogpad.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-238" title="analogpad" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/analogpad.jpg" alt="analogpad" width="466" height="377" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">铛铛铛～压轴大戏隆重登场了～最后要介绍的项目就是本届大奖得主， Jukka-Pekka Maakelaa写的QuickCalendarView， 这个程序大家务必要下载下来亲身感受。 它的Look＆Feel设计的真是非常不错。 主界面看起来是个经过重新设计的日历界面， 并不特殊， 但是你在这个日历上点一点就会发现， 它会根据你的点选发生动态的改变， 而且它的动态非常流畅、合理， 速度很快， 给用户的感觉很好。 截图也许不能说明什么问题， 下载到这里：<a href="http://www.qtsoftware.com/files/pimpmywidgets/pimp-my-widgets-quickcalendarview" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; ">http://www.qtsoftware.com/files/pimpmywidgets/pimp-my-widgets-quickcalendarview</a><br><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/calendar1.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-239" title="calendar1" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/calendar1.jpg" alt="calendar1" width="800" height="600" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a><br><a href="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/calendar2.jpg" style="color: rgb(0, 0, 0); text-decoration: underline; font-weight: bold; "><img class="alignnone size-full wp-image-240" title="calendar2" src="http://www.cuteqt.com/blog/wp-content/uploads/2009/05/calendar2.jpg" alt="calendar2" width="800" height="600" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></a></p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; ">大奖名不虚传哦， 大家一定要下载下来学习一下人家的设计和实现～ 大奖得主的奖品是传说中的Segway&#174; i2 Personal Transporter哦～～口水ing， 优胜奖也有Nokia的N810带回家， 唉， 可惜国内的Qter没有抓住这个机会啊～～下次&#8230;..咱下次一定努力&#8230;.</p><p style="font: normal normal normal 14px/normal Arial, Verdana, sans-serif; line-height: 17px; "><img class="alignnone" title="http://www.qtsoftware.com/images/developerzone/segway_i2_med.jpg/image_preview  " src="http://www.qtsoftware.com/images/developerzone/segway_i2_med.jpg/image_preview" alt="" width="320" height="400"></p></div><div class="index" style="font-size: 12px; "><br></div></span>
<img src ="http://www.cppblog.com/biao/aggbug/85777.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-05-26 11:45 <a href="http://www.cppblog.com/biao/archive/2009/05/26/85777.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: TextFinder(高亮搜索到的字符串)</title><link>http://www.cppblog.com/biao/archive/2009/05/24/85633.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Sun, 24 May 2009 11:36:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/05/24/85633.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/85633.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/05/24/85633.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/85633.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/85633.html</trackback:ping><description><![CDATA[<img src="http://www.cppblog.com/images/cppblog_com/biao/textFinder.png" id="" width="400" height="398" vspace="0" hspace="0" border="" align="baseline" alt="" longdesc="">

<div><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><font  color="#7F0055"><strong><span  style="color: rgb(0, 0, 0); font-family: Hei; font-size: 16px; font-weight: normal; ">要在QTextEdit中高亮搜索到的字符串, 使用它的QTextDocument和QTextCursor:&nbsp;</span></strong></font></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><font  face="Hei" size="4"><span  style="font-size: 16px;"><br></span></font></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><span style="color: #7f0055"><strong>void</strong></span> <strong>TextFinder::on_findButton_clicked</strong>() {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; QString searchString = ui_lineEdit-&gt;text();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; QTextDocument *document = ui_textEdit-&gt;document();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; <span style="color: #7f0055"><strong>bool</strong></span> found = <span style="color: #7f0055"><strong>false</strong></span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; <span style="color: #7f0055"><strong>if</strong></span> (isFirstTime == <span style="color: #7f0055"><strong>false</strong></span>)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; document-&gt;undo();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; <span style="color: #7f0055"><strong>if</strong></span> (searchString == <span style="color: #2a00ff">""</span>) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; QMessageBox::information(<span style="color: #7f0055"><strong>this</strong></span>, tr(<span style="color: #2a00ff">"Empty Search Field"</span>),</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; color: #2a00ff"><span style="color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>"The search field is empty. Please enter a word and click Find."<span style="color: #000000">);</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; } <span style="color: #7f0055"><strong>else</strong></span> {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; QTextCursor highlightCursor(document);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; QTextCursor cursor(document);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; color: #3f7f5f"><span style="color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; </span>//***************开始***************</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; cursor.beginEditBlock();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; QTextCharFormat plainFormat(highlightCursor.charFormat());</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; QTextCharFormat colorFormat = plainFormat;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; colorFormat.setForeground(Qt::red);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7f0055"><strong>while</strong></span> (!highlightCursor.isNull() &amp;&amp; !highlightCursor.atEnd()) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; highlightCursor = document-&gt;find(searchString, highlightCursor,</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QTextDocument::FindWholeWords);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7f0055"><strong>if</strong></span> (!highlightCursor.isNull()) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found = <span style="color: #7f0055"><strong>true</strong></span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; highlightCursor.movePosition(QTextCursor::WordRight,</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QTextCursor::KeepAnchor);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; highlightCursor.mergeCharFormat(colorFormat);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; cursor.endEditBlock();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; color: #3f7f5f"><span style="color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; </span>//***************结束***************</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; isFirstTime = <span style="color: #7f0055"><strong>false</strong></span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7f0055"><strong>if</strong></span> (found == <span style="color: #7f0055"><strong>false</strong></span>) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QMessageBox::information(<span style="color: #7f0055"><strong>this</strong></span>, tr(<span style="color: #2a00ff">"Word Not Found"</span>),</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; color: #2a00ff"><span style="color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>"Sorry, the word cannot be found."<span style="color: #000000">);</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">}</p></div><img src ="http://www.cppblog.com/biao/aggbug/85633.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-05-24 19:36 <a href="http://www.cppblog.com/biao/archive/2009/05/24/85633.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: 鼠标拖动窗口(没有标题栏时)</title><link>http://www.cppblog.com/biao/archive/2009/05/24/85627.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Sun, 24 May 2009 10:19:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/05/24/85627.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/85627.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/05/24/85627.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/85627.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/85627.html</trackback:ping><description><![CDATA[<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><span style="color: #7f0055"><strong>void</strong></span> <strong>ShapedClock::mousePressEvent</strong>(QMouseEvent *event) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; <span style="color: #7f0055"><strong>if</strong></span> (event-&gt;button() == Qt::LeftButton) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; dragPosition = event-&gt;globalPos() - frameGeometry().topLeft();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; event-&gt;accept();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><span style="color: #7f0055"><strong>void</strong></span> <strong>ShapedClock::mouseMoveEvent</strong>(QMouseEvent *event) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; <span style="color: #7f0055"><strong>if</strong></span> (event-&gt;buttons() &amp; Qt::LeftButton) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; move(event-&gt;globalPos() - dragPosition);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; &nbsp; &nbsp; event-&gt;accept();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">}</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><font  color="#7F0055"><strong><br></strong></font></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><span style="color: #7f0055"><strong>const</strong></span> QPoint &amp; <strong>QMouseEvent::globalPos</strong> () <span style="color: #7f0055"><strong>const</strong></span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px">Returns the global position of the mouse cursor at the time of the event. This is important on asynchronous window systems like X11. Whenever you move your widgets around in response to mouse events, globalPos() may differ a lot from the current pointer position QCursor::pos(), <span style="color: #7f0055"><strong>and</strong></span> from QWidget::mapToGlobal(pos()).</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px">QPoint <strong>QWidget::</strong><strong>mapToGlobal</strong> ( <span style="color: #7f0055"><strong>const</strong></span> QPoint &amp; pos ) <span style="color: #7f0055"><strong>const</strong></span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px">Translates the widget coordinate pos to global screen coordinates. For example, mapToGlobal(QPoint(0,0)) would give the global coordinates of the top-left pixel of the widget.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><br></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">QPoint <strong>QWidget::</strong><strong>mapFromGlobal</strong> ( <span style="color: #7f0055"><strong>const</strong></span> QPoint &amp; pos ) <span style="color: #7f0055"><strong>const</strong></span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px">Translates the global screen coordinate pos to widget coordinates.</p></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><br></p>
<img src ="http://www.cppblog.com/biao/aggbug/85627.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-05-24 18:19 <a href="http://www.cppblog.com/biao/archive/2009/05/24/85627.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: 改变应用程序的外观</title><link>http://www.cppblog.com/biao/archive/2009/05/24/85622.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Sun, 24 May 2009 09:43:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/05/24/85622.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/85622.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/05/24/85622.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/85622.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/85622.html</trackback:ping><description><![CDATA[<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">QApplication::setStyle(QStyleFactory::create(<span style="color: #2a00ff">"plastique"</span>));</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">QStyle * <strong>QStyleFactory::create</strong> ( <span style="color: #7f0055"><strong>const</strong></span> QString &amp; key ) &nbsp; [<span style="color: #7f0055"><strong>static</strong></span>]</p></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">使用QApplication::setStyle可以改变应用程序的外观, QStyleFactory中提供了几种经典的外观.</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">The valid keys can be retrieved using the keys() function. Typically they include "windows", "motif", "cde", "plastique" and "cleanlooks". Depending on the platform, "windowsxp", "windowsvista" and "macintosh" may be available. Note that keys are case insensitive.</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><br></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">另外还可以创建自己的外观风格, 这个是相当的复杂, 然后使用如&nbsp;QApplication::setStyle(<span style="color: #7f0055"><strong>new</strong></span> NorwegianWoodStyle)&nbsp;来进行加载使用外观. Demo中的widgets/styles例子创建了一个新的NorwegianWoodStyle外观.</p>
<img src ="http://www.cppblog.com/biao/aggbug/85622.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-05-24 17:43 <a href="http://www.cppblog.com/biao/archive/2009/05/24/85622.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: 给widget加上背景图</title><link>http://www.cppblog.com/biao/archive/2009/05/24/85620.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Sun, 24 May 2009 09:20:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/05/24/85620.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/85620.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/05/24/85620.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/85620.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/85620.html</trackback:ping><description><![CDATA[

关键使用widget的palette的brush.<div><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><span style="color: #7f0055"><strong>int</strong></span> <strong>main</strong>(<span style="color: #7f0055"><strong>int</strong></span> argc, <span style="color: #7f0055"><strong>char</strong></span> *argv[]) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; QApplication a(argc, argv);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; QWidget *widget = <span style="color: #7f0055"><strong>new</strong></span> QWidget();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; widget-&gt;setAutoFillBackground(<span style="color: #7f0055"><strong>true</strong></span>); // 这句要加上, 否则可能显示不出背景图.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; QPalette palette = widget-&gt;palette();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; palette.setBrush(QPalette::Window,</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QBrush(QPixmap(<span style="color: #2a00ff">"1.png"</span>).scaled( // 缩放背景图.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; widget-&gt;size(),</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Qt::IgnoreAspectRatio,</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Qt::SmoothTransformation))); // 使用平滑的缩放方式</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; widget-&gt;setPalette(palette); // 至此, 已给widget加上了背景图.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; QHBoxLayout *layout = <span style="color: #7f0055"><strong>new</strong></span> QHBoxLayout();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; layout-&gt;addWidget(widget);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; QWidget *mw = <span style="color: #7f0055"><strong>new</strong></span> QWidget();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; mw-&gt;setLayout(layout);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; mw-&gt;resize(500, 500);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; color: #3f7f5f"><span style="color: #000000">&nbsp; &nbsp; </span>//mw-&gt;setWindowFlags(Qt::FramelessWindowHint);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; mw-&gt;show();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; min-height: 17.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">&nbsp; &nbsp; <span style="color: #7f0055"><strong>return</strong></span> a.exec();</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier">}</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier"><img src="http://www.cppblog.com/images/cppblog_com/biao/BackgroundLabel.png"></p></div><img src ="http://www.cppblog.com/biao/aggbug/85620.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-05-24 17:20 <a href="http://www.cppblog.com/biao/archive/2009/05/24/85620.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: 自定义不规则窗体与按钮</title><link>http://www.cppblog.com/biao/archive/2009/05/24/85619.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Sun, 24 May 2009 09:13:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/05/24/85619.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/85619.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/05/24/85619.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/85619.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/85619.html</trackback:ping><description><![CDATA[
<strong>
关键是使用</strong><div><span class="Apple-tab-span" style="white-space:pre"><strong>	</strong></span><strong>void QWidget::setMask ( const QBitmap &amp; bitmap )</strong></div><div><span class="Apple-tab-span" style="white-space:pre"><strong>	</strong></span><strong>void QWidget::setMask ( const QRegion &amp; region )</strong></div><div><div><span style="font-family: 'Lucida Grande'; font-size: 14px; "><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; background-position: initial initial; "><strong>void QWidget::setMask ( const QRegion &amp; region )</strong>

Causes only the parts of the widget which overlap region to be visible. </pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; background-position: initial initial; ">只有widget与region重叠的地方才会显示出来. 自己构造一个QRegion就行了.</pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; background-position: initial initial; ">void ShapedClock::resizeEvent(QResizeEvent * /* event */) {
    int side = qMin(width(), height());
    QRegion maskedRegion(width() / 2 - side / 2, height() / 2 - side / 2, side,
                         side, QRegion::Ellipse);
    setMask(maskedRegion);
}</pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; background-position: initial initial; "><br></pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; background-position: initial initial; "><span style="font-family: Hei; font-size: 16px; white-space: normal; "><strong>void QWidget::setMask ( const QBitmap &amp; bitmap )</strong></span></pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; background-position: initial initial; "><span style="font-family: Hei; font-size: 16px; white-space: normal; ">Causes only the pixels of the widget for which bitmap has a corresponding 1 bit to be visible. If the region includes pixels outside the rect() of the widget, window system controls in that area may or may not be visible, depending on the platform.</span></pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; background-position: initial initial; "><font face="Hei" size="4"><span style="font-size: 16px; white-space: normal;">只有在bitmap中像素数据是1的地方才会显示出widget的相应像素来. <strong>Bitmap</strong>就是像素数据只有两个值: 0和1 (1 bit-depth, monochrome).</span></font></pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; background-position: initial initial; "><font face="Hei" size="4"><span style="font-size: 16px; white-space: normal;"><span style="font-family: 'Lucida Grande'; font-size: 14px; "><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; ">     QLabel topLevelLabel;
     QPixmap pixmap(":/images/tux.png");
     topLevelLabel.setPixmap(pixmap);
     topLevelLabel.setMask(pixmap.mask()); // 可以不使用转换的, 使用一张专门的bitmap图片.</pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; ">上面的这些方式用一普通的QWidget就可以了. 当然, 对于窗口而言, 很多时候我们要把它的标题栏去掉:</pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; "><span  style="font-family: Courier; white-space: normal; color: rgb(63, 127, 95); ">widget-&gt;setWindowFlags(Qt::FramelessWindowHint);</span></pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; "><br></pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; "><br></pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; ">但是对于不规则的<strong>QPushButton</strong>就有些特殊,&nbsp;要使用QIcon来处理:</pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; ">button-&gt;setIcon(QIcon("xxx.png"));</pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; ">button-&gt;setIconSize(w, h);</pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; ">button-&gt;setMask(maskBitmap/*maskedRegion*/);</pre><pre style="padding-top: 0.2em; padding-right: 0.2em; padding-bottom: 0.2em; padding-left: 0.2em; border-top-color: rgb(231, 231, 231); border-right-color: rgb(231, 231, 231); border-bottom-color: rgb(231, 231, 231); border-left-color: rgb(231, 231, 231); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(241, 241, 241); color: black; ">button-&gt;setFixedSize(w, h); // 这个当然最好使用它的icon的大小.</pre></span></span></font></pre></span></div></div><img src ="http://www.cppblog.com/biao/aggbug/85619.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-05-24 17:13 <a href="http://www.cppblog.com/biao/archive/2009/05/24/85619.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: 当QSpinBox的值为最小值时, 显示特殊的字符</title><link>http://www.cppblog.com/biao/archive/2009/05/24/85604.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Sun, 24 May 2009 07:27:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/05/24/85604.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/85604.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/05/24/85604.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/85604.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/85604.html</trackback:ping><description><![CDATA[
<img src="http://www.cppblog.com/images/cppblog_com/biao/SpecialTextSpinBox.png" id="" vspace="0" hspace="0" border="" align="baseline" alt="" longdesc=""><div>如图所示, 当QSpinBox的值为最小值的时候, 不是显示这个最小值, 而是显示一个特殊的字符串用以代替这个最小值, 也只是QSpinBox的一个函数调用而已:</div><div>void QSpinBox::setSpecialValueText(const QString &amp;specialText);</div><div><span style="font-family: 'Lucida Grande'; font-size: 14px; ">If set, the spin box will display this text instead of a numeric value whenever the current value is equal to minimum(). Typical use is to indicate that this choice has a special (default) meaning.</span></div><img src ="http://www.cppblog.com/biao/aggbug/85604.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-05-24 15:27 <a href="http://www.cppblog.com/biao/archive/2009/05/24/85604.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QtWidget: 带有前缀和后缀的QSpinBox</title><link>http://www.cppblog.com/biao/archive/2009/05/24/85600.html</link><dc:creator>暗金装备</dc:creator><author>暗金装备</author><pubDate>Sun, 24 May 2009 07:08:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2009/05/24/85600.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/85600.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2009/05/24/85600.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/85600.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/85600.html</trackback:ping><description><![CDATA[<img src="http://www.cppblog.com/images/cppblog_com/biao/PrefixAndSuffixSpinBox.png" id="" vspace="0" hspace="0" border="" align="baseline" alt="" longdesc=""><br><div>要在QSpinBox的输入区始终显示有一个如"%"的后缀和"$"的前缀, 非常简单:</div><div>只需要简单的调用两个函数即可实现:</div><div>void QSpinBox::setPrefix(const QString &amp;prefox);</div><div>void QSpinBox::setSuffix(const QString &amp;suffix);</div><img src ="http://www.cppblog.com/biao/aggbug/85600.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">暗金装备</a> 2009-05-24 15:08 <a href="http://www.cppblog.com/biao/archive/2009/05/24/85600.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>