在 PySide2 中协调绘制线和点
Drawing line and points in PySide2 with coordination
我想用 PySide2 画一些线和点,我按照文档提供了下面的代码,但是调用函数后它没有显示任何东西。
class Window2(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Deformed Structure")
self.DrawWindows = QtGui.QWindow()
self.DrawButton23 = QPushButton('Draw', self)
self.DrawButton23.setStyleSheet("Background-color: orange")
self.DrawButton23.move(100, 200)
self.DrawButton23.show()
self.DrawButton23.clicked.connect(self.PaintEvent)
def PaintEvent(self, painter):
painter = QtGui.QPainter()
painter.begin(self)
pen = QPen(Qt.green)
painter.setPen(pen)
for i in range(0, 10):
x0 = i * 30
y0 = i * 30
x1 = 100 + i * 50
y1 = 100 + i * 50
point1 = QPointF(x0, y0)
point2 = QPointF(x1, y1)
line1 = QLineF(point1, point2)
painter.drawPoint(point1)
painter.drawLine(line1)
print("OK123") #Just to check the loop, it prints 10 time
painter.end()
你必须明白:
Python 和 C++ 区分大小写,因此 paintEvent 与 PaintEvent 不同。
您不应直接调用 paintEvent,而应使用 update() 或 repaint() 方法。
按我的理解是你想要在按下按钮的时候执行绘画但是你不能直接控制绘画,逻辑是通过一些标志激活绘画的某个部分.
综合以上,解决方案是:
from PySide2 import QtCore, QtGui, QtWidgets
class Window2(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Deformed Structure")
self.flag = False
self.draw_button = QtWidgets.QPushButton("Draw", self)
self.draw_button.setStyleSheet("Background-color: orange")
self.draw_button.move(100, 200)
self.draw_button.clicked.connect(self.on_clicked)
def on_clicked(self):
self.flag = True
self.update()
def paintEvent(self, event):
painter = QtGui.QPainter(self)
if self.flag:
pen = QtGui.QPen(QtCore.Qt.green)
painter.setPen(pen)
for i in range(0, 10):
x0 = i * 30
y0 = i * 30
x1 = 100 + i * 50
y1 = 100 + i * 50
point1 = QtCore.QPointF(x0, y0)
point2 = QtCore.QPointF(x1, y1)
line1 = QtCore.QLineF(point1, point2)
painter.drawPoint(point1)
painter.drawLine(line1)
if __name__ == "__main__":
app = QtWidgets.QApplication()
w = Window2()
w.show()
app.exec_()
我想用 PySide2 画一些线和点,我按照文档提供了下面的代码,但是调用函数后它没有显示任何东西。
class Window2(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Deformed Structure")
self.DrawWindows = QtGui.QWindow()
self.DrawButton23 = QPushButton('Draw', self)
self.DrawButton23.setStyleSheet("Background-color: orange")
self.DrawButton23.move(100, 200)
self.DrawButton23.show()
self.DrawButton23.clicked.connect(self.PaintEvent)
def PaintEvent(self, painter):
painter = QtGui.QPainter()
painter.begin(self)
pen = QPen(Qt.green)
painter.setPen(pen)
for i in range(0, 10):
x0 = i * 30
y0 = i * 30
x1 = 100 + i * 50
y1 = 100 + i * 50
point1 = QPointF(x0, y0)
point2 = QPointF(x1, y1)
line1 = QLineF(point1, point2)
painter.drawPoint(point1)
painter.drawLine(line1)
print("OK123") #Just to check the loop, it prints 10 time
painter.end()
你必须明白:
Python 和 C++ 区分大小写,因此 paintEvent 与 PaintEvent 不同。
您不应直接调用 paintEvent,而应使用 update() 或 repaint() 方法。
按我的理解是你想要在按下按钮的时候执行绘画但是你不能直接控制绘画,逻辑是通过一些标志激活绘画的某个部分.
综合以上,解决方案是:
from PySide2 import QtCore, QtGui, QtWidgets
class Window2(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Deformed Structure")
self.flag = False
self.draw_button = QtWidgets.QPushButton("Draw", self)
self.draw_button.setStyleSheet("Background-color: orange")
self.draw_button.move(100, 200)
self.draw_button.clicked.connect(self.on_clicked)
def on_clicked(self):
self.flag = True
self.update()
def paintEvent(self, event):
painter = QtGui.QPainter(self)
if self.flag:
pen = QtGui.QPen(QtCore.Qt.green)
painter.setPen(pen)
for i in range(0, 10):
x0 = i * 30
y0 = i * 30
x1 = 100 + i * 50
y1 = 100 + i * 50
point1 = QtCore.QPointF(x0, y0)
point2 = QtCore.QPointF(x1, y1)
line1 = QtCore.QLineF(point1, point2)
painter.drawPoint(point1)
painter.drawLine(line1)
if __name__ == "__main__":
app = QtWidgets.QApplication()
w = Window2()
w.show()
app.exec_()