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 ?
So scenario is this :
1) Used paintEvent to draw a 2 circles with black colour
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 ?
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 ?
It is a much better idea to do all your painting in the paintEvent
handler.
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
.
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. This will schedule a paint event that will execute when the program returns to the event loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.