Disclaimer: So che questa è una vecchia domanda ma anche ora non ho trovato le risorse complete su come farlo.
Non riesco a rispondere per la parte di Visual Studio da quando creo sulla riga di comando (Windows), ma qui è il mio cmake.
Presumo che hanno già creato i seguenti file relativi al plugin, vale a dire:
- widget.h
- widget.cpp
- widget.ui
- widgetPlugin.h ->
QDesignerCustomWidgetInterface
classe
- widgetPlugin.cpp
A ND che si desidera creare una biblioteca con più plugin, cioè ha creato i file correlati:
- plugins.h ->
QDesignerCustomWidgetCollectionInterface
classe
- plugins.cpp
Il contenuto dei file è sufficiente seguire cosa c'è nelle esercitazioni.
Il 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
)
per ricaricare i plugin in QtDesigner, andare ad aiutare> Informazioni sui plugin> Ricarica.
Quindi nell'altro CMakeLists.txt, non volevo includere la libreria poiché ci sono anche dei file plugin inutili. Quindi ho incluso di nuovo i file che volevo:
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}
)
Spero che lo troverai utile!