Reader small image

You're reading from  Application Development with Qt Creator - Third Edition

Product typeBook
Published inJan 2020
Reading LevelBeginner
Publisher
ISBN-139781789951752
Edition3rd Edition
Languages
Right arrow
Author (1)
Lee Zhi Eng
Lee Zhi Eng
author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng

Right arrow

Building Applications with Qt Creator

The first thing you want to do with Qt Creator is figure out how to add source files and build (or debug) your project. This chapter is all about that – we'll go over how to add files to a project, how to create libraries in a project, and how to use the debugger and the console logger. These are the essential skills you need to create a good-quality application using Qt. At the end of this chapter, you'll be driving Qt Creator to develop console applications like a pro.

In this chapter, we will do the following:

  • Learn about our sample library
  • Look into the Build menu and the .pro file
  • Link against our sample library
  • Debug
  • Build our project
  • Run and debug our application

Technical requirements

The technical requirements for this chapter include Qt 5.12.3 MinGW 64-bit, Qt Creator 4.9.0, and Windows 10.

Getting started – our sample library

This chapter's example code has two pieces: a library that defines a public function, and a console application that calls this function. Libraries are a great way to break up your applications, and while this example is simple, it also lets me show you how to create a library and include it in your application.

I'm going to stretch your imagination a bit; let's pretend that you're responsible for setting up a library of math functions. In this example, we'll only write one function, factorial. You should be able to recollect the factorial function from introductory programming; it's represented by a ! and is defined as follows:

  • 0! is 1
  • 1! is 1
  • n! is n × (n - 1)!

This is a recursive definition and we can code it in the following way:

unsigned long factorial(unsigned int n) 
{ 
    switch(n)  
   ...

Learning the landscape – the Build menu and the .pro file

In the previous chapter, you learned how to build applications by hitting the hammer button in the corner of Qt Creator's main window or by starting the debugger. To just build your library – or any application – you can either use the hammer icon or the various choices in the Build menu. The obvious choice is either Build All or Rebuild All. Choosing Build All recompiles only those files that need to be rebuilt as recognized by Qt Creator; Rebuild All cleans the project of all the object files and rebuilds the entire project from scratch.

In most cases, it's sufficient to choose Build All, and that's what you want to do because it's faster. Sometimes, you really do want to rebuild the whole project, especially when things are broken and Qt's make system can't reconcile...

Linking against our sample library

Now, let's make an application that depends on our library. Our application will call the factorial function in the library, statically linking to the library in order to access the factorial function. To accomplish this, you need to perform the following steps:

  1. Select Close All Projects and Editors from the File menu.
  2. Choose New File or Project from the File menu and create a new Qt console application called MathFunctionsTest using the wizard.
  3. Right-click on MathFunctionsTest in the Project pane and click on Add Library. You can now choose a library in your build tree, a library outside your build tree, an external library on your system such as the Unix math library, fftmpeg, or another library that you've created. Select External Library and click on Next.
  1. Browse the library file that was built in the previous section by clicking...

Getting lost and found again – debugging

Qt Creator has a state-of-the-art GUI that hooks into either the GNU Debugger (GDB), or Microsoft's command-line debugger, CDB, if you use Microsoft tools.

If you've installed Qt Creator on macOS, Linux, or the MinGW version of Qt Creator for Windows, you have everything you need to begin debugging your application. If you already have Microsoft Visual Studio installed and then installed a version of Qt Creator that uses Microsoft's compiler, you also need to install the Microsoft command-line debugger to use Qt Creator's debugging features. Here's how you can install the command-line debugger:

  1. Download the debugging tools for Windows, either from http://msdn.microsoft.com/en-us/windows/hardware/hh852365 if you are using the 32-bit version of the compiler and Qt Creator, or from http://msdn.microsoft.com...

The Projects pane and building your project

You've seen how the .pro file affects your project's compilation, but there's even more to it than this. If you click on the Projects button on the left of Qt Creator, you'll see the project's options, which consist of the following:

  • The Build & Run options
  • The Editor options
  • The Code Style options
  • Dependencies

Each of these is in its own panel.

In most cases, you won't need to monkey around with any of these settings, but you might have to tinker with the Build & Run settings, especially if you're targeting multiple platforms such as Windows and Linux with cross-compilers or Android. (I will write more about this exciting development in Qt later in this book.)

The final thing that you should know is the build and run kit selector. Qt is one of the best cross-platform toolkits available today...

A review – running and debugging your application

You'll spend a lot of time editing, compiling, and debugging your code in Qt Creator, so it's wise to remember the following basics:

  • The arrow key runs your application without the debugger; to debug your application, choose the arrow key with the bug icon on it.
  • You can switch between the Editor view and the Debug view of your application by clicking on the Edit or Debug view choice on the left-hand side; if you debug your application, Qt Creator will enter the Debug view automatically.
  • There's more to breakpoints than just stopping at a line of code! Use data breakpoints to pin down weird bugs that occur only sometimes, or to quickly skip over the first bazillion items of a large loop.
  • The variable pane lets you see more than just the contents of variables; you can also add expressions composed of several...

Summary

Qt Creator's Integrated Development Environment (IDE) contains an editor and tools to start the compiler, linker, and debugger in order to build and debug your applications. Using this, you can start and stop your application, place breakpoints while your application is stopped, or examine the variables or the logical flow of your application.

While Qt Creator manages most of the project for you, sometimes you just have to get down and dirty with a .pro file. You can use scopes to handle conditional compilation (things such as when building for a specific platform or whether a file should be included in the Release or Debug mode). The .pro file consists of scopes, variables, and their values; by setting the variables that the .pro file feeds qmake, qmake understands the dependencies in your project and magically creates a Makefile to build your application.

In this...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Application Development with Qt Creator - Third Edition
Published in: Jan 2020Publisher: ISBN-13: 9781789951752
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng