When I’m working with QT, the most difficult thing is to configure
.PRO file. I do not understand about structure of this file and it makes me confused.
So, in this article, we will discuss about all things that turn around
Table of contents
Inroduction to QT
According to wiki.qt.io, we have:
QT (pronounced as **cute**, not **cu-tee**) is a cross platform framework that is usually used as a graphical toolkit, although it is also very helpful in creating CLI applications. It runs on the three major desktop OSes, as well as on mobile OSes, such as Symbian, Nokia Belle, Meego Harmattan, MeeGo or BB10, and on embedded devices.
Some common modules that use in QT:
||a base library that provides containers, thread management, event management, and much more.|
||a GUI toolkit for Desktop, that provides a lot of graphical components to design applications.|
||provides a useful set of classes to deal with network communications.|
||the webkit engine that enable the use of web pages and web apps in a Qt application.|
||a full featured SQL RDBM abstraction layer extensible with own drivers, support for
||support for simple
Structure of PRO file
.PRO file is the project file. Qt will use a command line tool to parses these project files to generate
makefiles, files that are used by compilers to build an application. This tool is called
Project files are plain text (i.e. use an editor like notepad, vim or xemacs) and must be saved with a
.pro extension. The name of the application’s executable will be the same as the project file’s name, but with an extension appropriate to the platform.
For example, a project file called ‘hello.pro’ will produce ‘hello.exe’ on Windows and ‘hello’ on Unix.
In a project file, there is some minimal code that should always be written:
TEMPLATE = app TARGET = name_of_the_app QT += core gui QT += network greaterThan(AT_MAJOR_VERSION, 4): QT += widgets SOURCES += \ main.cpp \ test.cpp HEADERS += main.h \ test.h CONFIG += qt warn_on release CONFIG += c++11
TEMPLATE- describe the type of build. It can be an appliation -
library, or simply
TARGET- the name of the app or the library.
QT- used to indicate what libraries (QT modules) are being used in our project.
The table shows the options that can be used with the Qt variable:
Option Features core (include by default) QtCore module gui (include by default) QtGui module network QtNetwork module opengl QtOpenGL module sql QtSql module svg QtSvg module xml QtXml module xmlpatterns QtXmlPatterns module qt3support Qt3Support module
Note that adding the
opengloption to the QT variable automatically causes the equivalent option to be added to the
CONFIGvariable. Therefore, for Qt applications, it is not necessary to add the
opengloption to both
SOURCES- specifies the source files that implement the application. Most applications require multiple files; this situation is dealt with by listing all the files on the same line space separated.
SOURCEs = main.cpp test.cpp
or each file can be listed on a separate line, by escaping the newlines.
SOURCES = main.cpp \ test.cpp
or a more verbose approach is to list each file separately.
SOURCES += main.cpp SOURCES += test.cpp
This approach uses
=which is safer, because it always adds a new file to the existing list rather than replacing the list.
HEADERS- specify the header files created for use by the application.
CONFIG- used to give
qmakeinformation about the application’s configuration.
CONFIGvariable specifies the options and features that the compiler should use and the libraries that should be link against. Anything can be added to the
Some following options control the compiler flags that are used to build the project:
Option Description release The project is to be built in release mode. This is ignored if debug is also specified. debug
CONFIG += qt warn_on release
+=- means we add our configuration options to any that are already present. This is safer than using
=which replaces all options with just those specified.
qmakethat the application is built using Qt. This means that
qmakewill link against the Qt libraries when linking and add in the neccesary include paths for compiling.
qmakethat it should set the compiler flags so that warning are output.
releasetells that the application must be built as a release application. During development, programmers may prefer to replace
Problems in QT
Error: LNK1158: cannot run ‘rc.exe’
This error will happen when we have both Visual Studio 2015 and Visual Studio 2017 or more precisely, multiple version of the Window SDK installed.
When that happens, the
vcvars32.batscript (located in our Visual Studio install dir) does not correctly add the location of the resource compiler (rc.exe) to our
PATH. Thus, QT Creator runs
vcvars32.bat(as specified in Qt Creator under Option->Build&Run->Compilers, but the tools directory for the Windows SDK Kit isn’t properly added to the PATH environment).
First way: Add the appropriate version of rc.exe to our
Do some command line:
cd "c:\program files(x86)" dir /s rc.exe
We’ll get several versions (x86 and x64) and for several versions of the SDK. Add the
PATHfor where rc.exe lives for the version that corresponds to the SDK and build flavor to our vcvars32.bat startup script.
Restart Qt Creator and that should fix it.
Uninstall all versions of Visual Studio (and all those side installs of SQL, Windows SDKs, dev tools, etc…). Reboot. Then cleanly install VS 2017 again. Then cleanly uninstall and re-install all of Qt again.
connect()method right way
When we use the above method
QObject::connect(ui->Open, SIGNAL(clicked()), p,SLOT(on_action_Open_triggered()));
It will makes our method
on_action_Open_triggered()that is called multiple times. In order to remove this problem, we need to use addtional paramater:
QObject::connect(ui->Open, SIGNAL(clicked()), p,SLOT(on_action_Open_triggered()), Qt::UniqueConnection);
QObject: Cannot create children for a parent that is in a different thread. (Parent is QNetworkAccessManager(0x1c2ed984210), parent’s thread is QThread(0x1c2ed9489d0), current thread is QThread(0x1c2f1c58760)
Runmode, we will see the checkbox about
Run in terminal.
We can remove the tick in this checkbox to resolve this problem.
QObject: Cannot create children for a parent that is in a different thread
- Understanding about >PRO file in Qt.