pyqt如何在已设置ui窗口里使用qpainter绘图?

ui窗口是用qtdesigner做的,通过pyuic转换成了.py文件,如何在.py中使用Qpainter进行绘图?
代码如下
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPainter, QPen

class Ui_Form(object):

def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(400, 300)

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

def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Form"))

def paintEvent(self, e):
    qp = QPainter()
    qp.begin(self)
    self.drawLines(qp)
    qp.end()

def drawLines(self, qp):
    pen = QPen(Qt.black, 2, Qt.SolidLine)

    qp.setPen(pen)
    qp.drawLine(120, 120, 120, 210)

    qp.setPen(pen)
    qp.drawLine(135, 94, 120, 120)

    qp.setPen(pen)
    qp.drawLine(135, 94, 150, 120)

    qp.setPen(pen)
    qp.drawLine(150, 120, 150, 210)

    qp.setPen(pen)
    qp.drawLine(120, 210, 150, 210)

if name == "__main__":

import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
阅读 1.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