How to edit QtDesigner widget class? [duplicate]

左心房为你撑大大i 提交于 2019-12-11 17:56:09

问题


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

来源:https://stackoverflow.com/questions/54389669/how-to-edit-qtdesigner-widget-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!