GitHub - PyQt5/QtDesignerPlugins: Qt Designer PluginsQt Designer Plugins. Contribute to PyQt5/QtDesignerPlugins development by creating an account on GitHub.https://github.com/PyQt5/QtDesignerPlugins
一、下载客户端
https://github.com/PyQt5/QtDesigner/releaseshttps://github.com/PyQt5/QtDesigner/releases
二、下载插件
https://github.com/PyQt5/QtDesignerPlugins/releaseshttps://github.com/PyQt5/QtDesignerPlugins/releases
三、解压
四、 打开客户端
五、自定义控件库
为防止出错,建议直接复制这个文件,进行替换修改。
新建一个demo_button_plugin.py文件,内容如下:
#!/usr/bin/env python"""
为 Qt Designer 提供的测试小部件插件。
"""
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QIcon, QPixmap, QPainter, QImage
from PyQt5.QtDesigner import QPyDesignerCustomWidgetPlugin
from PyQt5.QtSvg import QSvgRenderer
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout# 我的小部件控件
class PyDemoButton(QWidget):"""自定义按钮"""def __init__(self, parent=None):super().__init__(parent)layout = QVBoxLayout()self.button = QPushButton("Click Me")layout.addWidget(self.button)self.setLayout(layout)# 测试小部件
if __name__ == "__main__":import sysapp = QApplication(sys.argv)clock = PyDemoButton()clock.show()sys.exit(app.exec_())# 插件注册
# TODO
class PyDemoButtonPlugin(QPyDesignerCustomWidgetPlugin):"""通过实现特定于 PyQt 的自定义插件类来为 Qt Designer 提供一个 Python 自定义插件。"""# __init__() 方法仅用于设置插件并定义其初始化变量。def __init__(self, parent=None):# TODOsuper(PyDemoButtonPlugin, self).__init__(parent)self.initialized = False# initialize() 和 isInitialized() 方法允许插件设置任何所需的资源,确保每个插件只能执行一次此操作。def initialize(self, core):if self.initialized:returnself.initialized = Truedef isInitialized(self):return self.initialized# 此工厂方法创建具有适当父级的新自定义小部件实例。def createWidget(self, parent):# TODOreturn PyDemoButton(parent)# 此方法返回此插件提供的自定义小部件类的名称。def name(self):# TODOreturn "PyDemoButton"# 返回此小部件所属的 Qt Designer 小部件框中的组名称。def group(self):# TODOreturn "PyQt Examples"# 返回用于在 Qt Designer 的小部件框中表示自定义小部件的图标。def icon(self):# TODOreturn QIcon(_logo_pixmap)# 返回一个简短的描述,作为自定义小部件的工具提示。鼠标悬停在空间盒中的控件时显示。def toolTip(self):return "这一个自定义的按钮"# 返回一个简短的描述,作为“这是什么?”帮助消息中的自定义小部件。def whatsThis(self):return "更加华丽的按钮"# 如果自定义小部件充当其他小部件的容器,则返回 True;否则返回 False。请注意,自定义容器的插件还需要提供 QDesignerContainerExtension 接口的实现,以便向 Qt Designer 添加自定义编辑支持。def isContainer(self):return False# 返回一个 XML 描述,描述自定义小部件实例的默认属性值。每个由此插件创建的自定义小部件都将使用此描述进行配置,将会呈现在Qt Designer的控件属性面板编辑器。def domXml(self):return '<widget class="PyDemoButton" name="demoButton">\n' \' <property name="toolTip">\n' \' <string>自定义小按钮</string>\n' \' </property>\n' \' <property name="whatsThis">\n' \' <string>一个好的按钮。</string>\n' \' </property>\n' \'</widget>\n'# 返回包含自定义小部件类的模块。它可以包括模块路径。转换成 Python 代码后,以此路径导入自定义小部件类。def includeFile(self):return "demoButton"# SVG图标 TODO
svg_content = """<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1729845139063" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="24308" xmlns:xlink="http://www.w3.org/1999/xlink" width="256" height="256"><path d="M762.77551 929.959184H261.22449c-40.75102 0-74.187755-33.436735-74.187755-74.710204v-288.914286c0-6.791837-5.22449-12.016327-11.493878-12.016327h-27.689796c-21.942857 0-40.75102-12.538776-49.110204-32.914285-8.359184-19.853061-3.657143-42.840816 11.493878-58.514286l341.159183-343.771429c15.673469-16.195918 37.616327-25.077551 60.604082-25.077551s44.930612 8.881633 61.126531 25.6L914.285714 463.412245c15.15102 15.673469 19.853061 38.661224 11.493878 58.514286-8.359184 20.37551-27.167347 32.914286-49.110204 32.914285h-27.689796c-6.269388 0-11.493878 5.22449-11.493878 12.016327v288.914286c-0.522449 40.75102-33.959184 74.187755-74.710204 74.187755zM512 135.836735c-12.016327 0-22.987755 4.702041-31.346939 13.061224L139.493878 492.669388c-5.22449 5.22449-3.657143 10.971429-2.612245 13.061224 1.044898 2.089796 3.657143 7.314286 10.971428 7.314286h27.689796c29.257143 0 53.289796 24.032653 53.289796 53.812245v288.914286c0 17.763265 14.628571 32.914286 32.391837 32.914285h501.55102c17.763265 0 32.391837-14.628571 32.391837-32.914285v-288.914286c0-29.779592 24.032653-53.812245 53.289796-53.812245h27.689796c6.791837 0 9.926531-5.22449 10.971428-7.314286 1.044898-2.089796 2.612245-7.836735-2.612245-13.061224L543.346939 148.897959c-8.359184-8.359184-19.330612-13.061224-31.346939-13.061224z" fill="#555555" p-id="24309"></path><path d="M663.510204 929.959184H360.489796v-219.428572c0-45.97551 37.616327-83.591837 83.591837-83.591836h135.836734c45.97551 0 83.591837 37.616327 83.591837 83.591836v219.428572z m-261.22449-41.795919h219.428572v-177.632653c0-22.987755-18.808163-41.795918-41.795919-41.795918H444.081633c-22.987755 0-41.795918 18.808163-41.795919 41.795918v177.632653z" fill="#555555" p-id="24310"></path></svg>"""
# 创建 QSvgRenderer 并加载 SVG 内容
svg_renderer = QSvgRenderer(svg_content.encode('utf-8'))
# 创建一个 QImage 用于渲染 SVG
image = QImage(QSize(svg_renderer.defaultSize()), QImage.Format_ARGB32)
image.fill(0) # 清空图像
# 创建一个 QPainter 用于绘制 SVG
painter = QPainter(image)
svg_renderer.render(painter)
painter.end()
# 将 QImage 转换为 QPixmap
_logo_pixmap = QPixmap.fromImage(image)
六、将demo_button_plugin.py文件放入Qt Designer控件库中
八 .重启Qt Designer