Qt Creator programming environment (V: 9 min)

This section briefly shows you the most essential phases to start working with Qt Creator programming environment. You will learn how to write and execute a simple program in the virtual desktop environment used on the course.

We assume that you have created a connection to the virtual desktop by following the guidance given in the previous section of the course material.

An alternative for working in the virtual desktop is to install Qt Creator on your own computer. Instructions can be found from the end part of this page. This is a considerable choice especially in remote mode, but note that in this case you should also install Git on your own computer. Tips for this can also be found from the end part of the page.

General issues on Qt Creator

The programs in Qt Creator are projects, and thus, they always include a file with .pro suffix. This file tells which source code files the program has. It is also the file that you will open, when your aim is to open an existing program in Qt. (As simplest, a program has only one source code file, main.cpp, besides the project file.)

When storing programs into version control and submitting them in Plussa, make sure that there is also the .pro file along.

How to use Qt Creator on virtual desktop

Follow the steps below when you are starting to write a new program with Qt Creator. Note that steps 1 and 3 are initial ones that are needed to be done only once before writing the first program.

  1. If you have earlier attended a course, where you have used Qt Creator, you would be better to remove the old settings. If there have been any maintaining actions on C++ or Qt Creator after your last use of them, it is possible that Qt Creator do not work.

    So, if you are using Qt Creator from the remote desktop for the first time, this step does not concern you. You can move directly to step 2!

    During this step, Qt Creator must not be running. Otherwise the old settings will stay.

    Remove the old settings by removing all files in directory ~/.config/QtProject. Start file management by selecting Accessories -> Files:

    Screenshot on starting file management

    The opening window does not show system files starting with a dot. You can see them by adding a cross at “Show hidden files”:

    Screenshot on making hidden files visible

    After that you can see the content of .config directory and you can remove all the files inside QtProject directory (which is inside the first-mentioned directory).

  2. Start Qt Creator tool from Applications > Programming > Qt Creator:

    Screenshot on starting Qt Creator

    when you can see the starting window of Qt Creator:

    Screenshot on starting window of Qt Creator
  3. From the top bar menu, (depending on which one you can find) choose either

    • Tools -> Options

    • Edit -> Preferences.

    From there you can adjust the settings of Qt Creator. We may return to these setting later as the course advances, but already in this phase you can set the naming conventions of source code files.

    From {} C++, choose tab File Naming. Check that file suffix for Headers is hh and that for Sources is cpp (or cc):

    Screenshot on choosing file suffixes

    Choose OK from bottom right, and you will return to the starting window of Qt Creator and everything should be ready for the first programming project.

  4. You can start a new programming project by clicking mouse at New Project in the starting window of Qt Creator. If you have already started to write a program and you are continuing it after a break, you can choose the desirable project under Open Project or Recent Projects directly with mouse. However, we assume here that you are starting a new project, and thus, you can choose New Project.

    After that you will see a couple of windows, where Qt Creator asks some details concerning the project before letting you to write program code.

  5. The first thing to decide is the type of the project. In the beginning part of the course, all the programs are basic C++ programs without graphical interfaces or other specific features. Therefore, choose first the type of the project as Non-Qt Project and the subtype as Plain C++ Application:

    Screenshot on choosing the type of the project

    After that select Choose... from the bottom right.

  6. Next selection concerns the directory, where Qt Creator will store all the files of the project.

    At Name: write a name describing you project as well as possible. For example weekly_exercise-01-task-03  and  project_work-01 are good names for the project.

    Next you need to decide the directory, where you store the project. For this first task you, you can make the decision by yourself. It can be a good idea to create a directory called prog2 for the course. This can be done in file system management of the virtual desktop or by clicking Browse in Project Location window and then, in the top right corner of the opening window, clicking Create Folder, after navigating in the desirable directory.

    After the selections described above, Qt shows the name selections done:

    Screenshot on successful end result

    Choose now Next> from bottom right.

  7. In window Build System select qmake. Everything in window Kits should be OK, and it is sufficient to select Next> from bottom right.

  8. The last step before actual programming concerns Project Management window. Here you can choose a version control system for your project. Although we will use Git version control on the course, we will not yet do so for this first project. Therefore, at this time choose <None> at Add to version control. Finally, press Finish.

Running a program in Qt

By default Qt may have such a setting on that a program prints its output in Application Out, and the program cannot read user input.

Therefore it is better to change options that in different environments/versions can be found from different places:

  • From the top menu, (depending on which one you can find) choose either

    • Tools -> Options or

    • Edit -> Preferences.

  • After that you should find the point Build & Run. From General tab, at Default for "Running in terminal", choose Deduced From Project.

Screenshot on options to enable user input

When now running a program, depending on your version or environment, there should appear a separate window, or you should see a Terminal part at the bottom of Qt window. Both of them enable you to run a program: you can see program output and give input.

In addition, at least in remote desktop you choose the separate window for running programs:

  • Edit -> Preferences

where you should find Terminal. You can either check or uncheck the point Use internal terminal, depending on if you want to have a separate window or not

Screenshot on options to get separate execution window in use

Video: Basic use of Qt Creator (9 min)

Video shows how to open and run an existing program and how to create a new program.

Link to video

On installing Qt Creator to your own computer

It is possible to install Qt Creator to your computer, when you can write programs without internet connection. Installation process depends on the operating system of your computer.

If you are using Linux or Mac operating system, you must find out installation instructions by yourself. In both of these operating systems, you can check from Qt’s installing page if there is a suitable version for your operating system.

Mac users can find more information from here.

Caution

Note that probably this is not exactly the same version of Qt Creator and C++ compiler as has been installed in remote desktop. So, there can be differences.

Especially, it can happen so that a program works on your own computer, but it does not pass automated tests. In such cases, there is an error in your program, e.g. an uninitialized variable or over-indexing of a vector. (You can notice over-indexing by using at function instead of brackets.)

It is a good idea to test the assignments on the remote desktop before submitting them, since they will be tested in the same Linux environment by the course personnel. This is important, because it is possible that a program does not work in the same/correct way in different operating systems or in different versions of them.

Instructions to install Qt Creator in Windows

If you are installing Qt Creator in your own computer, especially in Windows you should install also Git Bash. This enables you to use command line in the same way as in Linux remote desktop. (Windows has a command line of its own, but it does not support Git commands, and thus, it is better to install Git Bash.)

The instructions below are valid for Windows (x86 Windows 11), but you can check from the link given in the first step, if you can start, even if you have another operating system.

To install Qt Creator to your own computer follow the steps below. To get Qt Creator to be installed, remember to check this choice.

Installation instructions:

  1. Download the Qt online installer from this link.

  2. Run the installer and create/log in to your Qt account.

  3. Select Custom Installation.

  4. Select any Qt version 6.8.3 or newer.

  5. Under your selected Qt version, choose a MinGW version from the dropdown menu.

    Screenshot on component selection

    This is an example of possible selections in the component selection window. Do not panic if Qt Creator is not in the exact same location as shown in the picture, as the layout of this custom installation screen may change in future versions. A version of Qt Creator should be available somewhere in the selection. If there is no MinGW version available under your selected Qt version, search elsewhere in the component selection to find some version of MinGW.

  6. Select Qt Creator (newest version available).

  7. Accept license terms and complete the installation.

  8. Launch Qt Creator to verify successful installation.