显示标签为“qt”的博文。显示所有博文
显示标签为“qt”的博文。显示所有博文

2007年9月29日星期六

常见的几个Qt编程问题的处理

常见的几个Qt编程问题的处理

1、如果在窗体关闭前自行判断是否可关闭
答:重新实现这个窗体的closeEvent()函数,加入判断操作
Quote:

void MainWindow::closeEvent(QCloseEvent *event)
{
if (maybeSave())
{
writeSettings();
event->accept();
}
else
{
event->ignore();
}
}


2、如何用打开和保存文件对话
答:使用QFileDialog
Quote:

QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())

{
loadFile(fileName);
}


Quote:

QString fileName = QFileDialog::getSaveFileName(this);
if (fileName.isEmpty())

{
return false;
}


3、如果创建Actions(可在菜单和工具栏里使用这些Action)
答:
Quote:

newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcut(tr("Ctrl+N"));
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));

openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
saveAct->setShortcut(tr("Ctrl+S"));
saveAct->setStatusTip(tr("Save the document to disk"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));

saveAsAct = new QAction(tr("Save &As..."), this);
saveAsAct->setStatusTip(tr("Save the document under a new name"));
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));

exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
cutAct->setShortcut(tr("Ctrl+X"));
cutAct->setStatusTip(tr("Cut the current selection's contents to the "
"clipboard"));
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));

copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
copyAct->setShortcut(tr("Ctrl+C"));
copyAct->setStatusTip(tr("Copy the current selection's contents to the "
"clipboard"));
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));

pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
pasteAct->setShortcut(tr("Ctrl+V"));
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
"selection"));
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));

aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

4、如果创建主菜单
答:采用上面的QAction的帮助,创建主菜单
Quote:

fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);

editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);

menuBar()->addSeparator();

helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);


5、如果创建工具栏
答:采用上面的QAction的帮助,创建工具栏
Quote:

fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);

editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);

6、如何使用配置文件保存配置
答:使用QSettings类
Quote:

QSettings settings("Trolltech", "Application Example");
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(400, 400)).toSize();


Quote:

QSettings settings("Trolltech", "Application Example");
settings.setValue("pos", pos());
settings.setValue("size", size());


7、如何使用警告、信息等对话框
答:使用QMessageBox类的静态方法
Quote:

int ret = QMessageBox::warning(this, tr("Application"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No,
QMessageBox::Cancel | QMessageBox::Escape);
if (ret == QMessageBox::Yes)
return save();
else if (ret == QMessageBox::Cancel)
return false;


8、如何使通用对话框中文化
答:对话框的中文化
比 如说,QColorDialog的与文字相关的部分,主要在qcolordialog.cpp文件中,我们可以从qcolordialog.cpp用 lupdate生成一个ts文件,然后用自定义这个ts文件的翻译,再用lrelease生成一个.qm文件,当然了,主程序就要改变要支持多国语言了, 使用这个.qm文件就可以了。

另外,还有一个更快的方法,在源代码解开后有一个目录translations,下面有一些.ts, .qm文件,我们拷贝一个:
Quote:

cp src/translations/qt_untranslated.ts ./qt_zh_CN.ts

然 后,我们就用Linguist打开这个qt_zh_CN.ts,进行翻译了,翻译完成后,保存后,再用lrelease命令生成qt_zh_CN.qm, 这样,我们把它加入到我们的qt project中,那些系统的对话框,菜单等等其它的默认是英文的东西就能显示成中文了。

9、在Windows下Qt里为什么没有终端输出?
答:把下面的配置项加入到.pro文件中
Quote:

win32:CONFIG += console


10、Qt 4 for X11 OpenSource版如何静态链接?
答:编译安装的时候加上-static选项
Quote:
./configure -static //一定要加static选项
gmake
gmake install

然后,在Makefile文件中加 static 选项或者在.pro文件中加上QMAKE_LFLAGS += -static,就可以连接静态库了。

11、想在源代码中直接使用中文,而不使用tr()函数进行转换,怎么办?
答:在main函数中加入下面三条语句,但并不提倡
Quote:
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

或者
Quote:
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));

使用GBK还是使用UTF-8,依源文件中汉字使用的内码而定
这样,就可在源文件中直接使用中文,比如:
Quote:
QMessageBox::information(NULL, "信息", "关于本软件的演示信息", QMessageBox::Ok, QMessageBox::NoButtons);


