如何编辑QtDesigner小部件类?

时间:2019-01-27 15:19:24

标签: python pyqt pyqt5 qt-designer

我正在为我的项目使用PyQt5 Designer。我使用pyuic将UI文件转换为py并将其导入到我的代码中。现在,我正在努力解决QtDesigner为我的小部件创建的编辑类。

我正在尝试创建新类(Canvas)并从用test.py(pyuic文件)编写的App.canvas类继承它,然后对其进行编辑,但是当我将自己的窗口小部件用于Canvas时, QtDesigner的窗口小部件与我的窗口小部件重叠,好像我没有创建任何类一样。如何为我的窗口小部件正确编辑QtDesigner的类?

# -*- coding: utf-8 -*-
import sys
from PyQt5 import uic
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from test import Ui_MainWindow


class App(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.initUi()

    def initUi(self):
        w = Canvas(self)

class Canvas(QWidget):
    def __init__(self, App):
        super().__init__()
        App.canvas.__init__()
        # after this init my attributes make no difference
        self.setCursor(QCursor(Qt.CrossCursor))
        p = self.palette()
        p.setColor(self.backgroundRole(), Qt.white)
        self.setAutoFillBackground(True)
        self.setPalette(p)        
        self.path = QPainterPath()

    def paintEvent(self, event):
        painter = QPainter()
        painter.begin()
        painter.drawPath(self.path)
        painter.end()

    def mouseMoveEvent(self, event):
        self.path.lineTo(event.pos())
        update()

    def mousePressEvent(self, event):
        self.path.moveTo(event.pos())
        update()


application = QApplication(sys.argv)
example = App()
example.show()
sys.exit(application.exec_())

0 个答案:

没有答案