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 > main.pro &

This example project file, main.pro, 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 main.pro text file and save:

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

3. Create the example application

First chttp://www.informit.com/articles/article.aspx?p=412354&seqNum=4reate 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 );
    x.show();
    return my->exec();
}

4. Use qmake to create the example project

$ qmake -o Makefile main.pro

5. Build and run the project

$ make
$ ./main

Giving you your first Qt dialog: