Setup Dev Environment for QT5 Development

QT Creator Didn't Work for Me

The first thing I tried was installing QT Creator using Linux Mint's Software Manager. After installing it, I took it for a spin and it didn't work. When I tried to create a test project, it asked me to select a kit. And there is no Kit. There is a configuration that was not done in the QT Creator that is preventing QT Creator find the QT5 libraries.

Another steps before I tried the sample project is to setup the build environment via command line:

This command sets up the basic environment for building console applications using C/C++.

sudo apt-get install build-essential

Next, I needed CMake for the command line build.

sudo apt-get install cmake

The command that sets up the QT5 development libraries:

sudo apt-get install qt5-default

Even that I install the two, I was not able to get QT Creator to work. I tried to find the answers online, but gave up when I couldn't. So I decided that I will get this to work with a simple IDE, and make files.

What Are Need

I need Java so that I can run Eclipse. I have already installed it. By searching the web I would find Eclipse for C/C++. I am already using Eclipse for Java development and for C++, I would use the same. I get the the one compressed file with everything in it. Once I have it, I uncompress it and set the Java runtime to the Eclipse IDE. With this and the two development libraries, I am all set.

But this is not the end of this post. Next, I will discuss how I got my first simple application to work.

The First QT5 Application

I used Eclipse to create the first sample application. I chose New C/C++ Project, then CMake Project. Enter the project name, and it is all set. The sample project would be a simple Hello World (did you notice you cannot spell hello without the word Hell?) console application.

To compile and create the executable binary. I have to do the following three commands:

cd build
cmake ..
make

If I have setup the build environment correctly, I can see the executable "testqt". And I can run it locally, with the following command:

./testqt

The console will display "Hello World". The next step is to turn this application into a GUI application using QT5.

Turn Console App Into GUI Application

This is the hardest part. I have to change the file CMakeLists.txt to build QT5 GUI application. Luckily, there were resources available online. First I need to change my CPP file to create GUI windows and run. Here it is:

#include <QApplication>
#include <QWidget>

int main(int argc, char *argv[]) {

    QApplication app(argc, argv);

    QWidget window;

    window.resize(250, 150);
    window.setWindowTitle("Simple example");
    window.show();

    return app.exec();
}

I found about code from a wonderful site called the "zetcode.com". It provided this simple source code that can popup a window when executable runs. The CMakeLists.txt is a bit trickier. And "qt.io" has this page that show me how.

Anyways, here is my CMakeLists.txt file that I finally got it to work:

cmake_minimum_required(VERSION 3.1.0)

project (testqt)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

if(CMAKE_VERSION VERSION_LESS "3.7.0")
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()

include_directories(
  ${QT_INCLUDES}
  ${CMAKE_CURRENT_BINARY_DIR}
)

find_package(Qt5 COMPONENTS Widgets REQUIRED)

add_executable(testqt testqt.cpp)

target_link_libraries(testqt Qt5::Widgets)

In the middle of this file, I have to add this to it. Without it, I was not able to compile the the code. The error was: QApplication: No Such File or Directory. Here is what I added:

include_directories(
  ${QT_INCLUDES}
  ${CMAKE_CURRENT_BINARY_DIR}
)

This adds all the directory for the compiler to search for the header files. There might be some other things to include. But this simple application, this is all needed.

How to Run

After the successful build, the executable binary will be created in the build directory. It can be run with the following command in terminal:

./testqt

Here is a screenshot of application:

Summary

Why do I have to do this? I was going to learn QT5 so that I can work with the source code of CyberOS project. This project is using QT5 and the projects looks so simple. If I can have the key to understand what the codes meant, it could bring my skill set to a whole new level. At minimum, I can learn something new.

Your Comment


Required
Required
Required

All Related Comments

Loading, please wait...
{{cmntForm.errorMsg}}
{{cmnt.guestName}} commented on {{cmnt.createDate}}.

There is no comments to this post/article.