zoukankan      html  css  js  c++  java
  • QT5中如何自定义窗口部件

    提升法

    eg.(定义一个新的QLable部件)
    1、定义一个类
    class Label : public base, public QLabel //可以支持多重继承
    2、在qt creator中打开ui编辑器,拖曳一个QLable兑现,提升,输入提升的类名Label,勾选全部包含,添加,提升成功。


    插件法
    Qt Assistance:Creating Custom Widgets for Qt Designer
    1、和提升法一样,也需要先建立一个新的部件类
    class AnalogClock : public QWidget
    2、建立一个插件类
    class AnalogClockPlugin : public QObject, public QDesignerCustomWidgetInterface
    {
        Q_OBJECT
        Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface" FILE "analogclock.json")
        Q_INTERFACES(QDesignerCustomWidgetInterface)
    public:
        explicit AnalogClockPlugin(QObject *parent = 0);
        //……
    };
    并在其cpp中实现其相关函数
    3、修改.pro文件
    ……
    CONFIG      += plugin
    CONFIG      += designer
    CONFIG      += debug_and_release
    TEMPLATE    = lib
    QT          += widgets designer
    SOURCES +=
        analogclock.cpp
        analogclockplugin.cpp
    HEADERS  +=
        analogclock.h
        analogclockplugin.h
    OTHER_FILES += analogclock.json
    target.path = $$[QT_INSTALL_PLUGINS]/designer

    INSTALLS += target

    ……

    4、在项目文件目录下建一个空的analogclock.json文件

    5、编译生成.dll文件,然后将其放置到C:QtQt5.2.05.2.0msvc2012_64_openglpluginsdesigner下就可以在qt designer中使用该自定义插件了。

    注:如果需要在qt Creator中使用该插件,由于Qt SDK for Windows的两部分是由不同编译环境编译而成,QtCreator是由msvc编译,Qt库是由mingw编译,所以还需要将其复制到C:QtQt5.2.0ToolsQtCreatorinpluginsdesigner,还要解决方法有以下几种:

    1) 下载QT Creator的源码然后在QT Creator中用MinGW编译
    2) 将插件在Visual Studio下编译Build the plugin with Visual Studio
    3) 编译QT Creator源码,但将build key checking关掉

    之后就可以在 QtCreator中打开项目的界面文件(*.ui),此时QtCreator允许你使用集成的QtDesigner来编辑这个ui文件,然后打开菜单项“工具->界面编辑器->About Qt Designer plugins...”即可查看哪些插件加载成功了,哪些未加载成功(在单独运行的QtDesigner中,打开“帮助->关于插件”菜单也可查看插件加载成功与否)。

  • 相关阅读:
    SVG.js 文本绘制整理
    SVG.js 基础图形绘制整理(二)
    SVG.js 基础图形绘制整理(一)
    C# 异步编程Task整理(一)
    Svg.Js 父类的基础操作
    Svg.Js A标签,链接操作
    Svg.Js 简介(转)
    SVG 相关整理
    Kendo UI
    Kendo UI
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/5105406.html
Copyright © 2011-2022 走看看