Getting started with Qt in Ubuntu Linux

A quick recipe on how to get started with Qt in Ubuntu Linux using a simple example program.

1. Obtain QT4

Run these from the command line:

$ sudo apt-get update
$ sudo apt-get install libqt4-dev qt4-qmake cmake r-base-dev libcurl4-gnutls-dev

2. Create and edit the qmake project file

To generate and edit the project file itself use something like:

$ mkdir qt
$ cd qt/
$ cat > &

This example project file,, is essentially a set of declarations required by qmake to enable you to build your application.

Using a text editor of your choice, just copy this into your text file and save:

SOURCES = main.cpp
CONFIG += qt warn_on release
TARGET  = main
QT += gui
QT += widgets
CONFIG += qt

3. Create the example application

First c your sample main.cpp. I often prefer the cat command to create new files:

$ cat > main.cpp &

And using your own Linux-based text editor of choice, create this simple Qt example and save as main.cpp:

#include <QtWidgets>
#include <QtCore>
#include <QLabel>
int main( int argc, char *argv[] )
    QApplication *my=new QApplication( argc, argv );
    QMainWindow x;
    QString str = "Hello";
    QLabel *label = new QLabel( str, 0 );
    x.setCentralWidget( label );;
    return my->exec();

4. Use qmake to create the example project

$ qmake -o Makefile

5. Build and run the project

$ make
$ ./main

Giving you your first Qt dialog:

Leave a Reply