I have installed C++SDK that have Qt but when I try compiling a code linking QApplication it gives me the error:
Error QApplication: no such file or directory
you have to add QT +=widgets in the .pro file before the first execution, if you execute before adding this line its not gonna working, so yo need to start file's creation from the beginning.
In Qt5 you should use QtWidgets
instead of QtGui
#include <QtGui/QComboBox> // incorrect in QT5
#include <QtWidgets/QComboBox> // correct in QT5
Or
#include <QtGui/QStringListModel> // incorrect in QT5
#include <QtCore/QStringListModel> // correct in QT5
I suggest you to update your SDK and start new project and recompile everything you have. It seems you have some inner program errors. Or you are missing package.
And ofc do what Abdijeek said.
Please make sure that the version of qmake you are using corresponds to the version of QT you want to use.
To be sure, you can just run :
$qmake -v
Your problem seems to be a symptom of a version conflict between QT 3 and 4, as can be seen here :
http://lists.trolltech.com/qt4-preview-feedback/2005-11/thread00013-0.html
To fix this, you can either delete your old install of QT, or specifically point to qmake-qt4 in your Makefile.
To start things off, the error QApplication: no such file or directory
means your compiler was not able to find this header. It is not related to the linking process as you mentioned in the question.
The -I
flag (uppercase i) is used to specify the include (headers) directory (which is what you need to do), while the -L
flag is used to specify the libraries directory. The -l
flag (lowercase L) is used to link your application with a specified library.
But you can use Qt to your advantage: Qt has a build system named qmake which makes things easier. For instance, when I want to compile main.cpp I create a main.pro file. For educational purposes, let's say this source code is a simple project that uses only QApplication
and QDeclarativeView
. An appropriate .pro file would be:
TEMPLATE += app
QT += gui declarative
SOURCES += main.cpp
Then, execute the qmake
inside that directory to create the Makefile that will be used to compile your application, and finally execute make
to get the job done.
On my system this make
outputs:
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_DECLARATIVE_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/opt/qt_47x/mkspecs/linux-g++ -I. -I/opt/qt_47x/include/QtCore -I/opt/qt_47x/include/QtGui -I/opt/qt_47x/include/QtDeclarative -I/opt/qt_47x/include -I/usr/X11R6/include -I. -o main.o main.cpp
g++ -Wl,-O1 -Wl,-rpath,/opt/qt_47x/lib -o main main.o -L/opt/qt_47x/lib -L/usr/X11R6/lib -lQtDeclarative -L/opt/qt_47x/lib -lQtScript -lQtSvg -L/usr/X11R6/lib -lQtSql -lQtXmlPatterns -lQtNetwork -lQtGui -lQtCore -lpthread
Note: I installed Qt in another directory --> /opt/qt_47x
Edit: Qt 5.x and later
Add QT += widgets
to the .pro file and solve this problem.
For QT 5
Step1:
.pro
(in pro file, add these 2 lines)
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
Step2:
In main.cpp
replace code:
#include <QtGui/QApplication>
with:
#include <QApplication>