2013-05-10 76 views
5

教程的數量,如何創建一個qt設計器插件是非常薄...我發現的那些總是使用qt創作者(像這樣:http://qt-project.org/doc/qt-4.8/designer-customwidgetplugin.html)。在那裏我有在.pro文件我如何創建一個自定義(小部件)插件qt設計師與cmake(和視覺工作室)

CONFIG  += designer plugin 

我使用CMake和Visual Studio中的編碼,所以這將是真正真棒如果有人能告訴我怎樣成功地創建一個.dll,我可以添加一些QT定義放入插件/設計器文件夾,讓自定義小部件顯示在Qt設計器中

回答

4

聲明:我知道這是一個老問題,但即使現在我沒有找到完整的資源如何做到這一點。

因爲我建立在(Windows)命令行上,所以我無法回答你的Visual Studio部分,但這裏是我的cmake。

我假定你已經創建相關的插件以下文件,即:

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

A第二要創建具有多個插件庫,即創建的相關文件:

  • plugins.h - >QDesignerCustomWidgetCollectionInterface
  • plugins.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中,我不想包含庫,因爲也有無用的*插件文件。所以我再次包括我想要的文件:

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

希望你會發現它很有用!

0

只需使用的cmake的QT汽保工具,並添加所有源(的.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 
) 
相關問題