gpt4 book ai didi

c++ - 我如何使用 cmake(和 visual studio)为 qt designer 创建自定义(小部件)插件

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:28 26 4
gpt4 key购买 nike

关于如何创建 qt designer 插件的教程数量非常少……我发现的那些总是使用 qt creator(比如这个:http://qt-project.org/doc/qt-4.8/designer-customwidgetplugin.html)。我必须在 .pro 文件中添加一些 qt 定义

 CONFIG      += designer plugin

我使用 CMake 和 Visual Studio 进行编码,所以如果有人能告诉我如何成功创建一个 .dll,我可以将其放入 plugins/designer 文件夹,让自定义小部件显示在 Qt Designer 中,那将是非常棒的

最佳答案

免责声明:我知道这是一个老问题,但即使是现在我也没有找到关于如何做的完整资源。

由于我是在 (windows) 命令行上构建的,所以我无法回答 Visual Studio 部分的问题,但这是我的 cmake。

我假设您已经创建了以下与插件相关的文件,即:

  • 小部件.h
  • 小部件.cpp
  • widget.ui
  • widgetPlugin.h -> QDesignerCustomWidgetInterface
  • widgetPlugin.cpp

并且您想创建一个包含多个插件的库,即创建相关文件:

  • plugins.h -> QDesignerCustomWidgetCollectionInterface
  • 插件.cpp

文件内容按照教程内容即可。

CMakeLists.txt 是:

cmake_minimum_required(VERSION 2.8)

set(PROJECT Plugins)
project(${PROJECT})

# Needed to compile against ui and moc generated files
include_directories(${CMAKE_CURRENT_BINARY_DIR})

set(SOURCES
plugins.cpp
widgetPlugin.cpp
widget.cpp
)

set(HEADERS
plugins.h
widgetPlugin.h
widget.h
)

set(FORMS
widget.ui
)

# This is experimental, it works but it may be not optimal, don't hesitate to change this
find_package(Qt4 REQUIRED QtCore QtGui QtDesigner)
if (QT4_FOUND AND QT_QTCORE_FOUND AND QT_QTGUI_FOUND AND QT_QTDESIGNER_FOUND)
set(QT_USE_QTDESIGNER TRUE)
include(${QT_USE_FILE})
else()
message(FATAL_ERROR "no qt...")
endif()

qt4_wrap_cpp(HEADERS_MOC ${HEADERS})
qt4_wrap_ui(FORMS_HEADERS ${FORMS})
qt4_add_resources(RESOURCES_RCC ${RESOURCES})

# Here too, I'm not sure every define is necessary
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_definitions(-DQT_PLUGIN)
add_definitions(-DQT_NO_DEBUG)
add_definitions(-DQT_SHARED)
add_definitions(-DQDESIGNER_EXPORT_WIDGETS)

add_library(${PROJECT} SHARED
${SOURCES}
${HEADERS_MOC}
${FORMS_HEADERS}
${RESOURCES_RCC}
)
target_link_libraries(${PROJECT} ${QT_LIBRARIES})

# Install the library in QtDesigner plugin directory
install(TARGETS ${PROJECT}
DESTINATION ${QT_PLUGINS_DIR}/designer
)

要在 QtDesigner 中重新加载插件,请转到“帮助”>“关于插件”>“重新加载”。

然后在另一个 CMakeLists.txt 中,我不想包含该库,因为还有无用的 *Plugin 文件。所以我再次包含了我想要的文件:

cmake_minimum_required(VERSION 2.8)

set(PROJECT GPAUSX)
project(${PROJECT})

# Include the other CMakeLists.txt
subdirs(Plugins)
find_package(Qt4 REQUIRED)

# Files to insert
set(SOURCES
main.cpp
MainWindow.cpp
${Plugins_SOURCE_DIR}/widget.cpp

)
set(HEADERS
MainWindow.h
${Plugins_SOURCE_DIR}/widget.h

)
set(FORMS
MainWindow.ui
${Plugins_SOURCE_DIR}/widget.ui
)

qt4_wrap_cpp(HEADERS_MOC ${HEADERS})
qt4_wrap_ui(FORMS_HEADERS ${FORMS})
qt4_add_resources(RESOURCES_RCC ${RESOURCES})

include(${QT_USE_FILE})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_definitions(${QT_DEFINITIONS})
# I'm no expert in libraries so, intuitively I'd say this is useless but it won't compile if I don't define it.
# This clearly needs to get fixed.
add_definitions(-DQDESIGNER_EXPORT_WIDGETS)

# Possible variants making it compile :
# 1/ either include Plugins_BINARY_DIR or include .uis
# including the binary dir makes use of the already generated .uis
# 2/ either target Plugins or add Plugins .uis, .hs and .cpps with -DQDESIGNER_EXPORT_WIDGETS
# if you target plugins, make sure you compile with the same flags

add_executable(${PROJECT}
${SOURCES}
${HEADERS_MOC}
${FORMS_HEADERS}
${RESOURCES_RCC}
)
target_link_libraries(${PROJECT}
# Uncomment the following if you want to target Plugins
#Plugins
${QT_LIBRARIES}
)

希望你会发现它有用!

关于c++ - 我如何使用 cmake(和 visual studio)为 qt designer 创建自定义(小部件)插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16486366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com