如何绘制子窗口小部件?

时间:2017-09-29 09:01:55

标签: python qt5 pyqt5

所以我有Ui_MainWindow生成的pyuic5类。

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

# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# 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(800, 450)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.canvas = QtWidgets.QWidget(self.centralWidget)
        self.canvas.setGeometry(QtCore.QRect(70, 30, 621, 341))
        self.canvas.setAutoFillBackground(False)
        self.canvas.setObjectName("canvas")
        self.gview = QtWidgets.QGraphicsView(self.canvas)
        self.gview.setGeometry(QtCore.QRect(80, 50, 256, 192))
        self.gview.setObjectName("gview")
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menuBar.setObjectName("menuBar")
        self.menuhola = QtWidgets.QMenu(self.menuBar)
        self.menuhola.setObjectName("menuhola")
        self.menust = QtWidgets.QMenu(self.menuBar)
        self.menust.setObjectName("menust")
        MainWindow.setMenuBar(self.menuBar)
        self.menuBar.addAction(self.menuhola.menuAction())
        self.menuBar.addAction(self.menust.menuAction())

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menuhola.setTitle(_translate("MainWindow", "hola"))
        self.menust.setTitle(_translate("MainWindow", "splendid"))

我想画一些东西,只要它不是主窗口。但是,无论我尝试什么,它都无法运作。

这是我的代码:

import sys

from PyQt5 import QtWidgets
from PyQt5.QtGui import QPainter
from src.qt_design.main_window import Ui_MainWindow


class SystemApp(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def paintEvent(self, event):
        # Nope
        painter = QPainter(self.ui.gview)
        painter.drawLine(0, 0, 50, 50)

        # Nope
        painter = QPainter(self.ui.canvas)
        painter.drawLine(0, 0, 50, 50)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = SystemApp()
    window.show()
    sys.exit(app.exec_())

我不确定我是否应该: 1.在孩子身上涂上这个父母(就像我上面尝试的那样) 2.覆盖孩子们的paintEvent,但这可能是一个坏主意,请参阅here

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以通过事件过滤器获得您想要完成的任务

class SystemApp(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.gview.installEventFilter(self)
        self.ui.canvas.installEventFilter(self)

    def eventFilter(self, obj, e):
        if e.type() == QtCore.QEvent.Paint:
            painter = QPainter()
            painter.begin(obj)
            if obj == self.ui.canvas:
                painter.setPen(QtCore.Qt.red)  # Some specific painting
            painter.drawLine(0, 0, 50, 50)
            painter.end()
            return True
        return super().eventFilter(obj, e)