In this example, we simply create and show a push button in a window frame on the desktop. The push button will have the label Hello world!
This represents the simplest possible Qt program.
First of all we need a project file:
helloworld.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = helloworld
TEMPLATE = app
SOURCES += main.cpp
greaterThan
line in order to compile it with Qt5.We also need the main.cpp containing a Qt application:
main.cpp
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton button ("Hello world!");
button.show();
return a.exec(); // .exec starts QApplication and related GUI, this line starts 'event loop'
}
a.exec()
the Qt event loop is launched.Hello world!
. The next line, button.show()
, shows the push button on the screen in its own window frame.Finally, to run the application, open a command prompt, and enter the directory in which you have the .cpp file of the program. Type the following shell commands to build the program.
qmake -project
qmake
make