简体   繁体   English

Qt5 和 Cmake 链接 QApplication header 错误

[英]Qt5 and Cmake linking QApplication header error

I am trying to link cmake with one of my Qt projects that uses QApplication.我正在尝试将 cmake 与我使用 QApplication 的 Qt 项目之一链接。 I am getting this error when I build the project我在构建项目时收到此错误

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

I am only using QApplication and QWebEngineWidget in my.cpp file and QDir in the header file我只在 my.cpp 文件中使用 QApplication 和 QWebEngineWidget,在 header 文件中使用 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();

}

and here is the code from my CMake file for this application这是我的 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)

I am aware that I may not need the other linked libs but I kept them there for future development.我知道我可能不需要其他链接的库,但我将它们保留在那里以供将来开发。

Anyway, when I added Qt5::WebEngineWidget for QWebEngineView in the target link it worked fine, but when I added Qt5::Widgets for QApplication it does not seem to be able to find the file and i dont know why.无论如何,当我在目标链接中为 QWebEngineView 添加 Qt5::WebEngineWidget 时它工作正常,但是当我为 QApplication 添加 Qt5::Widgets 时,它似乎无法找到该文件,我不知道为什么。

Any idea on what could be the issue?关于可能是什么问题的任何想法?

As mentioned in the comments by @vre , you never actually link your GUI app to Qt5 . 如@vre的评论中所述,您实际上从未将GUI应用程序链接到Qt5 Below is a fixed version of your posted CMakeLists.txt 以下是您发布的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)

It's not answer, but yet another question, While compiling a cmake generated ninja build for Qt.这不是答案,而是另一个问题,在编译 cmake 时,为 Qt 生成了忍者构建。 we get such an error - #include "../../../../../../.:/e./github/Qt/qtbase/src/corelib/animation/qparallelanimationgroup,h".我们收到这样的错误 - #include "../../../../../../.:/e./github/Qt/qtbase/src/corelib/animation/qparallelanimationgroup,h"。 or something like this, Of course.或类似的东西,当然。 the file won't be found, Where, I gess?找不到文件,我猜在哪里? is the fault - in cmake or in a cmake script for Qt building, It happens from version into version.是错误 - 在 cmake 或 cmake 脚本中用于 Qt 构建,它发生在版本之间。 when you build QT a 'disk-away' far from its sources location.当您构建 QT 时,远离其源位置的“磁盘”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM