Error QApplication: no such file or directory

C++Qt

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

In Qt 5 you now have to add widgets to the QT qmake variable (in your MyProject.pro file).

 QT += widgets

Solution 2 - C++

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.

Solution 3 - C++

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>

Solution 4 - C++

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.

Solution 5 - C++

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.

Solution 6 - C++

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

Solution 7 - C++

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.

Solution 8 - C++

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.

Solution 9 - C++

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

Solution 10 - C++

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! :)

Solution 11 - C++

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

Solution 12 - C++

> 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

Solution 13 - C++

> CONFIG += c++11

Write above code on myproject.pro

I'm using linuxmint 20.0

Solution 14 - C++

#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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRamy Al ZuhouriView Question on Stackoverflow
Solution 1 - C++TimmmmView Answer on Stackoverflow
Solution 2 - C++karlphillipView Answer on Stackoverflow
Solution 3 - C++Jassim TalatView Answer on Stackoverflow
Solution 4 - C++Abhijeet RastogiView Answer on Stackoverflow
Solution 5 - C++LoomchildView Answer on Stackoverflow
Solution 6 - C++AliSView Answer on Stackoverflow
Solution 7 - C++KABOURI ABDERRAZZAKView Answer on Stackoverflow
Solution 8 - C++DenonthView Answer on Stackoverflow
Solution 9 - C++cstreamView Answer on Stackoverflow
Solution 10 - C++teresaView Answer on Stackoverflow
Solution 11 - C++faressalemView Answer on Stackoverflow
Solution 12 - C++MAYOBYO HASSANView Answer on Stackoverflow
Solution 13 - C++Sailendra ChettriView Answer on Stackoverflow
Solution 14 - C++Denis VerreaultView Answer on Stackoverflow