How to install a custom widget in Qt?

Deadly 提交于 2020-06-27 18:46:09

问题


I have extended QListView to override its mouse events, and I wish to make that extension available as a widget on the Qt Creator visual design Widget Box.

I am following the analog clock example of building a custom widget. I managed to build it without errors. But how do I install it? Do I add a build step and append "install" to the make command?

The Qt docs merely mention, like, oh you could install just it or make install, without clearly explaining how to install a custom widget. How do I do a "make install" inside project build

In .NET, i.e. visual studio window forms, all I had to do was include the widget in my project.

Do I have to manually copy the .so or .dll files to a particular directory?

Could someone give me a succinct discrete clear explanation what I need to do to get the custom widget to appear in my Qt Creator widget box? I am not asking about how to write code and configuration to create a custom widget. I already have a custom widget. I just need to know what my next step should be.

I am using Ubuntu, so don't indulge in explaining why/how rebuild Creator on mingw for Windows.


回答1:


There is an easier approach. Just place a normal QListView in designer, right click, and "promote to". There, enter a classname for your extended widget.




回答2:


To use widget you only need include its module to project. To see widget in designer, you need to write plugin for qt designer.

I used cmake and linux, but I suppose this is also simmilar for other setups. In CMakeLists.txt I have:

ADD_LIBRARY(widgets_plugin SHARED
  foo.cpp
)
SET_TARGET_PROPERTIES(widgets_plugin PROPERTIES COMPILE_FLAGS "${QT_DEFINITIONS} -DQDESIGNER_EXPORT_WIDGETS -DQT_PLUGIN -DQT_NO_DEBUG -DQT_SHARED")

TARGET_LINK_LIBRARIES(widgets_plugin ${QT_LIBRARIES})
IF (NOT WIN32)
  ADD_CUSTOM_COMMAND(TARGET widgets_plugin POST_BUILD
      COMMAND rm -fr ${CMAKE_CURRENT_BINARY_DIR}/designer
      COMMAND mkdir ${CMAKE_CURRENT_BINARY_DIR}/designer
      COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/libwidgets_plugin.so ${CMAKE_CURRENT_BINARY_DIR}/designer/
 )
ENDIF (NOT WIN32)

After that you need define enviroment variable

 QT_PLUGIN_PATH="path/to/you/plugin"

and run qt designer in such way that it see this variable.

As I remember there is strange behaviour of qt designer on non windows machine: it not look at QT_PLUGIN_PATH, but use $QT_PLUGIN_PATH/designer, while on windows it looks at $QT_PLUGIN_PATH.



来源:https://stackoverflow.com/questions/8147395/how-to-install-a-custom-widget-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!