Qt: Hello World

So here is the most basic application of Qt: Printing HELLO WORLD !!!!!!

There are two ways of accomplishing that:

  • using Qt- Creator IDE
  • using terminal and creating our generating makefile

First the easy one 😛

  1. Using GUI method:-

First create new QT-console project. (console simply means the output will be in form of a console).

name the project as HELLO.and just click next buttons with the default setting of version control and all those stuffs.. Finally you endup with a screen like this:

Hello world example

hello.pro consists of the classes, templates, etc. included in the program.

hello.cpp will contain the actual code which will get executed.
Now, for printing the HELLO WORLD, what i have done is to simply print the output as a debug message on the consol. There are numerous techniques for printing any string. You can also print inside any widget.

Here,simply two steps are required :

  1. Include the QtDebug header file for the inclusion of this library.
  2. Call the QtDebug() function to print the string to the output.

the modified main.cpp will be as follows:

#include <QtCore/QCoreApplication>
#include <QtDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<“hello world”;
return a.exec();
}

Now, press the Build arrow on the left to see the output.1

Output of hello project

So let us build the same application with the non-Graphical approach. For this, there are few steps:

  1. Using non-GUI method: to create an application printing hello world in output:
  • for this method,you have to export the Qt-compiler to your environment. For this, type:
    $ export PATH=$PATH:/opt/QtSDK/Simulator/Qt/gcc/bin/
    where, /opt/QtSDK/ is the installation folder of Qt. This may vary for others.
  • Create a folder of same name as your project. — $ mkdir <project_name>
  • Go to folder — $ cd <project_name>
  • Open the project name.cpp file —  $ vim <name>.cpp
  • Type code, save your work and exit.
  • Now since our .cpp file ready,we have to create the <name>.pro file. This is basically a platform-independent project file. — $ qmake -project
  • Generate the makefile. — $qmake <name>.pro
  • Execute the make file to create the executable application. — $make
  • Run the application. — $ ./<name>

suppose the hello1 is the project name in this case.

  1. export PATH=$PATH:/opt/QtSDK/Simulator/Qt/gcc/bin/
  2. mkdir hello1
  3. cd hello1
  4. vim hello1.cpp

#include <QtCore/QCoreApplication>
#include <QtDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<“hello world”;
return a.exec();
}

5. save and exit.
6. qmake -project

7. qmake hello1.pro

8. make

9../hello1

hello1-output

This completes the non-graphical way of achieving the same thing.

Advertisements

About Shantanu Sharma

Currently working as Software R&D member in Samsung R&D India.

Posted on October 7, 2012, in Qt: The Face Of Embedded and tagged , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s