12、为什么将开发的使用数据库的程序发布到其它机器就连接不上数据库?
答:这是由于程序找不到数据库插件而致,可照如下解决方法:
在main函数中加入下面语句:
Quote:
QApplication::addLibraryPath(strPluginsPath");

strPluginsPath是插件所在目录,比如此目录为/myapplication/plugins
则将需要的sql驱动,比如qsqlmysql.dll, qsqlodbc.dll或对应的.so文件放到
/myapplication/plugins/sqldrivers/
目录下面就行了
这是一种解决方法,还有一种通用的解决方法,即在可执行文件目录下写qt.conf文件,把系统相关的一些目录配置写到qt.conf文件里,详细情况情参考Qt Document Reference里的qt.conf部分


13、如何创建QT使用的DLL(.so)以及如何使用此DLL(.so)
答:创建DLL时其工程使用lib模板
Quote:
TEMPLATE=lib

而源文件则和使用普通的源文件一样,注意把头文件和源文件分开,因为在其它程序使用此DLL时需要此头文件
在使用此DLL时,则在此工程源文件中引入DLL头文件,并在.pro文件中加入下面配置项:
Quote:
LIBS += -Lyourdlllibpath -lyourdlllibname

Windows下和Linux下同样(Windows下生成的DLL文件名为yourdlllibname.dll而在Linux下生成的为libyourdlllibname.so

14、如何启动一个外部程序
答:可使用QProcess和QThread这两个类结合使用的方法来处理,以防止在主线程中调用而导致阻塞的情况
先从QThread继承一个类,重新实现run()函数:
Quote:

class MyThread : public QThread
{
public:
void run();
};

void MyThread::run()
{
QProcess::execute("notepad.exe");
}

这样,在使用的时候则可定义一个MyThread类型的成员变量,使用时调用其start()方法:
Quote:

class ...............
{...........
MyThread thread;
............
};

.....................
thread.start();


15、如何打印报表
答: 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。

16、如何在系统托盘区显示图标
答:在4.2及其以上版本中使用QSystemTrayIcon类来实现

17、怎样将日志输出到文件中
答:(myer提供)
Quote:

void myMessageOutput( QtMsgType type, const char *msg )
{
switch ( type ) {
case QtDebugMsg:
//写入文件;
break;
case QtWarningMsg:
break;
case QtFatalMsg:
abort();
}
}

int main( int argc, char** argv )
{
QApplication app( argc, argv );
qInstallMsgHandler( myMessageOutput );
......
return app.exec();
}

qDebug(), qWarning(), qFatal()分别对应以上三种type。

18、如何将图像编译到可执行程序中去
答:使用.qrc文件
写.qrc文件,例如:
res.qrc
Quote:



images/copy.png
images/cut.png
images/new.png
images/open.png
images/paste.png
images/save.png



然后在.pro中加入下面代码:
Quote:

RESOURCES = res.qrc


在程序中使用:
Quote:

...
:images/copy.png
...


19、如何制作不规则形状的窗体或部件
答:请参考下面的帖子
http://www.qtcn.org/bbs/read.php?tid=8681

2007年9月27日星期四

Qt4.2.3编译记录

Qt4.2.3编译记录

Posted on 2007-06-19 09:27 chemz 阅读(561) 评论(1) 编辑 收藏 引用 所属分类: C++
Qt4.2.3编译记录
Qt已经发布了最新架构的版本Qt4了,目前的最新版本已经到了Qt4.3,由于原来还使用
的是Qt3.3.4的版本,所以就Qt4种的2.3版进行了编译,准备升级到Qt4。这个编译过程一般
而言还算是比较的简单,只是由于Visual Studio 2005在进行了SP1后引入了一个编译bug,
导致编译Qt4时候会出现编译时错误,同时一般而言在Qt4.2.3的编译选项中包含了一些和
Visual Studio2005默认不相同的选项(同时也是标准不太兼容的地方),所以现在将编译
的具体过程进行详细的说明。

1. 设置环境变量
首先肯定是要解压缩Qt4.2.3的源代码包到某个目录下(这里使用的是commercial版本),
如:D:\Qt\4.2.3\。然后在系统环境目录下添加一个QTDIR=D:\Qt\4.2.3环境变量,并且
在系统的PATH追加%QTDIR%\bin目录项,以使得可以找到编译后的库和应用程序。
2. 应用Patch
Qt4中有两个地方需要进行修改才能够正常的编译,一个是关于编译选项的,另外一个是
关于源代码的。
2.1. 修改编译选项
进入到%QTDIR%\mkspecs\win32-msvc2005\目录下,打开qmake.conf文件找到19行的
QMAKE_CFLAGS = -nologo -Zm200 -Zc:wchar_t-,删除后面的-Zc:wchar_t-选项,
主要是为了保持标准兼容性以及和其他代码的兼容性,否则会出现Qt4中采用了
wchar_t的函数不能够接受没有-Zc:wchar_t选项下的wchar_t类型;
2.2. 修改源代码
进入到%QTDIR%\src\corelib\tools\目录下,打开qmap.h文件找到QMultiMap类的
定义,用如下的内容替换replace和insert函数:
typedef QMap::iterator QMap_iterator;
inline QMap_iterator replace(const Key &akey, const T &avalue){ return QMap::insert(akey, avalue); }
inline QMap_iterator insert(const Key &akey, const T &avalue){ return QMap::insertMulti(akey, avalue); }
并将下面的replace和insert函数的定义体给注释掉;
同样的打开qhash.h文件找到QMultiHash类的定义,用如下的内容替换replace和
insert函数:
typedef QHash::iterator QHash_iterator;
inline typename QHash_iterator replace(const Key &akey, const T &avalue){ return QHash::insert(akey, avalue); }
inline typename QHash_iterator insert(const Key &akey, const T &avalue){ return QHash::insertMulti(akey, avalue); }
并将下面的replace和insert函数的定义体给注释掉。
之所以要这样修改是因为Visual Studio2005的SP1所引入的bug导致的。
3. 安装协议文件
要安装商业版本就必须具有安装商业的license,将.qt-license文件拷贝到用户的
%USERPROFILE%目录下,如:C:\Documents and Settings\Administrator\。
4. 配置并编译
打开console窗口,并设置VC的环境变量,然后运行如下的配置命令:
configure -debug-and-release -no-stl -no-sql-sqlite -qt-libjpeg -qt-gif -platform win32-msvc2005
成功后就可以通过运行nmake来完成源代码的完全编译了。
上面的命令行中,根据你的要求可以自行调整,一般而言为了成功编译必须要包含的命令
选项是-debug-and-release和-platform win32-msvc2005两个,其他可以选择。

QT4 和 VS2005 SP1 编译错误 C2244

Author : sanchaji2
Article ID : 94
Audience : Default
Version 1.00
Published Date: 2007/8/7 6:49:30
Reads : 32
一个BUG的修正,可能引入更多BUG的出现不仅仅是教科书上的套话。今天,我在重新编译QT商业版源代码时,就遇到了这样的一个例子。VS2005安装 SP1补丁后,编译QT4源代码(QT v4.2.2版),编译器报告很多模板方面的致命错误,导致编译失败。通过查询网络可知,这是SP1引入的BUG,微软已经发布了修正补丁,您可以在 http://support.microsoft.com/kb/930198 找到详细信息,另外,微软给出了该错误的详细描述,请参考 http://support.microsoft.com/kb/928957

我解决这个问题是通过修改配置文件,使 QT 系统调用 INTEL C++ Compiler 来编译代码避开这个BUG。如果您坚持希望通过MSVC 2005 SP1编译QT源代码,请安装上面提及的补丁,或者参考 thought 的 文章 修改QT源代码。

下面附上,我这里用到的 bin\qtvars.bat,如果您有微软的 KB930198 补丁,希望您能发送到我的电子邮箱 12985462 at QQ dot com ,谢谢!
下面再附上,一份QT与VS2005-SP1的兼容性修正补丁,不用自己修改了!


@echo off
rem
rem This file is generated
rem

echo Setting up a Qt environment...
echo -- QTDIR set to C:\Qt\4.2.2
echo -- Added C:\Qt\4.2.2\bin to PATH
echo -- QMAKESPEC set to win32-icc

set QTDIR=C:\Qt\4.2.2
set PATH=C:\Qt\4.2.2\bin;%PATH%
set QMAKESPEC=win32-icc

if not "%1"=="vsvars" goto END
call "E:\Program Files\Intel\Compiler\C++\9.1\IA32\Bin\ICLVars.bat"
:END

if not "%1"=="vsstart" goto ENDSTARTVS
call "E:\Program Files\Intel\Compiler\C++\9.1\IA32\Bin\ICLVars.bat"
devenv /useenv
:ENDSTARTVS


下面这份修正补丁来自日本一个专门说明这一问题的博客网页: http://ashula.info/mt/d061216_qt4_msvc8_sp1_problem.html


下载: qt4-multi-map-hash-msvc8-sp1.diff
diff -Naur tools_orig/qhash.h tools/qhash.h
--- tools_orig/qhash.h 2006-12-17 03:53:53.406250000 +0900
+++ tools/qhash.h 2006-12-17 05:16:20.921875000 +0900
@@ -848,8 +848,11 @@
QMultiHash() {}
QMultiHash(const QHash &other) : QHash(other) {}

- inline typename QHash::iterator replace(const Key &key, const T &value);
- inline typename QHash::iterator insert(const Key &key, const T &value);
+ // fix for vs2005 SP1
+ // cf. http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=987536&SiteID=1
+ typedef QHash::iterator QHashItr;
+ inline typename QHashItr replace(const Key &key, const T &value);
+ inline typename QHashItr insert(const Key &key, const T &value);

inline QMultiHash &operator+=(const QMultiHash &other)
{ unite(other); return *this; }
@@ -862,11 +865,11 @@
};

template
-Q_INLINE_TEMPLATE Q_TYPENAME QHash::iterator QMultiHash::replace(const Key &akey, const T &avalue)
+Q_INLINE_TEMPLATE Q_TYPENAME QMultiHash::QHashItr QMultiHash::replace(const Key &akey, const T &avalue)
{ return QHash::insert(akey, avalue); }

template
-Q_INLINE_TEMPLATE Q_TYPENAME QHash::iterator QMultiHash::insert(const Key &akey, const T &avalue)
+Q_INLINE_TEMPLATE Q_TYPENAME QMultiHash::QHashItr QMultiHash::insert(const Key &akey, const T &avalue)
{ return QHash::insertMulti(akey, avalue); }


diff -Naur tools_orig/qmap.h tools/qmap.h
--- tools_orig/qmap.h 2006-12-17 03:53:03.437500000 +0900
+++ tools/qmap.h 2006-12-17 05:16:29.984375000 +0900
@@ -890,8 +890,11 @@
QMultiMap() {}
QMultiMap(const QMap &other) : QMap(other) {}

- inline typename QMap::iterator replace(const Key &key, const T &value);
- inline typename QMap::iterator insert(const Key &key, const T &value);
+ // fix for vs2005 SP1
+ // cf. http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=987536&SiteID=1
+ typedef QMap::iterator QMapItr;
+ inline typename QMapItr replace( const Key &key, const T &value );
+ inline typename QMapItr insert( const Key &key, const T &value );

inline QMultiMap &operator+=(const QMultiMap &other)
{ unite(other); return *this; }
@@ -904,11 +907,11 @@
};

