首页 > 解决方案 > 将 qt 与 adtf 一起使用。在构建期间生成moc文件?

问题描述

我正在尝试将 qt 与 adtf 3.3 一起使用。

来自 adtf 的文档(https://support.digitalwerk.net/adtf/v3/adtf_html/page_external_dependencies.html)和使用 qt 的 adtf 示例(https://support.digitalwerk.net/adtf/v3/adtf_html/page_demo_qt_video_display_code .html)。

关于我正在尝试做的事情的简要介绍。

我使用 QtDesigner 创建了一个 ui 文件,然后我使用 uic 编译器手动生成了头文件,然后还生成了 moc 文件,因为我具有信号和插槽功能,我还需要 moc 文件来调用元对象。

所以现在我想做的不是使用 uic 和 moc 文件手动生成头文件,而是我想在构建时使用生成 moc 文件

set(CMAKE_AUTOMOC_ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

如果我更改某些功能或添加一些信号和插槽,那么我这样做的原因是我需要生成 separatley 并添加到源文件中。

所以基本上我的 uic 生成的头文件包含有关以 qt 形式使用的对象的信息。基本上,它使用 uic 编译器将包含有关 ui 信息的 xml 文件类型转换为头文件。

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPlainTextEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QTableWidget>
#include <QtWidgets/QWidget>
#include <QLineEdit>

QT_BEGIN_NAMESPACE

class Ui_Form
{
public:
    QGroupBox *groupBox_dummy;

    void setupUi(QWidget *Form)
    {
        if (Form->objectName().isEmpty())
            Form->setObjectName(QStringLiteral("Form"));
        //Form->resize(960, 480);
        Form->setFixedSize(960, 480);
        /*Contains several qobjects removed intentionally as it makes post so long*/
        
        retranslateUi(Form);

        QMetaObject::connectSlotsByName(Form);
    } // setupUi

    void retranslateUi(QWidget *Form)
    {


    } // retranslateUi

};

namespace Ui {
    class Form: public Ui_Form {};
} // namespace Ui

QT_END_NAMESPACE

然后我有一个 moc 文件生成了我的 moc 编译器,它们总是为所有 QObject 类生成的。它们是元数据工作所必需的,例如:信号。

我的 CMAKELists 看起来像

set (CMAKE_CXX_STANDARD 11)

include(FetchContent)

find_package(ADTF REQUIRED HINTS ${ADTF_DIR} COMPONENTS filtersdk ui)
adtf_use_qt(Widgets)

set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--export-all-symbols")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")

find_package(Eigen3 CONFIG REQUIRED HINTS ${CMAKE_SOURCE_DIR}/libs/eigen/share/eigen3/cmake)
set(OpenCV_STATIC ON)
find_package(OpenCV CONFIG REQUIRED HINTS ${CMAKE_SOURCE_DIR}/libs/opencv)

# include directories

    include_directories(inc)
    include_directories(${CMAKE_BINARY_DIR})
    include_directories(${OpenCV_INCLUDE_DIRS})
    include_directories(${EIGEN3_INCLUDE_DIR})
    
    # specify target
    set(TARGET_NAME dummy)
    set (TARGET_SOURCES
      inc/uic_dummy.h->file generated by uic compiler. this code snipper is shown above
      inc/dummy.h -> this basically contains several methods like createview and severalt hings and also slots (for qt functionality)
      inc/stdafx.h -> contains all includes in this file
        src/dummy.cpp->definitions of all the functions mentioned in dummy.h
    
        src/moc_dummy.cpp-> moc file everytime i need to keep in the sources list. However i want to generate it in build time instead of saying like this uisng CMAKE_AUTOMOC ON
    )
    
    adtf_add_filter(${TARGET_NAME} ${TARGET_SOURCES})
    target_link_libraries(${TARGET_NAME} PRIVATE Qt5::Widgets ${OpenCV_LIBS} adtf::ui)
    adtf_disable_qt_warnings(${TARGET_NAME})
    
    adtf_install_target(${TARGET_NAME} ${CMAKE_INSTALL_PREFIX}/dummy/dummy)
    
    adtf_create_plugindescription(
    /*removed intentionall as it is not the area of the concern in this scenario*/
    
    )

所以我的问题是我可以在构建时生成 moc 文件吗?

由于我正在使用 adtf_use_qt(widgets) 并且在 cmake gui 中我指出 qt_dir 那么一切正常。但是我需要将 moc 文件保存在源代码中。

Normally when i use qt withouf adtf i basicall use find_package(qt) and also use 
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE AUTORCC ON)
Then i will only keep my ui file in the sources and then it generates 
everyhting forme. Ideally i would like to know if i can do the same for adtf.

如果我的问题不清楚,请原谅。如果您需要更多信息,请询问我会尝试以比这更好的方式详细说明。谢谢

标签: adtfadtf3

解决方案


您还在为创建 moc 文件而烦恼吗?也许您只需将您的xxx.ui文件添加到过滤器源。ui_xxx.h可以包含在您的源文件中。

例如:

set(PLUGIN_TARGETNAME your_filter)

adtf_add_filter(${PLUGIN_TARGETNAME}
    ${PLUGIN_TARGETNAME}.h
    ${PLUGIN_TARGETNAME}.cpp
    ${PLUGIN_TARGETNAME}.ui
)

target_link_libraries(${PLUGIN_TARGETNAME} PRIVATE
    adtf::systemsdk
    adtf::filtersdk
    adtf::ui)

set_target_properties(${PLUGIN_TARGETNAME} PROPERTIES FOLDER src)

adtf_create_plugindescription(
    TARGET  ${PLUGIN_TARGETNAME}
    PLUGIN_SUBDIR "bin")

set_target_properties(${PLUGIN_TARGETNAME} 
    PROPERTIES AUTOMOC ON AUTOUIC ON AUTOGEN_TARGET_DEPENDS ${PLUGIN_TARGETNAME}.ui)


推荐阅读