﻿<?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++博客-CFW</title><link>http://www.cppblog.com/CFW/</link><description /><language>zh-cn</language><lastBuildDate>Thu, 23 Apr 2026 10:07:40 GMT</lastBuildDate><pubDate>Thu, 23 Apr 2026 10:07:40 GMT</pubDate><ttl>60</ttl><item><title>QtWidget: 给widget加上背景图</title><link>http://www.cppblog.com/CFW/archive/2012/02/26/166544.html</link><dc:creator>寻风笔记</dc:creator><author>寻风笔记</author><pubDate>Sun, 26 Feb 2012 03:00:00 GMT</pubDate><guid>http://www.cppblog.com/CFW/archive/2012/02/26/166544.html</guid><wfw:comment>http://www.cppblog.com/CFW/comments/166544.html</wfw:comment><comments>http://www.cppblog.com/CFW/archive/2012/02/26/166544.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/CFW/comments/commentRss/166544.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/CFW/services/trackbacks/166544.html</trackback:ping><description><![CDATA[<span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 13px/16px Verdana, Geneva, Arial, Helvetica, sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">关键使用widget的palette的brush.</span> 
<div style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 13px/16px Verdana, Geneva, Arial, Helvetica, sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">
<p style="text-indent: 20px; margin: 0px; font: 14px Courier"><span style="line-height: 15px; color: rgb(127,0,85); font-size: 13px"><strong>int</strong></span><span class="Apple-converted-space">&nbsp;</span><strong>main</strong>(<span style="line-height: 15px; color: rgb(127,0,85); font-size: 13px"><strong>int</strong></span><span class="Apple-converted-space">&nbsp;</span>argc,<span class="Apple-converted-space">&nbsp;</span><span style="line-height: 15px; color: rgb(127,0,85); font-size: 13px"><strong>char</strong></span><span class="Apple-converted-space">&nbsp;</span>*argv[]) {</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; QApplication a(argc, argv);</p>
<p style="text-indent: 20px; margin: 0px; min-height: 17px; font: 14px Courier"><br /></p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; QWidget *widget =<span class="Apple-converted-space">&nbsp;</span><span style="line-height: 15px; color: rgb(127,0,85); font-size: 13px"><strong>new</strong></span><span class="Apple-converted-space">&nbsp;</span>QWidget();</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; widget-&gt;setAutoFillBackground(<span style="line-height: 15px; color: rgb(127,0,85); font-size: 13px"><strong>true</strong></span>); // 这句要加上, 否则可能显示不出背景图.</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; QPalette palette = widget-&gt;palette();</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; palette.setBrush(QPalette::Window,</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QBrush(QPixmap(<span style="line-height: 15px; color: rgb(42,0,255); font-size: 13px">"1.png"</span>).scaled( // 缩放背景图.</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; widget-&gt;size(),</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Qt::IgnoreAspectRatio,</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Qt::SmoothTransformation))); // 使用平滑的缩放方式</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; widget-&gt;setPalette(palette); // 至此, 已给widget加上了背景图.</p>
<p style="text-indent: 20px; margin: 0px; min-height: 17px; font: 14px Courier"><br /></p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; QHBoxLayout *layout =<span class="Apple-converted-space">&nbsp;</span><span style="line-height: 15px; color: rgb(127,0,85); font-size: 13px"><strong>new</strong></span><span class="Apple-converted-space">&nbsp;</span>QHBoxLayout();</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; layout-&gt;addWidget(widget);</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; QWidget *mw =<span class="Apple-converted-space">&nbsp;</span><span style="line-height: 15px; color: rgb(127,0,85); font-size: 13px"><strong>new</strong></span><span class="Apple-converted-space">&nbsp;</span>QWidget();</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; mw-&gt;setLayout(layout);</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; mw-&gt;resize(500, 500);</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier; color: rgb(63,127,95)"><span style="line-height: 15px; color: rgb(0,0,0); font-size: 13px">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span>//mw-&gt;setWindowFlags(Qt::FramelessWindowHint);</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp; mw-&gt;show();</p>
<p style="text-indent: 20px; margin: 0px; min-height: 17px; font: 14px Courier"><br /></p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span style="line-height: 15px; color: rgb(127,0,85); font-size: 13px"><strong>return</strong></span><span class="Apple-converted-space">&nbsp;</span>a.exec();</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier">}</p>
<p style="text-indent: 20px; margin: 0px; font: 14px Courier"><img src="http://www.cppblog.com/images/cppblog_com/biao/BackgroundLabel.png"  alt="" /></p></div><img src ="http://www.cppblog.com/CFW/aggbug/166544.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/CFW/" target="_blank">寻风笔记</a> 2012-02-26 11:00 <a href="http://www.cppblog.com/CFW/archive/2012/02/26/166544.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt 无边框、透明、可移动、的个性窗体案例详解</title><link>http://www.cppblog.com/CFW/archive/2012/02/26/166543.html</link><dc:creator>寻风笔记</dc:creator><author>寻风笔记</author><pubDate>Sun, 26 Feb 2012 02:51:00 GMT</pubDate><guid>http://www.cppblog.com/CFW/archive/2012/02/26/166543.html</guid><wfw:comment>http://www.cppblog.com/CFW/comments/166543.html</wfw:comment><comments>http://www.cppblog.com/CFW/archive/2012/02/26/166543.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/CFW/comments/commentRss/166543.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/CFW/services/trackbacks/166543.html</trackback:ping><description><![CDATA[<p><span style="font-size: 16px">很多朋友都问透明的效果怎么做，为什么自己做的无边框窗体不可移动，一个个回答的很累，干脆写出来分享下好了，我只用代码说话。</span></p>
<h1>main.cpp</h1>
<h3>int main(int argc, char *argv[])<br />{<br />QApplication::setStyle("cleanlooks");<br />QApplication a(argc, argv);<br />login w;<br />w.setWindowTitle("ClientLogin");</h3>
<h3><br />w.setWindowOpacity(1);<br />w.setWindowFlags(Qt::FramelessWindowHint);<br />w.setAttribute(Qt::WA_TranslucentBackground);<br />w.show();<br />w.move(200,100);<br />return a.exec();<br />}</h3>
<p><br /></p>
<p><span style="font-size: 16px">关键的语句，就是其中的</span></p>
<h2>w.setWindowOpacity(1);<br />w.setWindowFlags(Qt::FramelessWindowHint);<br />w.setAttribute(Qt::WA_TranslucentBackground);</h2>
<h3>这些语句，不知道什么意思就摁下F1，或者直接查阅帮助文档&#8230;&#8230;</h3>
<h3>对窗体无边框的设置要写在main里面，这样所有派生的子窗口，QDialog,QWidget都可继承，很好规划和管理，方便统一美化设计。以工程中一个聊天窗口为例，先用PS制作一个窗体的背景图片，注意存为png格式，这是透明的关键。不会使PS，可以找些PNG资源图片。我的PNG透明背景图为：</h3>
<h3><span style="background-color: rgb(255,255,255)">将它添加到你的资源包中，然后设置为窗体的背景。</span></h3>
<h3><span style="background-color: rgb(255,255,255)">这个你就可以预览到透明的无边框窗体了，但是还有一个重要的问题，窗口竟然无法移动。</span></h3>
<h3><span style="background-color: rgb(255,255,255)">这也是无边框导致的&#8230;&#8230;具体原因我不细说，搜一下很清晰，我只说解决方案。</span></h3>
<h3><span style="background-color: rgb(255,255,255)">在每个子窗口中，都添加：</span></h3>
<h3><span style="background-color: rgb(255,255,255)">void yourwindow::mousePressEvent(QMouseEvent *event){</span></h3>
<h3><span style="background-color: rgb(255,255,255)">this-&gt;windowPos = this-&gt;pos();</span></h3>
<h3><span style="background-color: rgb(255,255,255)">this-&gt;mousePos = event-&gt;globalPos();</span></h3>
<h3><span style="background-color: rgb(255,255,255)">this-&gt;dPos = mousePos - windowPos;</span></h3>
<h3><span style="background-color: rgb(255,255,255)">}</span></h3>
<h3><span style="background-color: rgb(255,255,255)">void yourwindow::mouseMoveEvent(QMouseEvent *event){</span></h3>
<h3><span style="background-color: rgb(255,255,255)">this-&gt;move(event-&gt;globalPos() - this-&gt;dPos);</span></h3>
<h3><span style="background-color: rgb(255,255,255)">}</span></h3>
<h3><span style="background-color: rgb(255,255,255)">void yourwindow::changeEvent(QEvent *e){</span></h3>
<h3><span style="background-color: rgb(255,255,255)">QMainWindow::changeEvent(e);</span></h3>
<h3><span style="background-color: rgb(255,255,255)">switch (e-&gt;type()) {</span></h3>
<h3><span style="background-color: rgb(255,255,255)">case QEvent::LanguageChange: ui-&gt;retranslateUi(this);</span></h3>
<h3><span style="background-color: rgb(255,255,255)">break;</span></h3>
<h3><span style="background-color: rgb(255,255,255)">default:</span></h3>
<h3><span style="background-color: rgb(255,255,255)">break;</span></h3>
<h3><span style="background-color: rgb(255,255,255)">}</span></h3>
<h3><span style="background-color: rgb(255,255,255)">}</span></h3>
<h3><span style="background-color: rgb(255,255,255)">这样就大功告成了，运行一下看看效果.</span></h3><img src ="http://www.cppblog.com/CFW/aggbug/166543.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/CFW/" target="_blank">寻风笔记</a> 2012-02-26 10:51 <a href="http://www.cppblog.com/CFW/archive/2012/02/26/166543.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT中执行文件图标的添加（exe）</title><link>http://www.cppblog.com/CFW/archive/2012/02/26/166542.html</link><dc:creator>寻风笔记</dc:creator><author>寻风笔记</author><pubDate>Sun, 26 Feb 2012 02:50:00 GMT</pubDate><guid>http://www.cppblog.com/CFW/archive/2012/02/26/166542.html</guid><wfw:comment>http://www.cppblog.com/CFW/comments/166542.html</wfw:comment><comments>http://www.cppblog.com/CFW/archive/2012/02/26/166542.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/CFW/comments/commentRss/166542.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/CFW/services/trackbacks/166542.html</trackback:ping><description><![CDATA[1、将.ico文件放在工程文件中，命名为myappico.ico。 
<div>2、添加.rc文件，并命名为myapp.rc。<br />&nbsp;&nbsp;&nbsp;&nbsp; 并在该文件中添加代码：IDI_ICON1ICONDISCARDABLE"myappico.ico"。<br />3、在.pro文件中加入代码：</div>
<div><pre style="margin-top: 0px; margin-bottom: 0px"><span style="color: #800080">   RC_FILE</span> = myapp.rc</pre><pre style="margin-top: 0px; margin-bottom: 0px">   其中myicon.rc为rc文件。</pre></div><img src ="http://www.cppblog.com/CFW/aggbug/166542.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/CFW/" target="_blank">寻风笔记</a> 2012-02-26 10:50 <a href="http://www.cppblog.com/CFW/archive/2012/02/26/166542.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT中背景的添加</title><link>http://www.cppblog.com/CFW/archive/2012/02/26/166541.html</link><dc:creator>寻风笔记</dc:creator><author>寻风笔记</author><pubDate>Sun, 26 Feb 2012 02:42:00 GMT</pubDate><guid>http://www.cppblog.com/CFW/archive/2012/02/26/166541.html</guid><wfw:comment>http://www.cppblog.com/CFW/comments/166541.html</wfw:comment><comments>http://www.cppblog.com/CFW/archive/2012/02/26/166541.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/CFW/comments/commentRss/166541.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/CFW/services/trackbacks/166541.html</trackback:ping><description><![CDATA[<div>1、将需要添加的图片放入工程文件夹<br />2、在该窗口的.h文件中声明该函数：<br /><span style="color: #808000">void</span><span style="color: #c0c0c0"> </span>paintEvent<span style="color: #000000">(</span><span style="color: #800080">QPaintEvent</span><span style="color: #c0c0c0"> </span><span style="color: #000000">*);</span><br />3、在该窗口的.cpp中写出该函数：<br /><span style="color: #808000">void</span><span style="color: #c0c0c0"> </span>Widget<span style="color: #000000">::</span>paintEvent<span style="color: #000000">(</span><span style="color: #800080">QPaintEvent</span><span style="color: #c0c0c0"> </span><span style="color: #000000">*)</span><pre style="text-indent: 0px; margin: 0px; -qt-block-indent: 0"><span style="color: #000000">{</span></pre><pre style="text-indent: 0px; margin: 0px; -qt-block-indent: 0"><span style="color: #c0c0c0">    </span><span style="color: #800080">QPainter</span><span style="color: #c0c0c0"> </span>painter<span style="color: #000000">(</span><span style="color: #808000">this</span><span style="color: #000000">);</span></pre><pre style="text-indent: 0px; margin: 0px; -qt-block-indent: 0"><span style="color: #c0c0c0">    </span><span style="color: #800080">QPixmap</span><span style="color: #c0c0c0"> </span>pix<span style="color: #000000">;</span></pre><pre style="text-indent: 0px; margin: 0px; -qt-block-indent: 0"><span style="color: #c0c0c0">    </span>pix<span style="color: #000000">.</span>load<span style="color: #000000">(</span><span style="color: #008000">"images/aa.png"</span><span style="color: #000000">);</span></pre><pre style="text-indent: 0px; margin: 0px; -qt-block-indent: 0"><span style="color: #c0c0c0">    </span>painter<span style="color: #000000">.</span>drawPixmap<span style="color: #000000">(</span><span style="color: #000080">0</span><span style="color: #000000">,</span><span style="color: #000080">0</span><span style="color: #000000">,</span><span style="color: #000080">100</span><span style="color: #000000">,</span><span style="color: #000080">100</span><span style="color: #000000">,</span>pix<span style="color: #000000">);</span></pre><pre style="text-indent: 0px; margin: 0px; -qt-block-indent: 0"><span style="color: #000000">}</span></pre><br /><br /><br />运行即可得到你想要的效果。</div> <img src ="http://www.cppblog.com/CFW/aggbug/166541.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/CFW/" target="_blank">寻风笔记</a> 2012-02-26 10:42 <a href="http://www.cppblog.com/CFW/archive/2012/02/26/166541.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>