The first thing you would 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. 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:
.pro
filesThis 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:
This is a recursive definition and we can code it in the following way:
unsigned long factorial(unsigned int n) { switch(n) { case 0: return 0; case 1: return 1; default: return n * factorial(n-1); } }
An alternate definition that avoids the cost of function calls is given as follows:
unsigned long factorial(unsigned int n) { unsigned long result = 1; for(unsigned int i = n; i > 1; i--) { result *= i; } return result; }
Why did I pick the recursive definition? There are three reasons for this: I think that it's clearer, the function call's performance overhead isn't a big deal in this example, and many of you might be using this book as part of introductory Computer Science courses where recursion is taught and should be reinforced.
Let's begin by creating the library that implements our factorial function. To do this, follow these steps:
MathFunctions
. Choose a reasonable path for the project.A statically linked library is included in your program binary and is part of your application. If multiple applications use a static library, each will have its own copy. A dynamically linked library is stored as a separate file and can be shared by multiple applications at runtime because each application loads the dynamically linked library. Qt also supports plugins, which are dynamic libraries loaded at runtime that can extend an application's functionality.
mathfunctions.h
to include a static method declaration for our factorial function:#ifndef MATHFUNCTIONS_H
#define MATHFUNCTIONS_H
class MathFunctions
{
public:
MathFunctions();
static unsigned long int factorial(unsigned int n);
};
#endif // MATHFUNCTIONS_H
mathfunctions.cpp
. You can do this in one of the two or three ways available: either by double-clicking on it in the Project pane, by right-clicking on the factorial function and selecting Switch Header/Source, or by simply hitting the F4 key. Write your factorial function; mathfunctions.cpp
should now comprise something similar to this:#include "mathfunctions.h" MathFunctions::MathFunctions() { } unsigned long int MathFunctions::factorial(unsigned int n) { switch(n) { case 0: return 0; case 1: return 1; default: return n * factorial(n-1); } }
release
and debug
, respectively.As you write the code, note that Qt Creator prompts you at various stages about things it can deduce from your header with automatic suggestions (called autosuggest). For example, once you type MathFunc
, it offers to autocomplete the class name or the C preprocessor guard; you can select the class name either using the mouse or just hit Return to get the class name.
Similarly, typing the double colons tells Qt Creator that you're trying to enter something in the MathFunctions
class, and it prompts you with the MathFunctions
class members; you can use the arrows to select factorial
and hit Return, and it will type that.
Finally, typing an opening parenthesis cues Qt Creator that you're defining a function, and it prompts you with the arguments to the function you defined in the header file. You'll see this autocompletion a lot when you type code; it's a great way to learn Qt too, because you can type a class name or part of a function name and Qt Creator prompts you with helpful hints along the way. Qt Creator can also autocomplete variable and method names; start typing a function name and press Ctrl + Space bar to see a menu of possible completions.
Before you continue, be sure to build your library in both the release and debug configurations. The easiest way to do this is to click on the build selector at the bottom-left of the software and select either Release or Debug and then click on the hammer icon to perform a build.