Create Your First Qt Program on Ubuntu Linux

The Qt (SDK) software development kit is a portable cross platform application user interface framework which works on the Windows, Linux and Mac OS X operating systems. Qt SDK helps you create graphical user interfaces (GUI's) for your applications that will run on Windows, Linux and Mac OS X.

For this article we are going to use the following simple steps to construct our first Qt HelloWorld program.

  1. Create the directory QtHelloWorld in order to hold your Qt program
  2. Change into your directory QtHelloWorld
  3. Create the Qt source file main.cpp within the QtHelloWorld directory
  4. Compile and run your QtHelloWorld program

Note: This document presumes you have the Qt SDK successfully installed on your operating system. If you do not have the Qt SDK installed on your system please see the following document for more information How to Install Qt SDK on Ubuntu Linux. This document also presumes you have basic knowledge of the C++ programming language. Essentially, the Qt SDK is programmed in C++ and relies heavily on C++ design and functions. Note: There are some compilation changes with Qt SDK 4.8 and Qt SDK 5.0, hopefully this article will resole the compilation issues between the two different Qt SDK versions.

Steps

Qt 4.8 SDK Compilation Instructions

  1. For this exercise we are going to open up a terminal on Ubuntu Linux and issue the following command which will create the main directory for a Qt program.
    • Type/Copy/Paste: mkdir QtHelloWorld
  2. Change into your QtHelloWorld directory by issuing the following command
    • Type/Copy/Paste: cd QtHelloWorld
    • This is very important to make sure you are in the correct directory when creating your Qt program.
  3. While we are in the QtHelloWorld directory, we are going to create our Qt Program source code file
    • Type/Copy/Paste: nano main.cpp
    • or
    • Type/Copy/Paste: gedit main.cpp
    • This command will create the main.cpp file for the Qt program
  4. Now add the following lines in the code box below to your main.cpp source code file.
  • Type/Copy/Paste:

  • Save the file as main.cpp and exit
    • Make sure you are in the QtHelloWorld directory before you enter the following commands below to build and compile the file.
  • Type/Copy/Paste: qmake -project
    • This will create the Qt project file
  • Type/Copy/Paste: qmake
    • This will create the Qt make file
  • Type/Copy/Paste: make
    • This will compile the Qt make file on your system into an executable program. At this point, providing that there are no errors the file should compile into an executable program.
  • Finally execute your program by running the Qt executable. Use the command ./ to run your executable file or type the name of the executable program on the terminal line.
  • Type/Copy/Paste: ./QtHelloWorld

Qt 5.0 SDK Compilation Instructions:

  1. For this exercise we are going to open up a terminal on Ubuntu Linux and issue the following command which will create the main directory for a Qt program.
    • Type/Copy/Paste: mkdir QtHelloWorld
  2. Change into your QtHelloWorld directory by issuing the following command
    • Type/Copy/Paste: cd QtHelloWorld
    • This is very important to make sure you are in the correct directory when creating your Qt program.
  3. While we are in the QtHelloWorld directory, we are going to create our Qt Program source code file
    • Type/Copy/Paste: nano main.cpp
    • or
    • Type/Copy/Paste: gedit main.cpp
    • This command will create the main.cpp file for the Qt program
  4. Now add the following lines in the code box below to your main.cpp source code file.
  • Type/Copy/Paste:

  • Save the file as main.cpp and exit
    • Make sure you are in the QtHelloWorld directory before you enter the following commands below to build and compile the file.
  • Type/Copy/Paste: qmake -project
  • This will generate the Qt project file
  • However, in Qt 5.0 SDK you must use a text editor and add the following to your generated *.pro file, use a text editor such as nano or gedit and issue the following commands:
  • Type/Copy/Paste: nano QtHelloWorld.pro
  • Your generated QtHelloWorld.pro should look similar to this:

  • Edit your generated QtHelloWorld.pro file to look like this:

  • Once you add the following lines to the QtHelloWorld.pro file under the TARGET keyword, then run qmake
  • Type/Copy/Paste: QT += core gui
  • Type/Copy/Paste: QT += widgets
    • Save the file and exit
  • Type/Copy/Paste: qmake
    • This will create the Qt make file
  • Type/Copy/Paste: make
    • This will compile the Qt make file on your system into an executable program. At this point, providing that there are no errors the file should compile into an executable program.
  • Finally execute your program by running the Qt executable. Use the command ./ to run your executable file or type the name of the executable program on the terminal line.
  • Type/Copy/Paste: ./QtHelloWorld

Related Articles