template
-Q_INLINE_TEMPLATE Q_TYPENAME QMap::iterator QMultiMap::replace(const Key &akey, const T &avalue)
+Q_INLINE_TEMPLATE Q_TYPENAME QMultiMap::QMapItr QMultiMap::replace(const Key &akey, const T &avalue)
{ return QMap::insert(akey, avalue); }

template
-Q_INLINE_TEMPLATE Q_TYPENAME QMap::iterator QMultiMap::insert(const Key &akey, const T &avalue)
+Q_INLINE_TEMPLATE Q_TYPENAME QMultiMap::QMapItr QMultiMap::insert(const Key &akey, const T &avalue)
{ return QMap::insertMulti(akey, avalue); }

解决Visual Studio 2005 SP1编译QT代码的BUG

解决Visual Studio 2005 SP1编译QT代码的BUG

产品:QT 4.1.4 & QT 4.2.2
平台:Visual Studio 2005 Service Pack 1 (SP1)
错误:error C2244: unable to match function definition to an existing declaration

现象:
在qmap.h头文件中出现:error C2244: 'QMultiMap::insert' : unable to match function definition to an existing declaration
(据说在qhash.h中也会出现)

参考URL:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=214100

原因:编译器升级后,分不清嵌套类型的名字是指哪一个,哪怕它们很明显地是同一个类型。例如,如下代码就会引发此错误:

