Qt Programming – Part 1
1. Installing Qt & Qt Creator
As with most things, there is more than one way to install Qt and Qt Creator. The easiest method to install Qt is to download the .bin file which contains the SDK (Software Development Kit) and Qt Creator. This article will cover installing the .bin file that contains the SDK and IDE (Integrated Development Environment).
You can download the latest SDK and IDE from http://www.qtsoftware.com/downloads. Select the complete development environment. You can choose from either a 32 or 64 bit package. Once you have downloaded the .bin file you need to modify the files attributes and make it executable. You can make the file executable using the following command: cmod u+x <filename> (chmod u+x qt-sdk-linux-x86-opensource-2009.01.bin). Once you have made the file executable type ./qt-sdk-linux-x86-opensource-2009.01.bin to start the installation wizard, as shown in Figure1.
Next, step through the installation wizard. You will be asked where you would like to install the software. The default location is /opt. I would recommend installing the software there since /opt is for recommended location for installing extra software. If you do not have write access to /opt you can either ask you administrator to install the software or you can install the software in your home directory. Before installing the software make sure you have at least 890Mb of free disk space.
Next you will be asked which components you would like to install. Just click next at this step as the SDK and IDE are selected by default. Click next to complete the installation.
When the installation has finished you will be asked if you would like to run Qt Creator now. Select yes and we will look at how to use Qt Creator.
2. Qt Creator
2.1 About Qt Creator
Before Nokia bought Qt from Trolltech last year there was no IDE designed specially for Qt. Developers could use Qt plugins for popular IDEs such as KDevelop and Eclipse but configuring these IDEs was not always straightforward. This was one of the main stumbling blocks encountered by people wanting to learn how to develop software using Qt. Nokia noticed this and set out to make Qt development easier and create an IDE designed specially for Qt which could run on multiple operating systems. Qt Creator encompasses all the tools needed for Qt application development. From a text editor with syntax highlighting to API documentation.
2.2 Creating a Qt Project
The purpose of creating a project is to familiarise yourself with the IDE. Qt Creator is intuitive and simple to use.
When you first start Qt Creator you are presented with the welcome page. Here you can find your recent projects and sessions. Since this our first time of using Qt Creator there should be no recent projects or sessions.
To create a new project click File > New. You will be prompted with the New dialog as shown in Figure 3. Select Qt4 GUI Application and click ok.
Next, you will be asked to give your project a name and to select a destination for the project. Give your project a suitable name and set the destination and click next. You will be asked which Qt modules to link into your project. For this example the default modules will suffice so just click next. The project wizard will now ask for information regarding your main class. Give your main class a suitable name and change the base class from QMainWindow to QDialog and click next. When you click finish the wizard will close and four files will be displayed in the project pane. The files are your Qt class, main.cpp which contains the main function and a .ui file that contains user interface information.
Double click the .ui file to load the GUI designer as shown in Figure 4.
The left hand pane is a list of all the available Qt GUI widgets that you can use in your applications. On the right hand side of Figure 4 you can see the properties panel which can be used to set the currently selected components properties, for example, size, position, text, name, etc.
If you press the build button Qt Creator will build the application and an empty dialog window will be displayed, which is quite boring. To change the dialog window’s title go to the properties panel and select Window Title. Change the caption to anything you want then run the application again. You will see your dialog’s title is the caption you entered. Next drag a push button on to the dialog window. The default button caption is “Push Button”, which is really meaning full. Select the button by clicking it. The currently selected item will have 6 blue squares around its boarder which can be used to resize the button.
In the property panel scroll down to the text property and change the value to “Click Me”. The buttons caption will have changed to the text you just entered. If you run the application and click the button you will see that there is not functionality behind the button. The next step is to add some functionality.
In the GUI designer right click your button and select “Go to slots…”. A dialog will be displayed list all of the events supported by the button. For the time being select clicked() and click ok. events/slots and signals will be discussed in a lot more detail in a later tutorial. The main aim here is to show how to create projects. Once you click OK the code window will be displayed. Here we can add the functionality that will be called whenever the button is pressed.
Now Enter the following code into on_pushButton_clicked()
QMessageBox::information(this, “Hello”, “Welcome to Qt”);
and also type:
below the other #include directives. Now when you run your application and click the button a message box will be displayed displaying “Welcome to Qt”.
In part one of this Qt programming guide I explained how to install Qt SDK and IDE and create a project. Part 2 will focus on the file IO and container classes available in Qt. To demonstrate how to use the file IO and container classes we will create a class which will read songs from an iPod iTunesDB file.