[英]Create a line in PyQt Window by Mouse Events PyQt
I am trying to create a scenario where I need to draw line from the mousePressEvent
position till the latest mouse moveposition
which means i need to call paintEvent
from mousePressEvent
,Is it possible ? 我正在尝试创建一个场景,我需要从
mousePressEvent
位置绘制直到最新的鼠标moveposition
位置,这意味着我需要从mousePressEvent
调用paintEvent
,这可能吗?
So scenario is this : 所以场景是这样的:
1) Used paintEvent to draw a 2 circles with black colour 1)使用paintEvent绘制2个黑色圆圈
2) Mouse press event waits for a event and press happens , I want to change the colour of the circle to green , is it possible ? 2)鼠标按下事件等待事件并按下发生,我想将圆圈的颜色更改为绿色,是否可能?
import sys, random
from PyQt4 import QtGui, QtCore
class P(QtGui.QWidget):
def __init__(self):
super(P, self).__init__()
self.initUI()
def initUI(self):
q=self.frameGeometry()
cp=QtGui.QDesktopWidget().availableGeometry().center()
q.moveCenter(cp)
self.setFixedSize(300,300)
self.setWindowTitle('Points')
self.show()
def mousePressEvent(self, QMouseEvent):
cursor =QtGui.QCursor(self)
position = QMouseEvent.pos()
xpos = QMouseEvent.x()
ypos = QMouseEvent.y()
#Trial ??????
q = QtGui.QPainter()
q.drawLine(30,30,90,90)
print QMouseEvent.pos()
def mouseReleaseEvent(self, QMouseEvent):
cursor =QtGui.QCursor()
print cursor.pos()
def paintEvent(self,e):
qp = QtGui.QPainter()
qp.begin(self)
E1 = qp.drawEllipse(30,30,20,20)
E2 = qp.drawEllipse(30,130,20,20)
def main():
app = QtGui.QApplication(sys.argv)
ex = P()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
In Simple words I need to know can we call one Event from another , ie Paint Event from Mouse press event ? 简单来说,我需要知道我们可以从另一个事件中调用一个事件,即从鼠标按下事件中的Paint事件吗?
It is a much better idea to do all your painting in the paintEvent
handler. 在
paintEvent
处理程序中完成所有绘制是一个更好的主意。
You should use your mouse event handlers to handle the collection of data (starting points, lengths, etc) and then do the actual repainting in the paintEvent
. 您应该使用鼠标事件处理程序来处理数据集合(起始点,长度等),然后在
paintEvent
进行实际重新绘制。
Once you've collected the new data in the mouse event handlers, you can tell the QWidget that it needs to repaint by calling update
function. 一旦在鼠标事件处理程序中收集了新数据,就可以通过调用
update
函数告诉QWidget需要重新绘制它。 This will schedule a paint event that will execute when the program returns to the event loop. 这将调度将在程序返回事件循环时执行的绘制事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.