template
class Foo
{
public:
typedef int NestedType;
};

template
class Bar : public Foo
{
public:
typename Foo::NestedType test();
};

template
typename Foo::NestedType Bar::test() // this is where the error lies
{
return 0;
}

微软的解释:
The problem is due to the use of Foo and Bar on the line above; the compiler cannot figure out the relationship between the two template parameters even though they are clearly the same. The workaround is to add a typedef for the NestedType of Bar and use that in the implementation of test

解决方法:
在子类型中再次定义嵌套类型的名字

例如,对于QMultiMap,就是这样子

template
class QMultiMap : public QMap
{
public:
/////////////////////////////////////////////////////////////////////////
//Modified by Kenny
//Purpose: fixed the compiling-time error of Visual Studio 2005 SP1
//Ref:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=214100
typedef typename QMap::iterator QMapIteratorType;
//End of modification
/////////////////////////////////////////////////////////////////////////
QMultiMap() {}
QMultiMap(const QMap &other) : QMap(other) {}

inline QMapIteratorType replace(const Key &key, const T &value);
inline QMapIteratorType insert(const Key &key, const T &value);

inline QMultiMap &operator+=(const QMultiMap &other)
{ unite(other); return *this; }
inline QMultiMap operator+(const QMultiMap &other) const
{ QMultiMap result = *this; result += other; return result; }

private:
T &operator[](const Key &key);
const T operator[](const Key &key) const;
};

