Article
One Community, One World
Windows Commnication Foundation Addressing
by [lnkAuthor]  on 
  PageViews   |    Add to Fav   |     Send to Friend   |    Download Code

Prerequisites:
This article assumes that you know what C++ is and what a compiler is. Anyone who has at least taken one class or read a tutorial about C++ will have this level.  

Terminology: 

Makefile:

It's a file that contains a set of rules used in compiling your files and building your project. If you're a Microsoft Compiler user you can  think of it like the vcproj file. On UNIX and UNIX-like environments it's the only standard way to build a project.  

Introduction 
Qt (pronounced cutie) is a great cross-platform C++ library. Cross-platform means that the code you write on one platform (ex. Windows) can get compiled easily on another platform (ex. Linux). In this article I want to introduce you to this great and rich library.

Before libraries like Qt, a C++ developer would have to rewrite parts of his code to make it work on another platform. For example MFC used to be the dominant GUI library on the Windows platform, and on UNIX it was Motif. So he would have to rewrite the whole GUI on each platform. And what makes it worse is that these 2 libraries where hard to learn. Qt is very easy to learn, I have been using it for more than 2 years and I can't be happier than this. 

Although Qt is developed by a commercial company its also open source. This means that you get the whole source code of the library. You can learn from it, add on it. There is an open source project called KDE which is built on top of it, and a lot more.  

Qt dual licensing
Qt is developed by a company called TrollTech. And one of the great things about Qt is the dual licensing. This means that you can use Qt under one of two conditions, either commercial or open source.  Being commercial, you have to pay TrollTech the fees that are announced on their website. And if you are open source they won't ask you about any money.

Getting Qt

Downloading and building Qt for windows users  
You can download it from here   

If you are a Windows user you have one of two options:

  1. Build the library from source code.
  2. Download the precompiled package  

I strongly advise that you download the precompiled package at least to save you time. The precompiled package can download the mingw compiler for you, which is the port of the open source GCC compiler on Windows.

The reason why you might want to compile the package from source code is if you are using a Microsoft Compiler. It's not supported in the open source edition of the library. Here is a link about building the library using Microsoft compilers.

http://qtnode.net/wiki/Qt4_with_Visual_Studio

Downloading and building Qt for X11 users (UNIX and UNIX-like environments)
You can download it from here

For X11 users it's pretty easy to compile the package, but there are no precompiled packages on Trolltech's website. Of course you might find a package for your operating system, but you'll have to find it your self. It's worth mentioning that Qt3 comes bundled with almost every Linux distribution, if not all. But we aim here to work with Version 4 and it's totally not compatible with Version 3.  

Building steps:

1. Untar the archive            
    [user@machine] tar zxvf  qt-x11-opensource-src-4.2.2.tar.gz  

2. Get inside the directory and type configure           
    [user@machine] cd qt-x11-opensource-src-4.2.2         
    [user@machine] ./configure           

    It will start to configure the package. This step will take some time.  

3. Now start the making           
    [user@machine] make           

    This step will take a lot of time.  

4.And as root install           
    [user@machine] make install           

    After this step it should be installed in /usr/local/Trolltech           
    You should add the following environment variables at the end of your profile           

    Bash shell: File name .bash_profile inside your home folder           
    export QTDIR=/usr/local/Trolltech/Qt-4.2.2         
    export PATH=$QTDIR/bin:$PATH         
    export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH           

    C-shell: File name .cshrc inside your home folder           
    setenv QTDIR /usr/local/Trolltech/Qt-4.2.2         
    setenv PATH $QTDIR/bin:$PATH         
    setenv LD_LIBRARY_PATH $QTDIR/lib:$LD_LIBRARY_PATH  

Let's get to work 
Now that you have everything installed, let's start our first Qt program. Every good startup tutorial starts with a "Hello, World!" example. So, I can't find any reason why not follow the traditions.

#include <QApplication>
#include <QMessageBox>  

int main(int argc, char *argv[ ]) 
    {     
        // For a gui program we can only have one QApplication object    
        // this object will handle lots of stuff  like main event loop, application initialization    
        // and finalization...etc     
        QApplication app(argc, argv);     
        // Displaying our Hello, world message     
        QMessageBox::information(0, "A Message title" , "Hello, World!");     
    }
  

For people on windows using GCC compiler:
it will be the same like Linux. Now take that file main.cpp and put it in some folder.

Open a command line window and do the following:  
1. qmake -project: This creates the Qt's .pro file. It’s the project format for Qt applications. More on this later.  
2. qmake -app: This will generate the makefiles needed to build your program.  
3. make: Done now your program is built  

For people they are using Microsoft Visual C++:  
1. Open start -> All programs -> Microsoft Visual Studio 2005 -> Visual Studio tools -> Visual Studio 2005 command prompt        
    This will open the command prompt. Go to the folder where main.cpp is. For example if my folder is called "folder" on the c: partition I would this.    

2. cd c:\folde  

3. qmake -project  

4. qmake -app  

5. nmake  

Conclusion
Okay, We are done for now, let's wrap up. All we did till this moment is creating the right environment for building Qt applications. All of you should be sure that you did it correctly, or you won't be able to continue the other tutorials that explain the other parts of the Qt library.

Comments