Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Application Development with Qt Creator - Second Edition

You're reading from   Application Development with Qt Creator - Second Edition Design and build dazzling cross-platform applications using Qt and Qt Quick

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher Packt
ISBN-13 9781784398675
Length 264 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (15) Chapters Close

Preface 1. Getting Started with Qt Creator 2. Building Applications with Qt Creator FREE CHAPTER 3. Designing Your Application with Qt Designer 4. Qt Foundations 5. Developing Applications with Qt Widgets 6. Drawing with Qt 7. Doing More with Qt Quick 8. Multimedia and Qt Quick 9. Sensors and Qt Quick 10. Localizing Your Application with Qt Linguist 11. Optimizing Performance with Qt Creator 12. Developing Mobile Applications with Qt Creator 13. Qt Tips and Tricks Index

Your first application – Hello World

In Qt Creator, select New File or Project… from the File menu. Qt Creator will present you with the New Project wizard, which lets you choose the kind of project you want to create, give it a name, and so forth. To create your first application, perform the following steps:

  1. Select New File or Project… if you haven't done so already.
  2. Qt Creator presents you with a dialog that has a dizzying array of project choices. Choose Application, then Qt Console Application, and then click on Choose….
  3. Qt Creator asks you for a name and a path to the directory where you want to store the files for the project. For the name, enter HelloWorldConsole and choose a path that makes sense to you (or accept the default). Then, click on Next.
  4. Qt Creator can support various kits and libraries against which you can build an application. Select the Desktop Qt Kit, which should have been installed by default, leaving both the Release and Debug choices checked. Then, click on Next.
  5. In the next step, Qt Creator prompts you about the version control for your project. Qt Creator can use your installed version control clients to perform change tracking for your project. For now, skip this and leave Add to version control set to None. Then, click on Finish.

Qt Creator creates your project and switches to the Edit view. In the source code editor for the main.cpp file, enter the following code:

#include <QCoreApplication>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
    
    cout << "Hello world!";

  return a.exec();
}

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

The QCoreApplication task handles the entire system startup for an application, and every Qt Console app needs to create one and call its exec method as part of your main method. It sets up Qt's event handler and provides a bunch of porting helpers to determine things such as your application directory, library paths, and other details.

For a console application, that's all you need; you can freely mix and match Qt classes with the C++ standard library and Standard Template Library (although once you master Qt's foundation classes, many STL constructs might feel somewhat limiting).

Next, let's compile and run the application. There are several ways to do this. You can:

  • Click on the green Run arrow below the Help view button on the left to run the application
  • Hit F5 to build and run your application in the debugger
  • Click on Start Debugging… from the Debug menu
  • Click on the green Run arrow with the bug over the arrow in order to debug the application on the left
  • Choose Run from the Build menu (or hit Ctrl + R)

    Tip

    If you only want to build the application, you can click on the hammer icon below the Run and Debug icons.

When you choose one of these options, Qt Creator invokes the compiler and the linker to build your application. If you choose the Debug option, Qt Creator switches to the Debug view (which we will discuss in detail in the next chapter) as it starts your application.

Once the application starts, you'll see the Hello world! message in a console view.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime