Tag Archives: Qt

Compiling Qt with Visual Studio 2010

Qt is an object oriented C++ cross-platform GUI toolkit which can be used to build applications that run natively on several platforms including Windows, Linux, Mac OS, and embedded Linux without source code changes. Qt is available under GPL v3, LGPL v2 and a commercial license.

This tutorial is intended for Windows users preferring to stick to Visual Studio 2010 as a primary development environment instead of Qt Creator. It provides step-by-step instructions for compiling Qt from source using the open-source distribution.

Prerequisites

    • Visual Studio 2010 Professional with SP1.
    • Windows 7 Professional.
    • Qt 4.8.0 open-source distribution (or a recent Qt 4.7.x version).
    • Qt Visual Studio Add-in.
    • ActivePerl.

Setup Visual Studio 2010

1. Download and install Microsoft Visual Studio 2010 Service Pack 1.

2. For x64 users, it is recommended to download and install this patch for Visual Studio 2010, if it is not already up-to-date.

3. Download and install the Qt Visual Studio Add-In. After installation, a new menu called Qt is added to Visual Studio 2010.

Download and install ActivePerl

ActiveState provides a Perl distribution for Windows platforms. Download and install the latest x86 version from here. Stick to x86 unless you have any special reason for using 64-bit applications.

During installation, make sure that Add Perl to the PATH environment variable and Create Perl file extension association checkboxes are selected.

Download and compile Qt

1. Download the Qt source code as a .zip file. The Qt-everywhere package version 4.8.0 is located here.

2. Unzip the previous file into a location without spaces and special characters. I will use C:\Qt\4.8.0.

3. Open a Visual Studio Command Prompt: All programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt (2010). Stick to x86 unless you have any special reason for using 64-bit applications.

4. Get Qt configured:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC> cd C:\Qt\4.8.0
C:\Qt\4.8.0> configure -debug-and-release -opensource -shared -platform win32-msvc2010

Type y and press Enter to accept the terms of the license agreement.

You can of course specify additional options to add or remove some features depending upon your requirements. Type configure -help to get more details.

5. Compile Qt:

C:\Qt\4.8.0> nmake

The compilation takes a long time. Be patient.

Create a new Qt project

1. Open Visual Studio 2010. Go to Qt –> Qt Options and add the folder C:\Qt\4.8.0.

 

 

2. Create a new Qt project (File -> New -> Project… then select Qt4 Projects) or open an existing .pro file.

Debug and Release versions of the Qt shared libraries (QtCore4.dll, QtGui4.dll, QtCore4d.dll, QtGui4d.dll …) are located in C:\Qt\4.8.0\bin folder.  You will need to distribute the relevant ones used by the application when deploying on other systems.

PostgreSQL driver for Windows and Linux

Due to license incompatibilities with the GPL, the QPSQL plugin needed to connect to a PostgreSQL database is not provided with open source versions of Qt.

This tutorial is about setting up a compete Qt development environment with PostgreSQL support for Windows 7 and Ubuntu 11.10 (Oneiric Ocelot). It also describes all steps necessary to build the QPSQL plugin for Windows using Qt SDK with MinGW.

Windows 7

1. Download Qt SDK for Windows at http://qt.nokia.com/downloads and install it.

The current version is Qt_SDK_Win_offline_v1_1_4_en.exe and its default installation folder is C:\QtSDK.

2. Download the source code associated to your Qt SDK version at http://qt.nokia.com/downloads.

Qt SDK version 1.1.4 uses qt-everywhere-opensource-src-4.7.4.zip. Unzip its content into C:\Qt\4.7.4 folder for instance.

3. For Windows 7 x64 (64-bit) you need to download a 32-bit version since it is not possible, at the moment, to compile the 64-bit version of PostgreSQL plugin.

Download PostgreSQL at http://www.postgresql.org and install it.

The current version is postgresql-9.1.1-1-windows.exe. The default PostgreSQL installation folder is C:\Program Files (x86)\PostgreSQL\9.1 (short path is C:\Progra~2\PostgreSQL\9.1) for Windows 7 x64 (64-bit) and C:\Program Files\PostgreSQL\9.1 (short path is C:\Progra~1\PostgreSQL\9.1) for Windows 7 x86 (32-bit).

4. Open Start > All Programs > Qt SDK > Desktop > Qt 4.7.4 for Desktop (MinGW).

5. Run these commands to build the QPSQL plugin (change according to your environment and make sure you use short path type):

set QTSDK_SQLDRIVERS_PATH=C:\QtSDK\Desktop\Qt\4.7.4\mingw\plugins\sqldrivers
set QT_SRC_PATH=C:\Qt\4.7.4
set POSTGRES32=C:\Progra~2\PostgreSQL\9.1

cd %QT_SRC_PATH%\src\plugins\sqldrivers\psql

qmake "INCLUDEPATH+=%POSTGRES32%\include" "LIBS+=%POSTGRES32%\lib\libpq.lib" psql.pro

mingw32-make debug
mingw32-make release

copy release\libqsqlpsql4.a %QTSDK_SQLDRIVERS_PATH%
copy release\qsqlpsql4.dll %QTSDK_SQLDRIVERS_PATH%
copy debug\libqsqlpsqld4.a %QTSDK_SQLDRIVERS_PATH%
copy debug\qsqlpsqld4.dll %QTSDK_SQLDRIVERS_PATH%

These commands build a Release (qsqlpsql4.dll, libqsqlpsql4.a) and a Debug (qsqlpsqld4.dll, libqsqlpsqld4.a) versions of the QPSQL plugin. These 4 files are then copied into Qt SDK’s SQL drivers folder.

7. The QPSQL plugin uses additional DLLs located in bin folder under the PostgreSQL installation folder. You have to copy these DDLs to the same directory as your application binary.

iconv.dll
libeay32.dll
libiconv-2.dll
libintl-8.dll
libpq.dll
libxml2.dll
libxslt.dll
ssleay32.dll
zlib1.dll

Ubuntu 11.10 (Oneiric Ocelot)

1. Install Qt Creator.

$ sudo apt-get install qtcreator

2. Install PostgreSQL.

$ sudo apt-get install postgresql

3. Optionally, you can install pgAdmin III, a handy GUI for PostgreSQL administration.

$ sudo apt-get install pgadmin3

4. Install PostgreSQL plugin for Qt 4.

$ sudo apt-get install libqt4-sql-psql

Testing

Follow the following steps to check that everything is functional.

1. Create an empty Qt console application project.

2. Make sure you have QtSql Module in your project configuration file (.pro file) by adding this line:

QT += sql

3. Update the source code as follows:

#include <QtSql>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    qDebug() << "Has a valid QPSQL driver: " << (db.isValid() ? "YES" : "NO");

    return a.exec();
}

4. Compile and run the application (Under Windows, do not forget to copy the required DLLs from the bin folder located in the PostgreSQL installation folder). You should see:

Has a valid QPSQL driver: YES