如何正确使用QtDesigner中的“升级为”功能?

时间:2019-01-27 17:03:24

标签: python pyqt5 qt-designer

我正在尝试将对象提升为QtDesigner中的自定义窗口小部件。似乎不起作用。

我运行project.py时说

builtins.NameError: name 'Canvas' is not defined(in test.py),

但是该类是导入的,我可以在生成的pyuic代码(test.py)的末尾看到它:

from project import Canvas

这是project.py

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

class Canvas(QWidget):
    def __init__(self):
        super().__init__()
        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_())

这是test.py(它生成了,我一点都没碰过):

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'project.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(912, 715)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.sidebar = QtWidgets.QWidget(self.centralwidget)
        self.sidebar.setMinimumSize(QtCore.QSize(388, 0))
        self.sidebar.setAutoFillBackground(True)
        self.sidebar.setObjectName("sidebar")
        self.clearButton = QtWidgets.QPushButton(self.sidebar)
        self.clearButton.setGeometry(QtCore.QRect(20, 30, 91, 41))
        self.clearButton.setObjectName("clearButton")
        self.brushSizeSlider = QtWidgets.QSlider(self.sidebar)
        self.brushSizeSlider.setGeometry(QtCore.QRect(20, 120, 160, 22))
        self.brushSizeSlider.setOrientation(QtCore.Qt.Horizontal)
        self.brushSizeSlider.setObjectName("brushSizeSlider")
        self.brushSizeLabel = QtWidgets.QLabel(self.sidebar)
        self.brushSizeLabel.setGeometry(QtCore.QRect(20, 90, 101, 16))
        self.brushSizeLabel.setObjectName("brushSizeLabel")
        self.brushColorDial = QtWidgets.QDial(self.sidebar)
        self.brushColorDial.setGeometry(QtCore.QRect(20, 180, 50, 64))
        self.brushColorDial.setObjectName("brushColorDial")
        self.brushColorLabel = QtWidgets.QLabel(self.sidebar)
        self.brushColorLabel.setGeometry(QtCore.QRect(20, 160, 101, 16))
        self.brushColorLabel.setObjectName("brushColorLabel")
        self.horizontalLayout.addWidget(self.sidebar)
        self.canvas = Canvas(self.centralwidget)
        self.canvas.setMinimumSize(QtCore.QSize(500, 500))
        self.canvas.setMaximumSize(QtCore.QSize(5000, 5000))
        self.canvas.setBaseSize(QtCore.QSize(500, 500))
        self.canvas.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
        self.canvas.setAutoFillBackground(True)
        self.canvas.setStyleSheet("canvas{background-color: rgb(70, 70, 50);}")
        self.canvas.setObjectName("canvas")
        self.horizontalLayout.addWidget(self.canvas)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.clearButton.setText(_translate("MainWindow", "Clear canvas"))
        self.brushSizeLabel.setText(_translate("MainWindow", "Current brush size:"))
        self.brushColorLabel.setText(_translate("MainWindow", "Current brush color:"))

from project import Canvas

0 个答案:

没有答案