簡體   English   中英

Qt5 和 Cmake 鏈接 QApplication header 錯誤

[英]Qt5 and Cmake linking QApplication header error

我正在嘗試將 cmake 與我使用 QApplication 的 Qt 項目之一鏈接。 我在構建項目時收到此錯誤

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

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

主文件

#ifndef MAIN_H
#define MAIN_H

#include <QDir>

QUrl getlink(){

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

    return url;

}


#endif // MAIN_H

主文件

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

這不是答案,而是另一個問題,在編譯 cmake 時,為 Qt 生成了忍者構建。 我們收到這樣的錯誤 - #include "../../../../../../.:/e./github/Qt/qtbase/src/corelib/animation/qparallelanimationgroup,h"。 或類似的東西,當然。 找不到文件,我猜在哪里? 是錯誤 - 在 cmake 或 cmake 腳本中用於 Qt 構建,它發生在版本之間。 當您構建 QT 時,遠離其源位置的“磁盤”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM