﻿<?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</title><link>http://www.cppblog.com/cc/category/4796.html</link><description>半亩方塘 天光云影</description><language>zh-cn</language><lastBuildDate>Tue, 13 Nov 2012 16:31:46 GMT</lastBuildDate><pubDate>Tue, 13 Nov 2012 16:31:46 GMT</pubDate><ttl>60</ttl><item><title>Qt程序Release模式编译</title><link>http://www.cppblog.com/cc/archive/2012/11/12/195075.html</link><dc:creator>北风之神007</dc:creator><author>北风之神007</author><pubDate>Mon, 12 Nov 2012 06:01:00 GMT</pubDate><guid>http://www.cppblog.com/cc/archive/2012/11/12/195075.html</guid><wfw:comment>http://www.cppblog.com/cc/comments/195075.html</wfw:comment><comments>http://www.cppblog.com/cc/archive/2012/11/12/195075.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cc/comments/commentRss/195075.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cc/services/trackbacks/195075.html</trackback:ping><description><![CDATA[<span style="font-size: 12pt; ">&nbsp; &nbsp;在Linux下用Qt开发了一些界面的程序，编译出来结果比较大，有的都上百兆，查了之后才知道编译出来的程序不是release模式的，我们需要在pro文件中增加编译指令，用release模式编译，效率方面没有做测试，不过程序体积只是之前的十分之一，正常了很多。<br /><br />其实我们要做的不多，只要在pro文件中增加以下的选项即可，这样就可以release模式编译了。<br /><br /></span><div style="font-size: 12pt; "><strong style="font-family: 'Times New Roman'; font-size: 18pt; color: #0000ff; ">CONFIG+= &nbsp;release</strong></div><img src ="http://www.cppblog.com/cc/aggbug/195075.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cc/" target="_blank">北风之神007</a> 2012-11-12 14:01 <a href="http://www.cppblog.com/cc/archive/2012/11/12/195075.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux下QT程序字体设定错误造成程序中文乱码</title><link>http://www.cppblog.com/cc/archive/2010/09/25/127673.html</link><dc:creator>北风之神007</dc:creator><author>北风之神007</author><pubDate>Sat, 25 Sep 2010 14:37:00 GMT</pubDate><guid>http://www.cppblog.com/cc/archive/2010/09/25/127673.html</guid><wfw:comment>http://www.cppblog.com/cc/comments/127673.html</wfw:comment><comments>http://www.cppblog.com/cc/archive/2010/09/25/127673.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cc/comments/commentRss/127673.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cc/services/trackbacks/127673.html</trackback:ping><description><![CDATA[<br><span style="FONT-FAMILY: 微软雅黑">&nbsp;&nbsp;<span style="FONT-SIZE: 18pt"> 最近在Linux环境下写QT的程序，在我的测试机器上面QT程序，在启动的时候设定了字符集为GBK，中文都是显示正常的，没有任何问题，然后同学在他的虚拟机上面也写了QT程序，可以怎么弄中文都是乱码，什么GBK，UTF-8等等的编码都设定了，可是依然乱码，分析这种情况应该不是程序的问题，很可能是QT字体的问题，于是在命令行输入<span style="COLOR: #0000ff">qtconfig</span>，其他了qt配置程序，在其中设定了其他的字体，发现程序的中文就能正常显示了。<br><br>&nbsp; 因此，有时候程序没有问题了，但是QT的字体设定错误也会造成中文乱码现象，我们可以不通过qtconfig程序改变默认qt程序的字体，还可以在程序中强制设定程序本身所用的默认字体，这样也是可以的，这里只介绍这样的思路，代码就不贴了，希望大家碰见的时候有个参考。</span></span><img src ="http://www.cppblog.com/cc/aggbug/127673.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cc/" target="_blank">北风之神007</a> 2010-09-25 22:37 <a href="http://www.cppblog.com/cc/archive/2010/09/25/127673.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT Designer中安装QWT插件</title><link>http://www.cppblog.com/cc/archive/2008/09/05/60987.html</link><dc:creator>北风之神007</dc:creator><author>北风之神007</author><pubDate>Fri, 05 Sep 2008 04:54:00 GMT</pubDate><guid>http://www.cppblog.com/cc/archive/2008/09/05/60987.html</guid><wfw:comment>http://www.cppblog.com/cc/comments/60987.html</wfw:comment><comments>http://www.cppblog.com/cc/archive/2008/09/05/60987.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cc/comments/commentRss/60987.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cc/services/trackbacks/60987.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 14pt">&nbsp; QWT是一套非常不错的开发库，它能结合QT开发，做出非常好的<font style="FONT-SIZE: 14pt" size=2>曲线，刻度，表盘等效果来。<br><br>&nbsp; qwt的下载以及动态链接库的编译等这里就不做介绍了。在源码目录下可以找到designer目录，其中有插件的源码，编译完之后就会生成qwtplugin.dll和qwtplugin.lib，把他们拷贝到<font face=Arial>$(QTDIR)\plugins\designer目录下，重新启动QT Designer，就会看到ToolBox面板中多出了Qwt类，如下图所示。以上在QT3.3.4下测试通过，<a style="COLOR: red" href="http://www.cppblog.com/Files/cc/qwtplugin.rar"><u>qwtplugin.dll和qwtplugin.lib下载<br></u></a>
<div align=center src_cetemp="/images/cppblog_com/cc/2008_9/qwt.jpg"><img height=648 alt="" src="http://www.cppblog.com/images/cppblog_com/cc/2008_9/qwt.jpg" width=843 border=0></div>
</font></font></span><img src ="http://www.cppblog.com/cc/aggbug/60987.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cc/" target="_blank">北风之神007</a> 2008-09-05 12:54 <a href="http://www.cppblog.com/cc/archive/2008/09/05/60987.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT中求取中文字符串的长度</title><link>http://www.cppblog.com/cc/archive/2007/10/16/34371.html</link><dc:creator>北风之神007</dc:creator><author>北风之神007</author><pubDate>Tue, 16 Oct 2007 12:40:00 GMT</pubDate><guid>http://www.cppblog.com/cc/archive/2007/10/16/34371.html</guid><wfw:comment>http://www.cppblog.com/cc/comments/34371.html</wfw:comment><comments>http://www.cppblog.com/cc/archive/2007/10/16/34371.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cc/comments/commentRss/34371.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cc/services/trackbacks/34371.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 14pt">&nbsp;&nbsp;&nbsp;在使用QT编程的时候，我们处理字符串一般都使用的QT自带的QString类型，这个类型类似于STL中的string类型，还是非常好用的。不过QT的编码方式使用Unicode，我猜测真是由于这个原因，再使用QString的length()方法返回字符串长度的时候就会出现偏差，对于一般的西文字母是没有问题的，但是对于中文字符却有问题了，它把一个中文字符当作一个字节来处理了，所以像字符串&#8220;你好QT&#8221;的长度返回的是4，而不是6。<br><br>问题出现了，那么我们需要怎么解决呢，思路很简单，就是转换编码，然后再进行长度的求取，QT的文档里面有好多函数，试验了半天，终于找到一个合适的函数，顺利的完成了我的要求。该函数就是fromLocal8Bit ( const char * local8Bit, int len = -1 )<br><br><span style="FONT-SIZE: 14pt"><strong><em>QCString QString::local8Bit () const <br>以本地指定的格式返回字符串。在X11系统中，使用QTextCodec::codecForLocale()。 在Windows系统中，它是一种系统指定的编码方式。在Mac OS X中，使用uft8作为编码方式。<br></em></strong><br>&nbsp;有了上面的铺垫，下面就问题就好办了，要想求取带中文的字符串的长度，只需要稍微多走一步而已。假设有字符串str="QT你好",那么我们主要进行str.local8Bit().length(),这样结果就是6，符合我们的要求了，事情就是这样，此文完！</span></p><img src ="http://www.cppblog.com/cc/aggbug/34371.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cc/" target="_blank">北风之神007</a> 2007-10-16 20:40 <a href="http://www.cppblog.com/cc/archive/2007/10/16/34371.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转帖】常见的几个Qt编程问题的处理</title><link>http://www.cppblog.com/cc/archive/2007/09/27/33041.html</link><dc:creator>北风之神007</dc:creator><author>北风之神007</author><pubDate>Thu, 27 Sep 2007 10:21:00 GMT</pubDate><guid>http://www.cppblog.com/cc/archive/2007/09/27/33041.html</guid><wfw:comment>http://www.cppblog.com/cc/comments/33041.html</wfw:comment><comments>http://www.cppblog.com/cc/archive/2007/09/27/33041.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cc/comments/commentRss/33041.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cc/services/trackbacks/33041.html</trackback:ping><description><![CDATA[１、如果在窗体关闭前自行判断是否可关闭<br>答：重新实现这个窗体的closeEvent()函数，加入判断操作<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>void MainWindow::closeEvent(QCloseEvent *event)<br>{<br>&nbsp; &nbsp; &nbsp; if (maybeSave())<br>&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writeSettings();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="COLOR: blue">event-&gt;accept();</span><br>&nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="COLOR: blue">event-&gt;ignore();</span><br>&nbsp; &nbsp; &nbsp; }<br>}<br></blockquote><br><br>２、如何用打开和保存文件对话<br>答：使用QFileDialog<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="COLOR: blue">QString fileName = QFileDialog::getOpenFileName(this);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!fileName.isEmpty())</span><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadFile(fileName);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br></blockquote><br><br>
<h6 class=quote>Quote:</h6>
<blockquote><br>&nbsp; &nbsp; &nbsp; <span style="COLOR: blue">QString fileName = QFileDialog::getSaveFileName(this);<br>&nbsp; &nbsp; &nbsp; if (fileName.isEmpty())</span><br>&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; &nbsp; }<br></blockquote><br><br>３、如果创建Actions(可在菜单和工具栏里使用这些Action)<br>答：<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>newAct = new QAction(QIcon(":/images/new.png"), tr("&amp;New"), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; newAct-&gt;setShortcut(tr("Ctrl+N"));<br>&nbsp; &nbsp; &nbsp; &nbsp; newAct-&gt;setStatusTip(tr("Create a new file"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; openAct = new QAction(QIcon(":/images/open.png"), tr("&amp;Open..."), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; openAct-&gt;setShortcut(tr("Ctrl+O"));<br>&nbsp; &nbsp; &nbsp; &nbsp; openAct-&gt;setStatusTip(tr("Open an existing file"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(openAct, SIGNAL(triggered()), this, SLOT(open()));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; saveAct = new QAction(QIcon(":/images/save.png"), tr("&amp;Save"), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; saveAct-&gt;setShortcut(tr("Ctrl+S"));<br>&nbsp; &nbsp; &nbsp; &nbsp; saveAct-&gt;setStatusTip(tr("Save the document to disk"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; saveAsAct = new QAction(tr("Save &amp;As..."), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; saveAsAct-&gt;setStatusTip(tr("Save the document under a new name"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; exitAct = new QAction(tr("E&amp;xit"), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; exitAct-&gt;setShortcut(tr("Ctrl+Q"));<br>&nbsp; &nbsp; &nbsp; &nbsp; exitAct-&gt;setStatusTip(tr("Exit the application"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&amp;t"), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; cutAct-&gt;setShortcut(tr("Ctrl+X"));<br>&nbsp; &nbsp; &nbsp; &nbsp; cutAct-&gt;setStatusTip(tr("Cut the current selection's contents to the "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "clipboard"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; copyAct = new QAction(QIcon(":/images/copy.png"), tr("&amp;Copy"), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; copyAct-&gt;setShortcut(tr("Ctrl+C"));<br>&nbsp; &nbsp; &nbsp; &nbsp; copyAct-&gt;setStatusTip(tr("Copy the current selection's contents to the "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "clipboard"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&amp;Paste"), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; pasteAct-&gt;setShortcut(tr("Ctrl+V"));<br>&nbsp; &nbsp; &nbsp; &nbsp; pasteAct-&gt;setStatusTip(tr("Paste the clipboard's contents into the current "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "selection"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; aboutAct = new QAction(tr("&amp;About"), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; aboutAct-&gt;setStatusTip(tr("Show the application's About box"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; aboutQtAct = new QAction(tr("About &amp;Qt"), this);<br>&nbsp; &nbsp; &nbsp; &nbsp; aboutQtAct-&gt;setStatusTip(tr("Show the Qt library's About box"));<br>&nbsp; &nbsp; &nbsp; &nbsp; connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));<br></blockquote><br>４、如果创建主菜单<br>答：采用上面的QAction的帮助，创建主菜单<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>&nbsp; &nbsp; &nbsp; fileMenu = menuBar()-&gt;addMenu(tr("&amp;File"));<br>&nbsp; &nbsp; &nbsp; &nbsp; fileMenu-&gt;addAction(newAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; fileMenu-&gt;addAction(openAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; fileMenu-&gt;addAction(saveAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; fileMenu-&gt;addAction(saveAsAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; fileMenu-&gt;addSeparator();<br>&nbsp; &nbsp; &nbsp; &nbsp; fileMenu-&gt;addAction(exitAct);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; editMenu = menuBar()-&gt;addMenu(tr("&amp;Edit"));<br>&nbsp; &nbsp; &nbsp; &nbsp; editMenu-&gt;addAction(cutAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; editMenu-&gt;addAction(copyAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; editMenu-&gt;addAction(pasteAct);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; menuBar()-&gt;addSeparator();<br><br>&nbsp; &nbsp; &nbsp; &nbsp; helpMenu = menuBar()-&gt;addMenu(tr("&amp;Help"));<br>&nbsp; &nbsp; &nbsp; &nbsp; helpMenu-&gt;addAction(aboutAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; helpMenu-&gt;addAction(aboutQtAct);<br></blockquote><br><br>５、如果创建工具栏<br>答：采用上面的QAction的帮助，创建工具栏<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>&nbsp; &nbsp; &nbsp; fileToolBar = addToolBar(tr("File"));<br>&nbsp; &nbsp; &nbsp; &nbsp; fileToolBar-&gt;addAction(newAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; fileToolBar-&gt;addAction(openAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; fileToolBar-&gt;addAction(saveAct);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; editToolBar = addToolBar(tr("Edit"));<br>&nbsp; &nbsp; &nbsp; &nbsp; editToolBar-&gt;addAction(cutAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; editToolBar-&gt;addAction(copyAct);<br>&nbsp; &nbsp; &nbsp; &nbsp; editToolBar-&gt;addAction(pasteAct);<br></blockquote><br>６、如何使用配置文件保存配置<br>答：使用QSettings类<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>&nbsp; &nbsp; &nbsp; QSettings settings("Trolltech", "Application Example");<br>&nbsp; &nbsp; &nbsp; &nbsp; QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();<br>&nbsp; &nbsp; &nbsp; &nbsp; QSize size = settings.value("size", QSize(400, 400)).toSize();<br></blockquote><br><br>
<h6 class=quote>Quote:</h6>
<blockquote><br>&nbsp; &nbsp; &nbsp; QSettings settings("Trolltech", "Application Example");<br>&nbsp; &nbsp; &nbsp; &nbsp; settings.setValue("pos", pos());<br>&nbsp; &nbsp; &nbsp; &nbsp; settings.setValue("size", size());<br></blockquote><br><br>７、如何使用警告、信息等对话框<br>答：使用QMessageBox类的静态方法<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>int ret = QMessageBox::warning(this, tr("Application"),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tr("The document has been modified.\n"<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Do you want to save your changes?"),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QMessageBox::Yes | QMessageBox::Default,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QMessageBox::No,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QMessageBox::Cancel | QMessageBox::Escape);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ret == QMessageBox::Yes)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return save();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (ret == QMessageBox::Cancel)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;<br></blockquote><br><br>８、如何使通用对话框中文化<br>答：对话框的中文化<br>比如说，QColorDialog的与文字相关的部分，主要在qcolordialog.cpp文件中，我们可以从qcolordialog.cpp用 lupdate生成一个ts文件，然后用自定义这个ts文件的翻译，再用lrelease生成一个.qm文件，当然了，主程序就要改变要支持多国语言了，使用这个.qm文件就可以了。<br><br>另外，还有一个更快的方法，在源代码解开后有一个目录translations，下面有一些.ts, .qm文件，我们拷贝一个：<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>cp src/translations/qt_untranslated.ts ./qt_zh_CN.ts<br></blockquote><br>然后，我们就用Linguist打开这个qt_zh_CN.ts，进行翻译了，翻译完成后，保存后，再用lrelease命令生成qt_zh_CN.qm，这样，我们把它加入到我们的qt project中，那些系统的对话框，菜单等等其它的默认是英文的东西就能显示成中文了。<br><br>９、在Windows下Qt里为什么没有终端输出？<br>答：把下面的配置项加入到.pro文件中<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>win32:CONFIG += console<br></blockquote><br><br>10、Qt 4 for X11 OpenSource版如何静态链接？<br>答：编译安装的时候加上-static选项<br>
<h6 class=quote>Quote:</h6>
<blockquote>./configure -static&nbsp; ／／一定要加static选项<br>gmake<br>gmake install<br></blockquote><br>然后，在Makefile文件中加 static 选项或者在.pro文件中加上QMAKE_LFLAGS += -static，就可以连接静态库了。<br><br>11、想在源代码中直接使用中文，而不使用tr()函数进行转换，怎么办？<br>答：在main函数中加入下面三条语句，但并不提倡<br>
<h6 class=quote>Quote:</h6>
<blockquote>&nbsp; &nbsp; QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));<br>QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));<br>QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));<br></blockquote><br>或者<br>
<h6 class=quote>Quote:</h6>
<blockquote>&nbsp; &nbsp; QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));<br>QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));<br>QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));<br></blockquote><br>使用GBK还是使用UTF-8，依源文件中汉字使用的内码而定<br>这样，就可在源文件中直接使用中文，比如：<br>
<h6 class=quote>Quote:</h6>
<blockquote>QMessageBox::information(NULL, "信息", "关于本软件的演示信息", QMessageBox::Ok, QMessageBox::NoButtons);<br></blockquote><br><br>12、为什么将开发的使用数据库的程序发布到其它机器就连接不上数据库？<br>答：这是由于程序找不到数据库插件而致，可照如下解决方法：<br>在main函数中加入下面语句：<br>
<h6 class=quote>Quote:</h6>
<blockquote>QApplication::addLibraryPath(strPluginsPath");<br></blockquote><br>strPluginsPath是插件所在目录，比如此目录为/myapplication/plugins<br>则将需要的sql驱动，比如qsqlmysql.dll, qsqlodbc.dll或对应的.so文件放到<br>/myapplication/plugins/sqldrivers/<br>目录下面就行了<br>这是一种解决方法，还有一种通用的解决方法，即在可执行文件目录下写qt.conf文件，把系统相关的一些目录配置写到qt.conf文件里，详细情况情参考Qt Document Reference里的qt.conf部分<br><br><br>13、如何创建QT使用的DLL(.so)以及如何使用此DLL(.so)<br>答：创建DLL时其工程使用lib模板<br>
<h6 class=quote>Quote:</h6>
<blockquote>TEMPLATE=lib<br></blockquote><br>而源文件则和使用普通的源文件一样，注意把头文件和源文件分开，因为在其它程序使用此DLL时需要此头文件<br>在使用此DLL时，则在此工程源文件中引入DLL头文件，并在.pro文件中加入下面配置项：<br>
<h6 class=quote>Quote:</h6>
<blockquote>LIBS += -Lyourdlllibpath -lyourdlllibname<br></blockquote><br>Windows下和Linux下同样（Windows下生成的DLL文件名为yourdlllibname.dll而在Linux下生成的为libyourdlllibname.so<br><br>14、如何启动一个外部程序<br>答：可使用QProcess和QThread这两个类结合使用的方法来处理，以防止在主线程中调用而导致阻塞的情况<br>先从QThread继承一个类，重新实现run()函数：<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>class MyThread : public QThread<br>{<br>public:<br>&nbsp; &nbsp; void run();<br>};<br><br>void MyThread::run()<br>{<br>&nbsp; &nbsp; QProcess::execute("notepad.exe");<br>}<br></blockquote><br>这样，在使用的时候则可定义一个MyThread类型的成员变量，使用时调用其start()方法：<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>class ...............<br>{...........<br>MyThread thread;<br>............<br>};<br><br>.....................<br>thread.start();<br></blockquote><br><br>15、如何打印报表<br>答：Qt目前对报表打印支持的库还很少，不过有种变通的方法，就是使用XML+XSLT+XSL-FO来进行报表设计，XML输出数据，用XSLT将XML数据转换为XSL-FO格式的报表，由于现在的浏览器不直接支持XSL-FO格式的显示，所以暂时可用工具(Apache FOP, Java做的)将XSL-FO转换为PDF文档来进行打印，转换和打印由FOP来做，生成XSL-FO格式的报表可以由Qt来生成，也可以由其它内容转换过来，比如有工具(html2fo)将HTML转换为XSL-FO。<br><br>16、如何在系统托盘区显示图标<br>答：在4.2及其以上版本中使用QSystemTrayIcon类来实现<br><br>17、怎样将日志输出到文件中<br>答：（myer提供）<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>void myMessageOutput( QtMsgType type, const char *msg )<br>{<br>&nbsp; &nbsp; switch ( type ) {<br>&nbsp; &nbsp; &nbsp; &nbsp; case QtDebugMsg:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //写入文件；<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; &nbsp; case QtWarningMsg:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; &nbsp; case QtFatalMsg:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abort();<br>&nbsp; &nbsp; }<br>}<br><br>int main( int argc, char** argv )<br>{<br>&nbsp; &nbsp; QApplication app( argc, argv );<br>&nbsp; &nbsp; qInstallMsgHandler( myMessageOutput );<br>&nbsp; &nbsp; ......<br>&nbsp; &nbsp; return app.exec();<br>}<br></blockquote><br>qDebug(), qWarning(), qFatal()分别对应以上三种type。<br><br>18、如何将图像编译到可执行程序中去<br>答：使用.qrc文件<br>写.qrc文件，例如：<br>res.qrc<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>&lt;!DOCTYPE RCC&gt;&lt;RCC version="1.0"&gt;<br>&lt;qresource&gt;<br>&nbsp; &nbsp; &lt;file&gt;images/copy.png&lt;/file&gt;<br>&nbsp; &nbsp; &lt;file&gt;images/cut.png&lt;/file&gt;<br>&nbsp; &nbsp; &lt;file&gt;images/new.png&lt;/file&gt;<br>&nbsp; &nbsp; &lt;file&gt;images/open.png&lt;/file&gt;<br>&nbsp; &nbsp; &lt;file&gt;images/paste.png&lt;/file&gt;<br>&nbsp; &nbsp; &lt;file&gt;images/save.png&lt;/file&gt;<br>&lt;/qresource&gt;<br>&lt;/RCC&gt;<br></blockquote><br>然后在.pro中加入下面代码：<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>RESOURCES&nbsp; &nbsp; = res.qrc<br></blockquote><br><br>在程序中使用：<br>
<h6 class=quote>Quote:</h6>
<blockquote><br>...<br>:images/copy.png<br>...<br></blockquote><br><br>19、如何制作不规则形状的窗体或部件<br>答：请参考下面的帖子<br><a href="http://www.qtcn.org/bbs/read.php?tid=8681" target=_blank><font color=#2f5fa1>http://www.qtcn.org/bbs/read.php?tid=8681</font></a><img src ="http://www.cppblog.com/cc/aggbug/33041.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cc/" target="_blank">北风之神007</a> 2007-09-27 18:21 <a href="http://www.cppblog.com/cc/archive/2007/09/27/33041.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT读取中文文件</title><link>http://www.cppblog.com/cc/archive/2007/08/25/30824.html</link><dc:creator>北风之神007</dc:creator><author>北风之神007</author><pubDate>Sat, 25 Aug 2007 12:28:00 GMT</pubDate><guid>http://www.cppblog.com/cc/archive/2007/08/25/30824.html</guid><wfw:comment>http://www.cppblog.com/cc/comments/30824.html</wfw:comment><comments>http://www.cppblog.com/cc/archive/2007/08/25/30824.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/cc/comments/commentRss/30824.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cc/services/trackbacks/30824.html</trackback:ping><description><![CDATA[<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;虽然C++标准中有了文件读取的相关类，也很好用，但是在涉及到QT编程的时候却用起来不方便了，因为QT本身很多组件都是关联的自身的QString类型的字符串，所以再用C++本身String类型的时候就不是那么方便了，需要进行转化，这样给程序带来了复杂度，同时也带来了转化的开销，所以如果用QT开发，可以就用它本身所带的这些类型进行处理，形成一个系统，便于数据在程序之中的交互和共用。<br>&nbsp;&nbsp;&nbsp;QT很好，但是在处理中文或者其他语言的时候要注意编码格式，如果没有注意，读取文件的时候可能读出来的就是乱码或者干脆程序就死掉了，这是我们所不愿意看到的，下面就讲讲怎么样通过QT的类来读取中文文件。</span><br><br><span style="COLOR: #000000">介绍部分<br>&nbsp;&nbsp;&nbsp;我们需要用到几个头文件中的类：<br>#include&nbsp;&lt;qstring.h&gt;<br>#include&nbsp;&lt;qfile.h&gt;<br>#include&nbsp;&lt;qtextstream.h&gt;<br>#include&nbsp;&lt;qtextcodec.h&gt;<br></span><br>
<p style="COLOR: #000000"><span style="FONT-SIZE: 14pt"><strong>QString</strong></span><br><br>QString类提供了一个Unicode文本和经典的C以零结尾的字符数组的抽象。 <br>QString使用隐含共享，这使它非常有效率并且很容易使用。 <br>所有的QString的方法都使用const char *参数，const char *被解释为经典的C风格的以零结尾的ASCII字符串。所以const char *参数为0是合法的。如果const char *不是以零结尾的，结果是不确定的。把经典的C字符串复制到QString的函数将不会复制结尾的0字符。QString的QChar数组（可以通过unicode()返回）通常不以零结尾。如果你需要把QString传递到一个需要C的以零结尾的字符串，请使用latin1()。 <br>没有分配任何东西的QString是零，也就是长度和数据指针都为0。引用空字符串（&#8220;&#8221;，一个单一的'\0'字符）的QString是空。零和空这两个QString在方法中都是合法的。把(const char *) 0赋值给QString给定了一个零QString。为了方便，QString::null是一个零QString。当排序的时候，空字符串在最前面，然后是非空字符串，然后才是零字符串。我们建议使用if ( !str.isNull() )，而不是if ( !str )来检测非零字符串，关于解释说明也可以参考operator!()。 <br>注意如果你发现你正在混合使用QCString、QString和QByteArray，这将会导致很多不必要的复制并且也许会预示着你正在处理的真实自然数据是不确定的。如果数据是以零结尾的八位数据，请使用QCString；如果它是没有结尾的（也就是包含0）八位数据，请使用QByteArray；如果它是文本，请使用QString。 <br>字符串列表可以使用QStringList类来处理。你可以使用QStringList::split()来把一个字符串分割为一个字符串列表，并且可以使用QStringList::join()把一个字符串列表连接成一个使用随意间隔符的字符串。你也可以使用QStringList::grep()从一个字符串列表中获得包含特定子字符串或者包含匹配特定的regex的字符串列表。 <br>////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br><span style="FONT-SIZE: 18pt"><strong>QFile<br></strong></span><br>QFile类是一个操作文件的输入/输出设备。 <br>QFile是用来读写二进制文件和文本文件的输入/输出设备。QFile可以自己单独被使用，但是如果和QDataStream或QTextStream一起使用将更加方便。 <br>文件名通常可以通过构造函数来传递，但也可以使用setName()来设置。你可以通过exists()来检查一个文件是否存在并且可以通过remove()来移去一个文件。 <br>文件可以用open()来打开、用close()来关闭、用flush()来刷新。数据通常可以使用QDataStream或者QTextStream进行读写，但你也可以使用readBlock()和readLine()来读，使用writeBlock()来写。QFile也支持getch()、 ungetch()和putch()。 <br>size()可以返回文件的大小。你可以通过使用at()函数得到当前文件位置或者移到一个新的文件位置。如果你到了文件的末尾，atEnd()返回真。handle()返回文件句柄。 <br>这里是一个使用QTextStream来一行一行地读取一个文本文件的代码段。它会把每一行带上一个行号打印出来。 </p>
<p style="COLOR: #000000">&nbsp;&nbsp;&nbsp; QStringList lines;<br>&nbsp;&nbsp;&nbsp; QFile file( "file.txt" );<br>&nbsp;&nbsp;&nbsp; if ( file.open( IO_ReadOnly ) ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QTextStream stream( &amp;file );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QString line;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int n = 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ( !stream.eof() ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; line = stream.readLine(); // 不包括&#8220;\n&#8221;的一行文本<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf( "%3d: %s\n", n++, line.latin1() );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lines += line;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file.close();<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp; <br>写文本也很容易（假设我们有一个行的字符串列表要写）： </p>
<p style="COLOR: #000000">&nbsp;&nbsp;&nbsp; QFile file( "file.txt" );<br>&nbsp;&nbsp;&nbsp; if ( file.open( IO_WriteOnly ) ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QTextStream stream( &amp;file );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream &lt;&lt; *it &lt;&lt; "\n";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file.close();<br>&nbsp;&nbsp;&nbsp; }<br>/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////&nbsp; <br><span style="FONT-SIZE: 18pt"><strong>QTextStream<br></strong></span><br>QTextStream类提供了使用QIODevice读写文本的基本功能。 <br>文本流类的功能界面和标准的C++的iostream类非常相似。iostream和QTextStream的不同点是我们的流操作在一个很容易被继承的QIODevice上，而iostream只能操作一个不能被继承的FILE *指针。 <br>Qt提供了几个和iostream相似的全局函数： <br>bin设置QTextStream来读/写二进制数字 <br>oct设置QTextStream来读/写八进制数字 <br>dec设置QTextStream来读/写十进制数字 <br>hex设置QTextStream来读/写十六进制数字 <br>endl强制换行 <br>flush强制QIODevice刷新任何被缓存的数据 <br>ws作为任何可用的控制符（在输入的时候） <br>reset重新设置QTextStream为它的缺省模式（请见reset()） <br>qSetW(int)设置字段宽度作为指定参数 <br>qSetFill(int)设置填充字符作为指定参数 <br>qSetPrecision(int)设置精确度作为指定参数 <br>警告：默认情况下，QTextStream在读取流的时候，会自动地检测流中的数字是十进制、八进制、十六进制或者二进制格式。具体情况是，一个以&#8220;0&#8221;为开头的数字是八进制的，比如顺序为&#8220;0100&#8221;将会被解释为64。 <br>QTextStream类读写文本，它不适合处理二进制数据（而QDataStream是适合的）。 <br>默认情况下，输出的是使用本地8位编码后的Unicode文本（比如，QString）。这些可以使用setEncoding()方法进行改变。对于输入，QTextStream会自动检测标准Unicode&#8220;字节顺序标记的&#8221;文本文件，否则会使用本地8位编码。 <br>QIODevice在构造函数中被设置，或者之后在setDevice()中使用。如果输入到达了atEnd()，返回真。数据可以使用operator&gt;&gt;()重载操作符读到适当类型的变量中，或者使用read()把它作为整个部分读到一个单一的字符串中，或者使用readLine()把一次读一行。使用skipWhiteSpace()可以忽略控制符。你可以使用flags()或setf()来设置流的标记。这个流也支持width()、precision()和 fill()，使用reset()可以重新恢复默认设置。 <br>也可以参考QDataStream、输入/输出和网络和文本相关类.<br>/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br><br><span style="FONT-SIZE: 18pt"><strong>QTextCodec</strong></span><br><br>QTextCodec 类提供文本编码之间的转换。<br>QT使用Unicode来存储，绘制以及操作字符串。在很多情况下，你可能想要使用不同的编码方式来处理数据。例如大部分的日语文件都被存储在Shift-JIS或者 ISO2022的文件中，而俄罗斯的用户常常使用KOI8-R或者CP1251编码方式。QT提供了一个QTextCodec 类集合来从Unicode格式转化到相应的格式。</p>
<p style="COLOR: #000000" align=left>/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br><br>代码部分<br><br>#include &lt;qstring.h&gt;<br>#include &lt;qfile.h&gt;<br>#include &lt;qtextstream.h&gt;<br>#include &lt;qtextcodec.h&gt;</p>
<p style="COLOR: #000000" align=left>int main() <br>{&nbsp;&nbsp;&nbsp;<br>&nbsp;QFile file("test.txt");<br>&nbsp;if (file.open(IO_ReadOnly|IO_Raw))<br>&nbsp;{<br>&nbsp;&nbsp;QTextStream&nbsp;floStream(&amp;file);<br>&nbsp;&nbsp;QString line;<br><span style="BACKGROUND-COLOR: yellow">&nbsp;&nbsp;QTextCodec *codec=QTextCodec::codecForName("GBK");<br>&nbsp;&nbsp;floStream.setCodec(codec);</span>&nbsp;<br>&nbsp;&nbsp;while ( floStream.atEnd()==0 ) <br>&nbsp;&nbsp;{<br><span style="BACKGROUND-COLOR: yellow">&nbsp;&nbsp;&nbsp;line = codec-&gt;fromUnicode(floStream.readLine());</span><br>&nbsp;&nbsp;&nbsp;qWarning(line);<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;file.close();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;}<br>&nbsp;return 0;<br>}<br>代码中的主要改动就是黄底的部分<br>意思就是创立一个中文GBK编码样式，然后按照这种方式来把读入的文件流进行重新编码，这样中文就可以顺利输出了，不信你可以试一试，哈哈，先介绍这么多，下次有什么下次再写啦。<br></p><img src ="http://www.cppblog.com/cc/aggbug/30824.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cc/" target="_blank">北风之神007</a> 2007-08-25 20:28 <a href="http://www.cppblog.com/cc/archive/2007/08/25/30824.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VC6.0怎么样使用moc编译</title><link>http://www.cppblog.com/cc/archive/2007/08/13/29932.html</link><dc:creator>北风之神007</dc:creator><author>北风之神007</author><pubDate>Mon, 13 Aug 2007 13:32:00 GMT</pubDate><guid>http://www.cppblog.com/cc/archive/2007/08/13/29932.html</guid><wfw:comment>http://www.cppblog.com/cc/comments/29932.html</wfw:comment><comments>http://www.cppblog.com/cc/archive/2007/08/13/29932.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cc/comments/commentRss/29932.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cc/services/trackbacks/29932.html</trackback:ping><description><![CDATA[<span style="font-size: 18pt; color: #040000;">现在在学习QT界面开发，在windows环境下装了vc6和QT3.3.4版本的开发包，装好之后就可以在VC集成开发环境下产生一个工具栏进行QT开发了。在学习书上例子的时候，总是有些代码中要使用到Q_OBJECT宏，这样才可以使用自定义的signal还有slot了，不过试了好多次都编译不成功，好像是需要什么moc编译，真是晕，书上也没有说这个，那些自带的例子都是可以编译的，直接用工具栏上面的那个moc也不管用，要是你知道你就告诉我下怎么用。<br><br>查了下资料，moc就是元对象编程。<br><br><span style="background-color: yellow;">元对象编译器读取一个C++源文件。如果它发现其中的一个或多个类的声明中含有Q_OBJECT宏，它就会给这个使用Q_OBJECT宏的类生成另外一个包含元对象代码的C++源文件。尤其是，元对象代码对信号/槽机制、运行时类型信息和动态属性系统是需要的。 <br>一个被元对象编译器生成的C++源文件必须和这个类的实现一起被编译和连接（或者它被包含到（#include）这个类的源文件中）。 <br>如果你是用qmake来生成你的Makefile文件，当需要的时候，编译规则中需要包含调用元对象编译器，所以你不需要直接使用元对象编译器。<br><br>这里提供一种方法：类的声明放在一个头文件（.h文件）中 <br>如果在上述的文件myclass.h中发现类的声明，元对象编译器的输出文件将会被放在一个叫moc_myclass.cpp的文件中。这个文件将会像通常情况一样被编译，作为对象文件的结果是moc_myclass.o（在Unix下）或者moc_myclass.obj（在Windows下）。这个对象接着将会被包含到一个对象文件列表中，它们将会在程序的最后连编阶段被连接在一起。 <br><br>然后在<br></span>（1）在FileView视图中 右键点击需要产生MOC文件的头文件（就是类中包含Q_OBJECT宏，如果没有这个宏就不需要产生MOC文件）<br><br>（2）在右键菜单中选择Setting...<br><br>（3）选择Custom Build，在Commands栏中输入：<br>%QTDIR%\bin\moc.exe $(InputDir)\$(InputName).h -o $(InputDir)\moc_$(InputName).cpp<br><br>Outputs中输入：<br>$(InputDir)\moc_$(InputName).cpp<br>
<div style="text-align: left;"><img alt="" src="http://www.cppblog.com/images/cppblog_com/cc/2007_8/1.jpg" height="426" width="417"><br>（4）然后右键这个头文件编译<br><img alt="" src="http://www.cppblog.com/images/cppblog_com/cc/2007_8/2.jpg" height="213" width="276"><br><br>(5)最后生成一个 moc编译产生的cpp源文件<br><img alt="" src="http://www.cppblog.com/images/cppblog_com/cc/2007_8/3.jpg" height="373" width="659"><br></div>
<br>这样编译时就会在当前工程目录中自动生成MOC文件了<br><span style="background-color: #ffffff;"><br>本文参考 qt文档以及http://www.qobject.com/read.php?fid=46&amp;tid=2296&amp;toread=1
的文章。<br><br>谢谢！<br><br><br><br></span> </span><img src ="http://www.cppblog.com/cc/aggbug/29932.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cc/" target="_blank">北风之神007</a> 2007-08-13 21:32 <a href="http://www.cppblog.com/cc/archive/2007/08/13/29932.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt中中文处理的简单方法</title><link>http://www.cppblog.com/cc/archive/2007/07/29/28973.html</link><dc:creator>北风之神007</dc:creator><author>北风之神007</author><pubDate>Sun, 29 Jul 2007 08:39:00 GMT</pubDate><guid>http://www.cppblog.com/cc/archive/2007/07/29/28973.html</guid><wfw:comment>http://www.cppblog.com/cc/comments/28973.html</wfw:comment><comments>http://www.cppblog.com/cc/archive/2007/07/29/28973.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cc/comments/commentRss/28973.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cc/services/trackbacks/28973.html</trackback:ping><description><![CDATA[<span style="COLOR: #020000">&nbsp;&nbsp;&nbsp; QT是一套很不错的界面开发库，而且考虑到了跨平台的要求，使用也相对比较容易上手。我也刚刚才学习用QT开发，发现它对中文的处理做的不是很好，或者更贴切的是做的不够智能吧，如果在字符串中输入中文，显示的就会是乱码。<br><br>&nbsp;&nbsp;&nbsp; 下面就介绍一个简单的方法，让我们的中文正确显示出来，先看一段程序，该程序主要功能就是显示一个窗口，窗口上面的按钮显示中文。<br><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qapplication.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qpushbutton.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qfont.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qvbox.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;5</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qtextcodec.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;6</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qstring.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qobject.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;8</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;argc,&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">**</span><span style="COLOR: #000000">argv)<br></span><span style="COLOR: #008080">&nbsp;9</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;QApplication&nbsp;a(argc,argv);<br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;QVBox&nbsp;box;<br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;box.resize(</span><span style="COLOR: #000000">200</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">120</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;QPushButton&nbsp;quit(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">退出</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">box)</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">15</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;quit.setFont(&nbsp;QFont(&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Times</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">18</span><span style="COLOR: #000000">,&nbsp;QFont::Bold&nbsp;)&nbsp;);</span><span style="COLOR: #008080"></span><span style="COLOR: #000000"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">&nbsp;&nbsp;&nbsp; QObject::connect(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">quit,SIGNAL(clicked()),</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a,SLOT(quit()));<br></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">&nbsp;&nbsp;&nbsp; a.setMainWidget(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">box);<br></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">&nbsp;&nbsp;&nbsp; box.show();<br></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">&nbsp;&nbsp;&nbsp; </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;a.exec();<br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">}<br></span><span style="COLOR: #008080"></span><span style="COLOR: #000000"></span></div>
<br><span style="COLOR: #040000">在vc6.0下面编译运行程序之后我们会见到界面如下，但是上面的按钮却是乱码了。<br><br></span></span>
<div style="TEXT-ALIGN: center"><span style="COLOR: #020000"><img height=127 alt="" src="http://www.cppblog.com/images/cppblog_com/cc/2007_7/lm.png" width=208></span><br><span style="COLOR: #020000"></span></div>
<span style="COLOR: #020000"><br style="COLOR: #040000"><span style="COLOR: #040000">那我们该怎么办呢？别着急，其实方法很简单，主要思路就是给应用设定一个字符集，并且把要显示的字符做适当的转化处理就可以了，说起来好像还有些复杂，但是看看代码你就很快明白了。</span><br><br><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qapplication.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qpushbutton.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qfont.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qvbox.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;5</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qtextcodec.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;6</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qstring.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">qobject.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;8</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;argc,&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">**</span><span style="COLOR: #000000">argv)<br></span><span style="COLOR: #008080">&nbsp;9</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;QApplication&nbsp;a(argc,argv);<br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #000000; BACKGROUND-COLOR: yellow">&nbsp;&nbsp;&nbsp;&nbsp;a.setDefaultCodec(QTextCodec::codecForName(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">GBK</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;QVBox&nbsp;box;<br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;box.resize(</span><span style="COLOR: #000000">200</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">120</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #000000; BACKGROUND-COLOR: yellow">&nbsp;&nbsp;&nbsp;&nbsp;QPushButton&nbsp;quit(QObject::tr(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">退出</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">),</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">box);<br></span><span style="COLOR: #008080">15</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;quit.setFont(&nbsp;QFont(&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Times</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">18</span><span style="COLOR: #000000">,&nbsp;QFont::Bold&nbsp;)&nbsp;);<br></span><span style="COLOR: #008080">16</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;QObject::connect(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">quit,SIGNAL(clicked()),</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a,SLOT(quit()));<br></span><span style="COLOR: #008080">17</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;a.setMainWidget(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">box);<br></span><span style="COLOR: #008080">18</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;box.show();<br></span><span style="COLOR: #008080">19</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;a.exec();<br></span><span style="COLOR: #008080">20</span>&nbsp;<span style="COLOR: #000000">}</span></div>
<br></span><span style="COLOR: #020000"><br><span style="COLOR: #040000">在vc6.0下面编译运行程序之后我们会见到界面如下，这个时候就显示正常了。</span><br><br></span>
<div style="TEXT-ALIGN: center"><span style="COLOR: #020000"><img height=147 alt="" src="http://www.cppblog.com/images/cppblog_com/cc/2007_7/zc.png" width=208><br><br></span>
<div style="TEXT-ALIGN: left">代码分析：<br>&nbsp; 上面的两段代码基本上是大同小异，没有多大的差别，程序的具体含义这里我不做介绍，只讲一下两段代码的区别，见上例中代码的黄色部分，该部分就是为中文显示所做出的改进。<br>&nbsp;setDefaultCodec(QTextCodec::codecForName("GBK"));<br>查阅了qt的函数帮助可知，<br>setDefaultCodec的作用如下：<br>void QApplication::setDefaultCodec ( QTextCodec * codec ) <br>设置应用程序的默认编解码器为codec。 <br>如果程序中的文字引用文本不是Latin1编码，这个函数可以用来设置合适的编码。例如，韩国程序员开发的软件在程序中对所有文本可以使用eucKR，这种情况下，main()函数可能看起来像这样： <br><br>&nbsp;&nbsp;&nbsp; int main(int argc, char** argv)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QApplication app(argc, argv);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... install any additional codecs ...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.setDefaultCodec( QTextCodec::codecForName("eucKR") );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp; <br><span style="COLOR: #000000">注意，这不是用户来选择编码的方式。例如，把一个包含英语字符串的应用程序转换为韩语，所需要做的一切就是对于所有的英语字符串通过tr()传递并且载入翻译文件。有关国际化的详细情况，请参考Qt国际化文档。 <br>注意也有一些Qt内置类为不同的字符串调用tr()。这些字符串使用英语，所以对于完整的翻译，编解码器将被这些字符串需要。<br></span><br><br>QPushButton&nbsp;quit(QObject::tr("退出"),&amp;box);<br>tr()函数的说明如下：<br>QString QObject::tr ( const char * sourceText, const char * comment ) const <br>返回sourceText的翻译版本，或者如果没有合适的版本返回sourceText它自己。翻译上下文是comment的QObject（默认为空）。所有使用Q_OBJECT宏的QObject对象有这个函数的重新实现，把子类名作为上下文。 <br>也可以参考trUtf8()、QApplication::translate()和Qt中的国际化。 <br>实例：network/networkprotocol/view.cpp。 <br>QString QObject::trUtf8 ( const char * sourceText, const char * comment ) const <br>返回sourceText的翻译版本，或者如果没有合适的版本返回QString::fromUtf8(sourceText)。它是另一种tr(sourceText, comment)。 <br>也可以参考tr()和QApplication::translate()。 <br><br></div>
<div style="TEXT-ALIGN: left"><span style="COLOR: #020000">两个函数的作用一个用来设定字符集一个是用来进行字符串转化，刚好满足了前面提到的思路，事情就是这样的，呵呵，就写到这里，希望对你有用。</span><br><span style="COLOR: #020000"></span></div>
<span style="COLOR: #020000"></span></div><img src ="http://www.cppblog.com/cc/aggbug/28973.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cc/" target="_blank">北风之神007</a> 2007-07-29 16:39 <a href="http://www.cppblog.com/cc/archive/2007/07/29/28973.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>