Lu Ma
Lu Ma

Reputation: 147

there is an error QApplication: no such file or directory

I have installed QtCreator 2.7.2 , but when I try compiling my code include QApplication it gives me that error :Error QApplication: no such file or directory.

It can compile the tutorials code "notepad" smoothly, and I can find QApplication in /usr/include/qt4/QtGui/QApplication.

here is my code : #include #include "framework/vision_app.h"

int main(int argc, char** argv)
{
    QApplication qapp(argc, argv);

    VisionApp vapp;
    if (vapp.init(argc, argv))
    {
       return qapp.exec();
    }
    else
    {
        return 1;
    }
}

to say more, I don't have any .pro file. I use cmake to package my code.

here is my cmakelist:

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

# Set the build type.  Options are:
#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)

rosbuild_init()
rosbuild_add_boost_directories()

find_package(OpenCV 2.3 REQUIRED)
if(OpenCV_FOUND)
  message(STATUS "OpenCV include: ${OpenCV_INCLUDE_DIRS}")
  message(STATUS "OpenCV libs: ${OpenCV_LIBS}")
  include_directories(${OpenCV_INCLUDE_DIRS})
else(OpenCV_FOUND)
  message(SEND_ERROR "OpenCV not found!!")
endif()

# Qt4 requirements
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set(QT_USE_QTOPENGL TRUE)
find_package(OpenGL REQUIRED)
message (STATUS "OpenGL_found:" ${OPENGL_FOUND})

find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})
add_definitions(-DQT_NO_KEYWORDS)

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

#uncomment if you have defined messages
#rosbuild_genmsg()
#uncomment if you have defined services
#rosbuild_gensrv()

include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/src/gui)
set(we_vision_HDRS src/framework/vision_app.h
  src/gui/main_window.h
)
set(we_vision_SRCS src/vision_node_qt.cpp
  src/framework/vision_app.cpp
  src/gui/main_window.cpp
)
set(we_vision_UIS res/main_window.ui)
QT4_WRAP_CPP(we_vision_HDRS_MOC ${we_vision_HDRS})
QT4_WRAP_UI(we_vision_UI_HDRS ${we_vision_UIS})

rosbuild_add_executable(vision_node_qt ${we_vision_SRCS}
  ${we_vision_HDRS_MOC}
  ${we_vision_UI_HDRS}
)
target_link_libraries(vision_node_qt ${OpenCV_LIBS} ${QT_LIBRARIES} ${OPENGL_LIBRARIES})
rosbuild_link_boost(vision_node_qt thread filesystem system)

can anybody help me?

Upvotes: 2

Views: 11690

Answers (6)

Vinay Kumar Shukla
Vinay Kumar Shukla

Reputation: 21

Adding

target_link_libraries(PROJECT_NAME
  PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

solved my problem.

Upvotes: 0

DomTomCat
DomTomCat

Reputation: 8569

The following command (after add_executable) did the trick for me:

qt5_use_modules(${PROJECT_NAME} Widgets)

Upvotes: 2

momusa
momusa

Reputation: 1

Using qmake-qt4 (instead of qmake) worked for me.

Upvotes: -1

Greenflow
Greenflow

Reputation: 3989

You won't believe it, but I just had with a very old project exactly the same problem you had: Error QApplication: no such file or directory

What I discovered: I added, as I proposed, MESSAGE(${QT_INCLUDES}) to my CMakeLists.txt. This revealed something very interesting: All my Qt includes pointed to an old Qt 4.8.2, which did not exist on my machine anymore. Even when I added a new kit and ran CMake, it did not update CMakeCache.txt. The kit was connected to my recent Qt 4.8.5, but cmake 'found' Qt 4.8.2. I solved my problem by deleting the old CMakeCache.txt

When QtCreator did not find CMakeCache.txt, it created its own. But not where I had my old, outdated one, but in the build folder. So maybe this is the explanation, why your commandline works, but not the QtCreator: There are two different QMakeCache.txt files. On commandline you use a correct one, the QtCreator uses its own outdated.

Upvotes: 4

CCC
CCC

Reputation: 2242

It may cause by two points,

  1. QtCreator2.7.2 released with Qt5. In your .pro file, you may add this line
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    and rebuild your project again.

  2. If your code is GUI based, make sure your .pro has QT += gui

Upvotes: -1

Greenflow
Greenflow

Reputation: 3989

I see include_directories() several times. I never tried this, so I don't know if you add to the include path this way or just overwrite the path again and again.

Try:

include_directories(
  ${PROJECT_SOURCE_DIR}/src
  ${PROJECT_SOURCE_DIR}/src/gui
  ${QT_INCLUDES}
  ${CMAKE_CURRENT_BINARY_DIR}
  #more includes if necessary
)

Especially add ${QT_INCLUDES} in include_directories.

Upvotes: 1

Related Questions