/////////////////////////////////////////////////////////////////////////////
//Modified by Kenny
//Purpose: fixed the compiling-time error of Visual Studio 2005 SP1
//Ref:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=214100
template
Q_INLINE_TEMPLATE Q_TYPENAME QMultiMap::QMapIteratorType QMultiMap::replace(const Key &akey, const T &avalue)
{ return QMap::insert(akey, avalue); }

template
Q_INLINE_TEMPLATE Q_TYPENAME QMultiMap::QMapIteratorType QMultiMap::insert(const Key &akey, const T &avalue)
{ return QMap::insertMulti(akey, avalue); }

//The old code is commented out as the following lines

//template
//Q_INLINE_TEMPLATE Q_TYPENAME QMap::iterator QMultiMap::replace(const Key &akey, const T &avalue)
//{ return QMap::insert(akey, avalue); }
//
//template
//Q_INLINE_TEMPLATE Q_TYPENAME QMap::iterator QMultiMap::insert(const Key &akey, const T &avalue)
//{ return QMap::insertMulti(akey, avalue); }

//End of modification
/////////////////////////////////////////////////////////////////////////////

building qt4 with vc2005

Introduction

In this article I'll try to give step by step explanation of how to build QT4 using latest Microsoft Visual C++ 2005 (8.0) compiler. These instructions are based on article Compiling Qt4 on Windows from PsiWiki.

Prerequisites

  1. You need to have Microsoft Visual C++ 2005 installed. You can use VC++ compiler from every edition of Visual Studio, even from Express Edition (free).

  2. Download Qt/Windows Open Source Edition.

  3. Download unofficial patch for QT4 from qt-win project.

/!\ IMPORTANT Select appropriate version of qt-win patch for version of QT you've downloaded.

Examples:

  • QT 4.1.0 -> patch for QT 4.1.0

  • QT 4.1.1 -> patch for QT 4.1.1

  • etc.

Mixing those versions can cause undefined behaviour and compilation likely will fail. I did so, and had to fight with many problems. Here you can find my story about it the Qt Centre.

Notes about my environment configuration

  1. I unpacked QT 4.1.1 sources into the following directory:
    D:\lib\qt\4.1.1 
    So, I set QTDIR environment variable as follows:
    QTDIR=D:\lib\qt\4.1.1

    During further explantion I'll use %QTDIR% refering to the location above.

  2. I use Visual C++ 2005 Professional Edition installed in the following location:
    C:\Program Files\Microsoft Visual Studio 8 
  3. Visual C++ configuration script can be found here:
    C:\Program Files\Microsoft Visual Studio 8\VC\bin\vcvars32.bat 

First: Apply qt-win patch

  1. You should have downloaded qt-win patch. If you have QT 4.1.1 you need this fileacs4qt411p1.zip

  2. Unpack the patch from ZIP to your %QTDIR%.

  3. Open Command Line window and move to the %QTDIR%.

  4. Run installpatch41.bat script

    D:\lib\qt\4.1.1>installpatch41.bat
    You will see many messages like:
    patching file qconfigure.bat
    patching file examples/threads/waitconditions/waitconditions.pro
    patching file misc/bcc32pch/Makefile.win32-borland
    ...
    Check the output for possible errors. If there are no errors, patch is applied correctly.

