This is an old revision of the document!
Table of Contents
Project structure
There are two views you can take toward how Qt projects are structured: the file view and the workflow view. Depending on how your brain works and/or the issue you are trying to understand, one of these will work better than the other. In all probability, both will be useful at some point.
The file view
A Qt project is a collection of project description files, source code files, resource files, and binary output files. Some of these files must be explicitly generated, others are automatically generated by the build process.
Project description files
Explicitly generated
{directory-name}.pro
- Created by running
qmake -project
- May require hand editing (e.g. to add/remove source files, specify whether debug and/or release build should be available, etc.).
Automatically generated
Makefile
,Makefile.debug
, andMakefile.release
- Standard files for use by
make
(Linux and OS X) ornmake
(Windows) to do the actual project build. - Automatically generated when you run
qmake
Source files
Explicitly generated
main.cpp
- The project's main C++ file containing the main function.
- The name of this file is a convention, not a requirement.
{yourclass}.h
and{yourclass}.cpp
- C++ class header and implementation files for a class that you derived from QObject (if needed).
- May contain
Q_OBJECT
, a Qt macro used to implement signals and slots. - There may be more than one of these.
{yourdialog}.ui
- A description of a Qt GUI.
- Typically created using Qt Designer.
Automatically generated
moc_{yourclass}.cpp
- Implementation file corresponding to
YourClass
after theQ_OBJECT
macro has been fully expanded and other QObject foo is resolved.- In other words, this is pure C++.
ui_{yourdialog}.
- C++ file based on
{yourdialog}.ui
.
Resources
{resources}.qrc
- A Qt resource collection file specifying the location of icons and other resources for your project.
- You can have more than one of these.
- Automatically processed by
rcc (the Qt resources compiler)
if mentioned in {directory-name}.pro.
Binary output
- Compiled binaries will end up in
{directory-name}
. - If
{directory-name}.pro
has been configured for both release and debug, then object files will be{directory-name}/release
or{directory-name}/debug
depending on how you invokedmake
(ornmake
). Otherwise, object files will also be in{directory-name}
.
The workflow view
The typical Qt project workflow consists of the following major phases: creating a project, adding files to a project, building and testing a project. These are discussed below.
Creating a project
- Create a new directory to contain your project that carries the name of your project.
- Create a main.cpp file in that directory that contains some skeleton code (e.g., HelloWorld).
- Execute
qmake -project
in a terminal shell in the project directory. This will generate a
{directory-name}.pro
project description file.
- Notes:
- If any *.cpp or *.h files already exist in the directory when you run
qmake -project
, they will be included in the generated{directory-name}.pro
. Having a main.cpp before you runqmake -project
will make the resulting *.pro file much easier to work with. - You should probably run
qmake -project
only once during the life of a project. After the *.pro file's initial generation, you will need to edit it manually to add/remove files from the project (see the next section).
Adding files
- Create the desired *.h, *.cpp, *.ui, and *.qrc source files in the project directory and edit to your heart's content.
- Edit
{directory-name}.pro
to reflect the added files. - Run
qmake
to generate a new Makefile and
debug
andrelease
sub-directories (if needed).
Notes:
- Repeat the above as needed. Whenever you add new files, be sure to reflect those changes into the *.pro file and regenerate the Makefile before building.
Building and testing
- If you did not configure the
{directory-name}.pro
file to build both release and debug versions, build the project by executingmake
in the project directory. If you did specify release and/or debug builds in
{directory-name}.pro
, then runmake release
or
make debug
depending on what you want.
- Launch your application by executing
./{directory-name}
in the project directory (or use your file manager to launch the executable, or load the executable into a debugger or …).
Notes:
- If you want to remove all intermediate files that the compiler generated (e.g., object files), run
make clean