您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

我在哪里为Qt设计器中的单个升级QWidget编写类

我在哪里为Qt设计器中的单个升级QWidget编写类

在所有代码都出现错误之前:为什么需要从 QLabel和QPixmap?我不明白重点,QLabela QPixmap(合成),QLabel示例:新标签.py**

from PyQt5 import QtCore, QtWidgets


class NeuLabel(QtWidgets.QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setAcceptDrops(True)

    def mousePressEvent(self, e):
        super().mousePressEvent(e)
        if e.button() == QtCore.Qt.LeftButton:
            print("press")

然后在.ui中,您必须在表单中填写以下内容,按“添加”按钮,然后是“升级”按钮

正在生成以下.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="NeuLabel" name="label">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>160</y>
      <width>251</width>
      <height>191</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>NeuLabel</class>
   <extends>QLabel</extends>
   <header>neulabel</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

Then used in the main file:

import os
import sys

from PyQt5 import QtWidgets, uic

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

uifile_1 = os.path.join(CURRENT_DIR, "testpromote.ui")
form_1, base_1 = uic.loadUiType(uifile_1)


class myApp(base_1, form_1):
    def __init__(self):
        super(base_1, self).__init__()
        self.setupUi(self)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = myApp()
    ex.show()
    sys.exit(app.exec_())



├── main.py
├── neulabel.py
└── testpromote.ui
其他 2022/1/1 18:45:23 有380人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