Hello World

From qtnode

Jump to: navigation, search

Here's a Hello World test for Qt.

Note: This is for Qt 4.x. If you are using Qt 3.x check out Hello World (Qt3)). For a console-based Hello World for Qt 4, check out Hello World (Console).

Contents

Create a source file

Go into an empty directory and create the following file hello.cpp:

#include <QApplication>
#include <QLabel>

int main(int argc, char **argv) {
  QApplication app(argc, argv);
  QLabel *label = new QLabel("Hello World!");
  
  label->show();

  return app.exec();
}

Create the project file with qmake

Then run the following command:

prompt> qmake -project -o hello.pro

You will now have a project file named hello.pro. This will be used to create a Makefile.

Create a Makefile with qmake

Simply run qmake once more with no options to create a Makefile:

prompt> qmake

Build your application

Depending on your environment, run the appropriate make command:

prompt> mingw32-make

or

prompt> make

or

prompt> nmake

If you recieve an error and you copied and pasted the source, make sure there's an extra line after the last bracket ( } ); just press enter and re-save it. Remember to rebuild your project after changing any sources.

Run your application

Depending on your Qt configuration, the default build target will be either in the release or debug directory. Go look for it.

Then you can run it by typing (replace \'s with /'s if on *nix):

prompt> release\hello

or

prompt> debug\hello
Personal tools