Mar 03

At last I am able to beat my procrastination and decided to start something like sharing knowledge to others through this blog. Why not I could start with Qt? I too am new to this cute technology, so I thought it would be great idea to share what I do to get mastered in Qt with fellow Qt learners ;-)

Let’s get started by setting up the development environment. It is very simple,

1 ) Download latest Qt SDK from trolltech site (Now QT Software after Nokia Acquire). Here is direct link for Windows SDK,

http://www.qtsoftware.com/downloads/sdk-windows-cpp

Install the SDK to something like C:\Qt\ or just leave the default install path as it is.

2) [Optional] This step is required if you want to use Visual C++ express edition as IDE for building Qt apps. I would recommend to use VC++ editor, I found it very powerful and lot easier to develop. Otherwise QT Creator will just do the job!. Get latest VC++ express version from here,

http://www.microsoft.com/express/vc/

This download includes Platform SDK aswell if it is VC++ 2008 or greater

Follow the installation wizard and finish installation

3) set environment variable PATH to qt’s bin directory. For example mine is

PATH=”\Qt\2009.02\qt\bin;\Qt\2009.02\bin”

4)Now configure qt for the target development platform. You can do this by launching VC++ command prompt and running thiscommand,

configure -platform win32-msvc2008

this step will vary if you use other IDE other than Visual Studio.

5)Here comes the final step, change to \qt\2009.02\qt directory and run nmake and grab a cup of coffee as this is going to take a while to finish. My machine with 2 processors (2.6GHz) took 2 hours :)

Bingo! your development environment is now set, you can right away start building Qt apps at this point (Of course if you know how to :P )

Hello World!

Hey whats a getting started guide without a “Hello World!” app! Here is one written in Qt,

#include <QtGui>

int main(int argc, char *argv[])
{
 QApplication app(argc, argv);

 QLabel label(“Hello World!”);
 label.show();

 return app.exec();
}

create a file called main.cpp and copy/paste above code. Create platform independent project file by running “qmake -project” in the directory where you placed main.cpp. Generate VS project file by running “qmake -tp vc” which will create *.vcproj file which you can use further to build and work on this app from VS IDE.
Build the application in Visual Studio, the resulting program will look something like this on Win XP,

Hello World in Qt

Hello World in Qt

Let’s get started!

Now we shall get started and write something simple and cool. To make it interesting I will do a “screen shooter” application which will basically simulate a shooting effect on mouse click into your desktop screen :D

The idea is grab an image of current desktop in full screen, listen for mouse press event, draw a burn image where the mouse was pressed, play an explode sound to get a feel we shot ;)

Here we go,

1) Get a screenshot of the entire desktop, this is how we can do in Qt,
QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); 

QPixmap is a Qt class which makes it easy to deal with image data. Find more info here, http://doc.trolltech.com/4.5/qpixmap.html

2) Listen for mouse press event and store the coordinates when a click is made,

void ScreenShooter::mousePressEvent(QMouseEvent *event) //QWidget’s virtual method
{
 currentClickedPos = event->pos();

}

3)simulating the shot!, draw the things on the screen using QPainter like this,

QPainter painter(this);
 
painter.drawPixmap(0, 0, QPixmap::grabWindow(QApplication::desktop()->winId()));
painter.drawImage(currentClickedPos – QPoint(20, 20), QImage(“:/explode.png”));  //explode.png is 40×40, so the magic offset point (20, 20) :D

4) You can play sound in Qt with QSound as simple as like this,

QSound snd(“resources/Explosion.wav”);
snd.play();

Seems like QSound can play only wav files as of Qt 4.5

5) Putting the pieces together,

void ScreenShooter::paintEvent(QPaintEvent* event)
{
 QPainter painter(this);
 
 painter.drawPixmap(0, 0, QPixmap::grabWindow(QApplication::desktop()->winId()));
 painter.drawImage(currentClickedPos – QPoint(20, 20), QImage(“resources/explode.png”));
}

void ScreenShooter::mousePressEvent(QMouseEvent *event)
{
 currentClickedPos = event->pos();

 update();

 QSound snd(“resources/Explosion.wav”);
 snd.play();
}

 Screen shooter running on my machine…

screen_shooter2

The explode watermark image doesn’t look great, could be made more natural :P

Thats all folks for now!

Keep checking for more advanced experiments in my next post.

Tagged with:
preload preload preload