简体   繁体   English

如何使用cmake(和visual studio)为qt设计器创建自定义(小部件)插件

[英]how do i create a custom (widget) plugin for qt designer with cmake ( and visual studio )

The amount of tutorials, how to create a qt designer plugin is very thin..and the ones i found always use qt creator ( like this one : http://qt-project.org/doc/qt-4.8/designer-customwidgetplugin.html ). 教程的数量,如何创建一个qt设计器插件是非常薄的...我发现总是使用qt创建者(像这样: http//qt-project.org/doc/qt-4.8/designer-customwidgetplugin .html )。 Where i have to add some qt definitions in the .pro file 我必须在.pro文件中添加一些qt定义

 CONFIG      += designer plugin

I use CMake and Visual Studio for coding, so it would be really awesome if someone could tell me how i successfully create a .dll that i can put in the plugins/designer folder to have the custom widget show up in Qt Designer 我使用CMake和Visual Studio进行编码,所以如果有人能告诉我如何成功创建一个.dll,我可以放入插件/设计器文件夹以使自定义小部件显示在Qt Designer中,这真的很棒

Disclaimer: I know this is an old question but Even now I didn't find complete resources on how to do it. 免责声明:我知道这是一个老问题,但即使是现在我也没有找到完整的资源来解决这个问题。

I can't answer you for the Visual Studio part since I build on the (windows) command line, but here is my cmake. 因为我构建在(windows)命令行上,所以我无法回答你的Visual Studio部分,但这是我的cmake。

I assume you have already created the following files related to the plugin, ie: 我假设您已经创建了与插件相关的以下文件,即:

  • widget.h widget.h
  • widget.cpp widget.cpp
  • widget.ui widget.ui
  • widgetPlugin.h -> QDesignerCustomWidgetInterface class widgetPlugin.h - > QDesignerCustomWidgetInterface
  • widgetPlugin.cpp widgetPlugin.cpp

And that you want to create a library with multiple plugins, ie created the related files: 并且您想要创建一个包含多个插件的库,即创建相关文件:

  • plugins.h -> QDesignerCustomWidgetCollectionInterface class plugins.h - > QDesignerCustomWidgetCollectionInterface
  • plugins.cpp plugins.cpp

The content of the files simply follow what's in the tutorials. 文件的内容只是遵循教程中的内容。

The CMakeLists.txt is: 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
)

To reload the plugins in QtDesigner, go to Help > About Plugins > Reload. 要在QtDesigner中重新加载插件,请转到“帮助”>“关于插件”>“重新加载”。

Then in the other CMakeLists.txt, I didn't want to include the library since there are also useless *Plugin files. 然后在其他CMakeLists.txt中,我不想包含该库,因为还有无用的* Plugin文件。 So I included again the files I wanted : 所以我再次包含了我想要的文件:

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

Hope you'll find it useful ! 希望你会发现它很有用!

Just use the cmake's qt auto tools and add all sources(.cpp .ui .qrc etc) to the target as follow: 只需使用cmake的qt auto工具并将所有源(.cpp .ui .qrc等)添加到目标,如下所示:

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt4 REQUIRED QtCore QtGui QtDesigner #...
)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})

add_library(someplugin SHARED
    path/to/plugin/someplugin.cpp
    path/to/plugin/someplugin.qrc
    path/to/plugin/someplugin.ui
    path/to/plugin/s/other/src.cpp
    #...
)

target_link_libraries(someplugin ${QT_LIBRARIES})

install(TARGETS someplugin
    DESTINATION ${QT_PLUGINS_DIR}/designer
)

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

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