c++ - Qt5 和 Cmake 链接 QApplication 头错误

标签 c++ cmake qt5 qwidget qapplication

我正在尝试将 cmake 与我使用 QApplication 的 Qt 项目之一链接起来。 我在构建项目时遇到此错误

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

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

主要.h

#ifndef MAIN_H
#define MAIN_H

#include <QDir>

QUrl getlink(){

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

    return url;

}


#endif // MAIN_H

主要.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 时,它似乎无法找到该文件,我不知道为什么。

知道可能是什么问题吗?

最佳答案

正如@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)

关于c++ - Qt5 和 Cmake 链接 QApplication 头错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49377527/

相关文章:

c++ - 以编程方式推广 QWidget

c++ - C++ 中的无休止循环,比较两个字符

c++ - 基于编译时元编程的定点算法。乘法溢出?

C++ DLL 函数导出。 DLL 不会保持加载状态

cmake - CPack:如何执行多个 CPACK_NSIS_EXTRA_INSTALL_COMMANDS?

c++ - 如何识别应用程序在 Linux 上以深色主题运行?

具有大量 Windows 或复杂 UI 的 Qt 5 QML 应用程序

c++ - .EOF 在读取文件 :FIXXED 时导致段错误

windows - 如何使用 CMake 生成 Windows DLL 版本控制信息?

c++ - 用cmake构建比特币