How to edit QtDesigner widget class? [duplicate]

2019-08-28 06:55发布

问题:

This question already has an answer here:

  • How do I use promote to in Qt Designer in pyqt4? 1 answer
  • Customising code of Qt designer widget? 1 answer

I'm using PyQt5 Designer for my project. I converted UI file into py with pyuic and imported it into my code. Right now I'm struggling with editing class that was made by QtDesigner for my widget.

I'm trying to create new class(Canvas) and inherit it from App.canvas class that is written in test.py(pyuic file) and then edit it, but when I use my own widget for Canvas it seems like QtDesigner's widget overlaps mine, as if I didn't create any class. How do I properly edit QtDesigner's class for my widgets?

# -*- 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_())