Qt5和Cmake链接QApplication头错误

时间:2018-03-20 06:23:46

标签: c++ cmake qt5 qwidget qapplication

我正在尝试将cmake与我使用QApplication的一个Qt项目联系起来。 我在构建项目时收到此错误

Cannot open include file: 'QApplication': No such file or directory

我只在我的.cpp文件中使用QApplication和QWebEngineWidget,在头文件中使用QDir

main.h

#ifndef MAIN_H
#define MAIN_H

#include <QDir>

QUrl getlink(){

    QUrl url = QUrl::fromLocalFile(QDir::currentPath();

    return url;

}


#endif // MAIN_H

的main.cpp

    #include <QApplication>
    #include <QWebEngineView>
    #include "main.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);

    QWebEngineView view;
    view.setUrl(getlink());
    view.resize(1524, 850);
    view.show();

   return app.exec();

}

这是我的CMake文件中的代码

if (ENABLE_QT_SimApp_GUI)

find_package(qtlibs)

find_package(SimApp)
if (NOT SimApp_FOUND)
    message(FATAL_ERROR "Could not find SimApp")
endif(NOT SimApp_FOUND)

set(CMAKE_PREFIX_PATH ${PREFIX_PATH})
message(STATUS "CMAKE_PREFIX_PATH" ${CMAKE_PREFIX_PATH})

find_package(Qt5 REQUIRED COMPONENTS Widgets Core Gui Sql PrintSupport WebEngineWidgets)

qt5_wrap_cpp(QtSimAppProjectLib_hdr_moc ${QtSimAppProjectLib_hdr})
qt5_wrap_ui(QtSimAppProjectLib_ui_uic ${QtSimAppProjectLib_ui})
qt5_add_resources(QtSimAppProjectLib_qrc_rcc ${QtSimAppProjectLib_qrc})

include_directories (${PROJECT_SOURCE_DIR})
include_directories (${PROJECT_BINARY_DIR})

add_library (QtSimAppProjectLib STATIC ${QtSimAppProjectLib_src} ${QtSASQProjectLib_hdr_moc} ${QtSASQProjectLib_ui_uic})

target_link_libraries (QtSimAppProjectLib
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        Qt5::Sql
        Qt5::PrintSupport
        Qt5::WebEngineWidgets)

#WIN32 to suppress the console window under Windows
add_executable(SIM_APP WIN32 ${QtSimAppProjectLib_src} ${QtSimAppProjectLib_qrc_rcc})

    if (ENABLE_COPY_QT_LIBS AND WIN32)
        find_package(qtdlls)
    endif(ENABLE_COPY_QT_LIBS AND WIN32)

endif (ENABLE_QT_SimApp_GUI)

我知道我可能不需要其他链接的库,但我将它们留在那里以供将来开发。

无论如何,当我在目标链接中为QWebEngineView添加Qt5 :: WebEngineWidget时,它运行正常,但是当我为QApplication添加Qt5 :: Widgets时,它似乎无法找到该文件,我不知道为什么。< / p>

关于可能出现什么问题的任何想法?

1 个答案:

答案 0 :(得分:1)

如@vre的评论所述,您实际上从未将GUI应用程序链接到Qt5。以下是您发布的CMakeLists.txt

的固定版本
if (ENABLE_QT_SimApp_GUI)

find_package(qtlibs)

find_package(SimApp)
if (NOT SimApp_FOUND)
    message(FATAL_ERROR "Could not find SimApp")
endif(NOT SimApp_FOUND)

set(CMAKE_PREFIX_PATH ${PREFIX_PATH})
message(STATUS "CMAKE_PREFIX_PATH" ${CMAKE_PREFIX_PATH})

find_package(Qt5 REQUIRED COMPONENTS Widgets Core Gui Sql PrintSupport WebEngineWidgets)

qt5_wrap_cpp(QtSimAppProjectLib_hdr_moc ${QtSimAppProjectLib_hdr})
qt5_wrap_ui(QtSimAppProjectLib_ui_uic ${QtSimAppProjectLib_ui})
qt5_add_resources(QtSimAppProjectLib_qrc_rcc ${QtSimAppProjectLib_qrc})

include_directories (${PROJECT_SOURCE_DIR})
include_directories (${PROJECT_BINARY_DIR})

add_library (QtSimAppProjectLib STATIC ${QtSimAppProjectLib_src} ${QtSASQProjectLib_hdr_moc} ${QtSASQProjectLib_ui_uic})

target_link_libraries (QtSimAppProjectLib
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        Qt5::Sql
        Qt5::PrintSupport
        Qt5::WebEngineWidgets)

#WIN32 to suppress the console window under Windows
add_executable(SIM_APP WIN32 ${QtSimAppProjectLib_src} ${QtSimAppProjectLib_qrc_rcc})

# use one of the following target_link_libraries() calls
target_link_libraries(SIM_APP Qt5::Core Qt5::Gui Qt5::Widgets)
# target_link_libraries(SIM_APP QtSimAppProjectLib)

if (ENABLE_COPY_QT_LIBS AND WIN32)
    find_package(qtdlls)
endif(ENABLE_COPY_QT_LIBS AND WIN32)

endif (ENABLE_QT_SimApp_GUI)