Setup Qt5 w/o QtCreator in Linux

Setup Qt5 w/o QtCreator on Linux (Debian)

I generally dont like a special IDE for making a GUI application, like QtCreator, i wanted to have more control and use Makfile and make to build the Qt Application. So this is a guide on how i did that.

For anybody who doesn't know: QTCreator is an IDE (Integrated Development Environment) for QT. It is developed and maintained by the same people who develop the QT libraries and is an excellent piece of software to use for quickly building QT based applications.

Moreover the Qt Installer is huge and has a lot of packages that i don't really need, i wanted just the dev packages and qmake so that i could do everything on the terminal. I am using a Raspberry Pi for this tutorial, but this guide will work for any Linux based on Debian, prolly similar to other distors.

I am assuming you have g++ and make installed

Install the dev sources for simple Qt5 applications

sudo apt install qt5-default qt5declarative5-dev

Create the application source files

create a folder QtTest and place these files there (refer to the link in references on explanation of these)

main.cpp

#include <QtWidgets>
#include "mainwidget.h"

int main(int argc, char *argv[])
{
    // Creates an instance of QApplication
    QApplication a(argc, argv);

    // This is our MainWidget class containing our GUI and functionality
    MainWidget w;
    w.show(); // Show main window

    // run the application and return execs() return value/code
    return a.exec();
}

mainwidget.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>

class QPushButton;
class QTextBrowser;

// This is the declaration of our MainWidget class
// The definition/implementation is in mainwidget.cpp
class MainWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MainWidget(QWidget *parent = 0); //Constructor
    ~MainWidget(); // Destructor

private:
   QPushButton* button_;
   QTextBrowser* textBrowser_;
};

#endif // MAINWIDGET_H

mainwidget.cpp

#include <QtWidgets>
#include "mainwidget.h"

// Constructor for main widget
MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent)
{
   button_ = new QPushButton(tr("Push Me!"));
   textBrowser_ = new QTextBrowser();

   QGridLayout *mainLayout = new QGridLayout;
   mainLayout->addWidget(button_,0,0);
   mainLayout->addWidget(textBrowser_,1,0);
   setLayout(mainLayout);
   setWindowTitle(tr("Connecting buttons to processes.."));
}

// Destructor
MainWidget::~MainWidget()
{
   delete button_;
   delete textBrowser_;
}

Build !

Generate the build files

qmake -project

This will generate a QtTest.pro file, which will be used by qmake to create the Makefile

qmake doesn't really detect the dependencies so we will add it ourselves i.e. QT += widgets

QtTest.pro

######################################################################
# Automatically generated by qmake (3.1) Wed Sep 9 22:00:28 2020
######################################################################

TEMPLATE = app
TARGET = QtTest
INCLUDEPATH += .

QT += widgets

# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

# Input
HEADERS += mainwidget.h
SOURCES += main.cpp mainwidget.cpp

Run this to generate the Makefile

qmake QtTest.pro

Make Make Make !

make

Run !

./QtTest

And here it is

run

References