Second: Compile QT4 using VC++ 2005

After qt-win patch is applied you can start building QT4 with Visual C++ 2005 compiler.

  1. Run vsvars32.bat script to configure environment for VC++ 2005 compiler

    D:\lib\qt\4.1.1>"C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"
    Setting environment for using Microsoft Visual Studio 2005 x86 tools.
    Note the quotation marks around the path to script. They're required because path includes spaces.
  2. Run qconfigure.bat script specifying VC++ 2005 compiler Please, write the name of the script correctly. It's qconfigure.bat but not configure.exe which also exists in the %QTDIR%.

    D:\lib\qt\4.1.1>qconfigure.bat msvc2005

    This is the Qt/Windows Open Source Edition.

    You are licensed to use this software under the terms of
    the GNU General Public License.

    Type '?' to view the GNU General Public License.
    Type 'y' to accept this license offer.
    Type 'n' to decline this license offer.

    Do you accept the terms of the license?

    Now, accept the license according to instructions above giving y and confirming with enter key. After this step, nmake will run automatically and VC++ 2005 will start building QT4. This process can take a few minutes, depending on speed of your hardware.

  3. After compilation is finished without any errors, you should have following binaries ready to use or run:
    • QT4 Libraries
    • QT4 Tools like designer, assistance and linguist

    • QT4 Demo applications
    • QT4 Examples

    Here is sample screenshot presenting QT Tools compiled with VC++ 2005

Third: Environment Configurtion

  1. Open My Computer -> Properties -> Advanced -> Environment Variables

  2. Add new variable QTDIR as follows

    QTDIR=D:\lib\qt\4.1.1 
  3. To PATH variable add following path:

    %QTDIR%\bin 
  4. Configure Visual C++ 2005 settings. Open Tools -> Options dialog box.

    Add following directories under QTDIR to Include files:

    Add QTDIR\lib directory to Library files:

Notes about Visual C++ 2005 Express Edition

Elmar de Koning wrote to me about his experience in building QT4 with Visual C++ 2005 EE. I'd like to thank Elmar for this note. Here is the essence of Elmar's e-mail:

  • Well I managed to do just that, although several additional steps were needed. In short, vs2005 express comes with no platform SDK, but the "Microsoft Windows Core SDK" part is needed to build qt4. This document describes in short how to get it what to do with it after you downloaded it. I suggest using the "Windows® Server 2003 R2 Platform SDK Web Install" option from here as that allows you to just download the core sdk instead of the whole 400MB package.

/!\ Note: The first link in the above paragraph appears to be dead. You can access that same page using the internet wayback timemachine here. TS

I (TimSutton) would like to make some additional notes here since I built Qt4.3.1 with Visual C++ Express and ran into many hiccups. Firstly dont forget that you always need to run vsvars32.bat in any shell you open otherwise your environment wont be set up properly. Next you really need to take care that your %LIB%,%INCLUDE% and %PATH% environment variables are set properly in

Here is what my %LIB% dir looked like after installing the SDK and setting everything up:

C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib;
C:\Program Files\Microsoft Visual Studio 8\VC\LIB;
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;

Here is my %PATH%;

C:\Program Files\CMake 2.4\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;
"c:\Program Files\Microsoft Visual Studio 8\Common7\Tools";
c:\Qt\4.3.1\bin;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin\.;
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin\WinNT\;
C:\Program Files\svn\bin;C:\Program Files\Microsoft Visual Studio 8\VC\bin;
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE

The End

If you have any problems or questions, please contact me on mateusz@loskot.net . Also, I'd like to hear if someone managed to build QT4 using Visual C++ Express Edition without problems. So, please send me a note about that or put some info on this wikipage.

I'd like to thank authors of PsiWiki for their article about building QT4 with Visual C++.

Next Step

I'm going to publish step by step HOWTO about building Quantum GIS using Visual C++ 2005 compiler. I'll provide you with complete and working solution and project files as well.

References

Qt4 on Free IDE Visual Studio Express 2005 C++ with Qt code completion

Building QT 4 with Visual C++ 2005 (2007-09-24 20:37:25由TimSutton编辑)