PYQT5画线[重复]
Posted
技术标签:
【中文标题】PYQT5画线[重复]【英文标题】:PYQT5 drawing line [duplicate] 【发布时间】:2019-03-09 14:41:33 【问题描述】:def __init__(self):
super().__init__()
self.title = 'Main menu'
self.left = 80
self.top = 80
self.width = 1500
self.height = 1000
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
painter = QPainter(self)
painter.setBrush(Qt.black)
painter.setPen(QPen(Qt.black, 2, Qt.SolidLine))
painter.drawLine(200,200,400,400)
self.show()
我想画一条直线,但它什么也没画。我怎么画它?
【问题讨论】:
【参考方案1】:void QWidget::paintEvent(QPaintEvent *event)
此事件处理程序可以在子类中重新实现,以接收事件中传递的绘制事件。
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Widget(QWidget):
def __init__(self):
super().__init__()
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(Qt.red)
painter.drawLine(10,10,100,140)
painter.setPen(Qt.blue)
painter.drawRect(120,10,80,80)
rectf = QRectF(230.0,10.0,80.0,80.0)
painter.drawRoundedRect(rectf,20,20)
p1 = [QPoint(10,100),QPoint(220,110),QPoint(220,190)]
painter.drawPolyline(QPolygon(p1))
p2 = [QPoint(120,110),QPoint(220,110),QPoint(220,190)]
painter.drawPolygon(QPolygon(p2))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Widget()
ex.resize(400, 280)
ex.show()
sys.exit(app.exec_())
【讨论】:
以上是关于PYQT5画线[重复]的主要内容,如果未能解决你的问题,请参考以下文章