C++ - Qt Framework - Hello World

Now that you have already installed Eclipse, Qt Framework Open Source and MinGW with the this tutorial, we will create our first program window.

Let's go.

1. Hello World on Windows OS

Open Eclipse, on the Explorer > Right Click > New Project > Qt > Qt Gui Project.
Write a project name such as hello-world, then Next, Next, Finish.

A lot of files and directories are created for you.
Delete all.

So now your directory hello-world is empty.
Create a main.cpp file.

Inside write this:

#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTextEdit textEdit;

    textEdit.setWindowTitle("Our first window :D");
    textEdit.setText("Hello, you can write here.");
    textEdit.show();

    return app.exec();
}


Now open a MinGW shell by running this file: C:\soft\min-gw\msys\1.0\msys.bat
Go until the project directory and write this command:

ls

The shell displays just the main.cpp file.

OK, let's create all files needed for our executable.
In the same directory type this:

qmake -project

Then:

ls

A new file has appeared, the hello-world.pro one.

The tool qmake is a software created by Nokia company for using Qt.
So the file with a .pro extension are used by qmake to generate all others files.

Let's continue and simply type this in the shell:

qmake

Then once again:

ls

You are seeing 5 files:

  1. Makefile
  2. Makefile.Debug
  3. Makefile.Release
  4. hello-world.pro
  5. main.cpp

And 2 directories:

  1. release
  2. debug

Let's finish by compiling our project:

make release

Open your directory hello-world\release and double click the hello-world.exe to see the window appears.

You want now send this program to your best friend.
He has also a Windows OS but without QT nor MinGW installed in his computer.

You have to send him the .exe with all files required to execute this program.
These files are:

  • libgcc_s_dw2-1.dll
  • libstdc++-6.dll
  • mingwm10.dll
  • QtCore4.dll
  • QtGui4.dll

You can find them respectively in:

  • C:\soft\mingw\bin
  • C:\soft\mingw\bin
  • C:\soft\mingw\bin
  • C:\soft\nokia\qt-opensource\4.7.4\bin
  • C:\soft\nokia\qt-opensource\4.7.4\bin

So, do not forget to send these files with the .exe if you expect that your friend see the window program appears.

Otherwise he should have great errors such as:

  • Entry point for function _Z5qFreePV not found in QtCore4.dll
  • This application has failed to start because libgcc_s_dw2-1.dll was not found
  • This application has failed to start because libstdc++-6.dll was not found
  • The procedure entry point ?trUtf8@QMetaObject@@QBE?AVQString@@PBD0H@Z could not be located in the dynamic link library QtCore4.dll.

Great job, you made it! angel

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.