Results 1 to 2 of 2

Thread: Drawing Lines In Real Time (PyQt)

  1. #1
    Join Date
    Apr 2016
    Qt products
    Qt4 Qt5
    MacOS X Unix/X11

    Default Drawing Lines In Real Time (PyQt)

    I'm making an application and I want to connect certain items in the QGraphicsScene by using lines. I want this done in real time, so while I'm holding the mouse down and trying to draw the line I want to see the line being drawn. I've written some code to allow this but the problem with this code is that it keeps on clearing everything and I dont know a way around this. Any help would be much appreciated.

    class graphicsScene(QtGui.QGraphicsScene, QtGui.QWidget):
    self.pen = QtGui.QPen(, 3, QtCore.Qt.SolidLine)

    def mousePressEvent(self, event):
    if connectLine_cs == 1:
    self.cursorStartPosition = event.scenePos()
    self.start = QtCore.QPoint(self.cursorStartPosition.x(), rsorStartPosition.y())

    def mouseMoveEvent(self, event):
    if connectLine_cs == 1:
    self.cursorCurrentPosition = event.scenePos()
    current = QtCore.QPointF(self.cursorCurrentPosition.x(),self .cursorCurrentPosition.y())
    link = QtGui.QGraphicsLineItem(QtCore.QLineF(self.start, current))

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 627 Times in 617 Posts
    Qt products
    Unix/X11 Windows Android

    Default Re: Drawing Lines In Real Time (PyQt)

    You realize that your call to self.clear() is of course deleting all of the items from your scene every time you move the mouse, right?

    One way to do what you want is to use QRubberBand. The other is to start and add a new line item to the scene on mouse down (and remember it), update that same line's endpoint on mouse move, and then stop updating on mouse up.

    Please use [CODE] tags when posting code to make it more readable. Click "Go advanced" then click the "#" icon to insert a pair of tags, and put your code between them. This is especially important for Python where formatting and indentation make all the difference.

Similar Threads

  1. Drawing lines on qwtpolarplot
    By jerrychan in forum Qwt
    Replies: 3
    Last Post: 12th June 2013, 10:55
  2. Qwt real time
    By oddytz1989 in forum Qwt
    Replies: 3
    Last Post: 10th February 2012, 04:41
  3. Real time QT application?
    By marc2050 in forum Newbie
    Replies: 1
    Last Post: 8th June 2011, 06:08
  4. Replies: 0
    Last Post: 1st July 2010, 18:22
  5. Removing and Redrawing Lines in a QGraphicsScene in PyQT
    By giverson in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2007, 20:23

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.