Category Archives: Qt

QString与printf的%s

qt 4里
QString str("hello");
printf("%s
", str.data()); //只打印出h
//这是由于在qt4里每个char存留unicode,第二字节为零
解决方法
1. printf("%s
", qPrintable(str));
//qPrintable在qt文档里没有出现,但可在其源码里发现
2. printf("%s
", str.toLatin1());
//toLatin1,toUtf8,toLocal8bit返回QByteArray,可以自动转成 char*

compile qt with msvc.net

1. unzip opensource version

//2. run configure with win32-g++ // obselete, a good methods here

3. cvs_qtmsvc_bcc.zip should merge with qt4msvc_bcc.zip, then overwrote by files from mkspecs-from-evaluation-version

4. set qmakespec to win32-msvc.net
4.1 rename the License.cpl to license.TROLL
4.2 copy qmake-from-eval to qmake dir, rename to qmake.exe
4.2 open the makefile under the qmake dir, comments the all: xxx line, and then add a line "all: "
4.3 run configure, it will configure for you.

5. use qmake -recursive to generate makefiles (in general, this step
will be done during the configure process. but, if you run with any
other makespec rather than msvc.net, please do it again. In a clean installation, it’s ok to skip this step)

6. nmake all files, it will cost much time. but, you can stop it at any
time, and then simply run nmake again to continue the process.

qt 4 installed with msvc.net and dev-c++

qt 4 installed with msvc.net and dev-c++

adipose from qt forum has summerized some points in how to compile qt 4 source open version with msvc.net. here is its url

this thread also address some ideas on how to make qt4 work with dev-c++. apart from this thread, there are some as following:
http://sourceforge.net/forum/forum.php?thread_id=1314521&forum_id=48211
http://www.hoppelkoppel.de/docs/InstallationQt.pdf
http://evilissimo-softdev.de/files/stuff/qt-devcpp-template.txt
http://www.c-plusplus.de/forum/viewtopic-var-t-is-114023-….html

hope to help.

compiling Qt 4 with msvc.net (obselete)

see this item for latest method

1. unpack qt-win-opensource-4.0.0.zip to c:qt4.0.0
2. open cmd, then locate to c:qt4.0.0

c:
cd qt4.0.0

3. unpack qt4msvc_bcc.zip to the Qt 4.0.0 directory
4. configure Qt 4 with MinGW. This step is necessary, otherwise it will prompt missing of files when do nmake.

configure -platform win32-g++

5. go to the qmake directory, then use Makefile.win32-msvc to recompile qmake.

cd qmake
nmake -f Makefile.win32-msvc
I
am also wondering if there is a makefile for win32-msvc.net.
Unfortunately, there is not such a file due to absence of qmake
generator file specific to msvc.net. However, we can regard qmake using
win32-msvc option is compatible with msvc.net environment, considering
the successful final nmake link result.

6. go back the root directory of Qt 4. Using qmake with recursive
option to produce makefiles in current directory and all its
subdirectories.

cd ..
qmake -recursive -spec win32-msvc.net

7. nmake Qt4. The process will be longer than you expected. Be patient. 🙂

nmake

编译是变态的慢,要有心理准备哦!我在dell c640/2.0G/512M上花了将近6个小时才编译完成

需要qt4msvc_bcc.zip的请留email给我.
If you need qt4msvc_bcc.zip, please leave me a message, or drop me an
Email. You can also download this file from Kde-cygwin maillist. Thank
Andreas Hausladen for sending this zip file to the public maillist.

p.s. It will be better to use makespec files got from the win
evaluation Qt 4 version. Just override the counterparts in
qt4msv_bcc.zip. I have done as this, and comipled successfully.

qt 4推出在windows平台下的GPL版

可惜不支持ms VC++或VC.net,网上已经有一些讨论,看是否将qt opensource版在
MsVc.net环境下编译过去,目前来看,好象有些问题,比如trolltech给出opensourc里
的qmake可能跟商业版的qmake不尽相同。现在没有时间整这些,试目以待。
在这个邮件列表里可以搜索到一些 关于此话题的技术讨论
http://lists.kde.org/?l=kde-cygwin&r=1&w=2
在http://www.qtforum.org/board.php?boardid=45j和
http://www.osnews.com/comment.php?news_id=9675&limit=no里也有一些讨论,很是
热闹!

低级错误

犯了一个十分低级的错误,居然花了我很长时间才找到原因。
const QString& PmKeywords::Keyword()
{
return keywords.toString();
}
而 这里,keywords被定义成QValueVector,toString返回QString。大家明白问题出在哪 了吗?由于toString返回QString,而不是QString&,是局部变量,而Keyword属性返回QString&,是一 个引用,即是将局部变量的引用返出来了,但局部变量在scope出了后,就被销毁,这样返出来的就是一个无效的引用。由于整个代码已经近5000行,查错 查起来很麻烦,好歹还是找着了,前面还一直怀疑是否QSqlCursor有问题,在进行QSqlCursor Update的时候发生错误,但根本在于上面描述。一个教训,轻易不要怀疑这些大公司写的商业代码,只能从自己的代码上找原因,这样也许会更有助于除错。

Qt里的用于文字显示的控件

QLable: 显示文字或图片,不提供用户交互
QSimpleRichText: 用于显示由stylesheet定制的rich text,一但构造完成,不能被修

QTextEdit: 支持修改和显示rich text
QTextBrowser: 支持显示rich text,适合有超文本接连的场合,不能被修改。

关于qt国际化

qt对国际化有很好的支持。但默认的是unicode编码。中文系统下默认的并非unicode,
而是比如gbk或gb2312。所以在进行const char*参数传递和显示时必须特别注意。
比如在默认情况下
QMessageBox::information(this, "Test", "这是一个测试");
中文字符无法正确显式
又如
bool connect(const char * connectString);
你如果输入
QString str("一个测试");
connect((const char*)str);
默认是调用QString.latin1()方法,是直接从Unicode的QString返回latin-1字符表示
集。在中文系统下有时不是你想要的。
解决方案:在main.cpp里增加
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
这时涉及到QString/QCString到const char*的转换将使用本地的编码集。
类似的还有setCodecForTr()方法。

NOTE: 这跟qt使用unicode进行内部通讯、显示并不冲突。当显示一个const char*时,
qt会将const char*再行转成unicode。