Reputation: 21966
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
How do I link these libraries? I searched into the directories and there is a file named QApplication.h; So I tried to link it with -I (linking the directory) but it was still giving me that error.
Upvotes: 86
Views: 205584
Reputation: 31
CONFIG += c++11
Write above code on myproject.pro
I'm using linuxmint 20.0
Upvotes: 1
Reputation: 506
Am using QT 6
I found the problem to be on how i set the version
I had set a version greater than 6 meaning version 7 and above, yet am using QT version 6 so i changed from this
greaterThan(QT_MAJOR_VERSION, 6) : QT += widgets
To
greaterThan(QT_MAJOR_VERSION, 5) : QT += widgets
Upvotes: 2
Reputation: 9
#include #include
#include "ui_mainwindow.h" Error here ...
I have fix: I use KDevelop, File->Open..->build->build ->(clic on ui_mainwindow.h) -> Open
Voila!
Upvotes: -1
Reputation: 664
Well, It's a bit late for this but I've just started learning Qt and maybe this could help somebody out there:
If you're using Qt Creator then when you've started creating the project you were asked to choose a kit to be used with your project, Let's say you chose Desktop Qt <version-here> MinGW 64-bit
. For Qt 5, If you opened the Qt folder of your installation, you'll find a folder with the version of Qt installed as its name inside it, here you can find the kits you can choose from.
You can go to /PATH/FOR/Qt/mingw<version>_64/include
and here you'll find all the includes you can use in your program, just search for QApplication
and you'll find it inside the folder QtWidgets
, So you can use #include <QtWidgets/QApplication>
since the path starts from the include
folder.
The same goes for other headers if you're stuck with any and for other kits.
Note: "all the includes you can use" doesn't mean these are the only ones you can use, If you include iostream
for example then the compiler will include it from /PATH/FOR/Qt/Tools/mingw<version>_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/iostream
Upvotes: 1
Reputation: 93410
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.
Upvotes: 85
Reputation: 96586
In Qt 5 you now have to add widgets
to the QT
qmake variable (in your MyProject.pro
file).
QT += widgets
Upvotes: 140
Reputation: 1
You can change build versiyon.For example i tried QT 5.6.1 but it didn't work.Than i tried QT 5.7.0 .So it worked , Good Luck! :)
Upvotes: -1
Reputation: 401
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>
Upvotes: 40
Reputation: 11
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.
Upvotes: 1
Reputation: 111
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
Upvotes: 6
Reputation: 139
Make sure you have qmake in your path (which qmake), and that it works (qmake -v) (IF you have to kill it with ctr-c then there is something wrong with your environment).
Then follow this: http://developer.qt.nokia.com/doc/qt-4.8/gettingstartedqt.html
Upvotes: 0
Reputation: 96
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.
Upvotes: 4
Reputation: 65
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.
Upvotes: -1
Reputation: 17234
Looks like you don't have the development libraries installed. Install them using:
sudo apt-get install libqt4-dev
As you said int the comments that you have them installed, just re-install it. Now. to update the locate's database, issue this command $sudo updatedb
Then $locate QApplication
to check that you now have the header file installed.
Now, goto the the folder where you have the code & type these commands
qmake -project
qmake
make
Then you can find the binary created.
Alternatively, you can use Qt Creator if you want the GUI.
See the official documentation for more info. http://developer.qt.nokia.com/doc/qt-4.8/gettingstartedqt.html
To learn how to use Qt Creator, use http://doc.qt.nokia.com/qtcreator-2.2/creator-qml-application.html
Upvotes: 16