Important: Please read the Qt Code of Conduct -

Dynamic preview while drawing

  • Hi,

    I am making an app that enables drawing shapes using mouse. I use QGraphicsScene and I add Items to it and then the modified scene is being displayed by QGraphicsView. Everything works fine but I want to extend the program and add dynamic preview of an item the is being currently drawn. Id like to achive the same effect as it is in MS Paint or any graphic editor. I found many tutorials how to do it with paintEvent and QPainter but this approach isn`t suitable in my case. How can I do this with QGraphicsScene? I have overwritten MousePressEvent so it catches position of the cursor - 1st point of my item (e.g. line). MouseMoveEvent does the same but with the last point. That 2 points go to the function that draws line based on them. With this approach I get many lines, because everytime the cursor position is changed another line is being drawn. I tried to remove them so only the last one (the newest) is visible but I cannot do it right. I can delete them all or I cannot delete them at all. I want to have preview of a line while I press and hold LMB and move mouse cursor. After releasing the button I need the line to be added permanently to the scene. Can you please give me so ideas how can I solve this problem? I use pyqt 4 and python 3.4 .