Results 1 to 7 of 7

Thread: Drawing shapes using GraphicsView, GraphicsScene and GraphicsItem(s)

  1. #1
    Join Date
    Mar 2010
    Location
    Brazil
    Posts
    39
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Drawing shapes using GraphicsView, GraphicsScene and GraphicsItem(s)

    Hi.

    If I want to create a kind of "Ms Paint" using GraphicsView, GraphicsScene and Items, which class I have to reimplement to get mousepress and mousemove events? GraphicsView or Scene?

    I tried to use Scene, but even with it in 600x500 WidthxHeight ever place that I click the coordinates are the same 0,0 and the follow code dont draw any thing, in this case, a line.

    Qt Code:
    1. from PyQt4.QtGui import *
    2. from PyQt4.QtCore import *
    3.  
    4. class GraphicsScene(QGraphicsScene):
    5. def __init__(self, parent = None):
    6. super(GraphicsScene, self).__init__(parent)
    7.  
    8. self.setSceneRect(0, 0, 600, 500)
    9. self.setBackgroundBrush(QColor(242, 251, 235))
    10.  
    11.  
    12. def mousePressEvent(self, e):
    13. self.pointBegin = self.pointEnd = e.pos()
    14.  
    15. self.line = QGraphicsLineItem(QLineF(self.pointBegin, self.pointEnd))
    16. self.line.setFlags(QGraphicsItem.ItemIsMovable)
    17.  
    18. self.addItem(self.line)
    19.  
    20. def mouseMoveEvent(self, e):
    21. self.pointEnd = e.pos()
    22.  
    23. self.line.setLine(QLineF(self.pointBegin, self.pointEnd))
    24.  
    25.  
    26. def mouseReleaseEvent(self, e):
    27. self.line.setLine(QLineF(self.pointBegin, self.pointEnd))
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Drawing shapes using GraphicsView, GraphicsScene and GraphicsItem(s)

    Have you looked in the docs?

  3. #3
    Join Date
    Mar 2010
    Location
    Brazil
    Posts
    39
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing shapes using GraphicsView, GraphicsScene and GraphicsItem(s)

    Yes, I did. The "e" param at mouseXEvent is all of that type.

    My question is: "...which class I have to reimplement to get mousepress and mousemove events?"

    Thank you anyway.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Drawing shapes using GraphicsView, GraphicsScene and GraphicsItem(s)

    My answer is, as described in the docs: change e.pos() to e.scenePos()

    And you're welcome.

  5. The following user says thank you to norobro for this useful post:

    HelderC (2nd October 2012)

  6. #5
    Join Date
    Mar 2010
    Location
    Brazil
    Posts
    39
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing shapes using GraphicsView, GraphicsScene and GraphicsItem(s)

    Sorry man, but I'm a little confuse.

    You want to say I have to reimplement the GraphicsView, instead GraphicsScene and use e.scenePos() instead e.pos() ??

    Is that correct?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing shapes using GraphicsView, GraphicsScene and GraphicsItem(s)

    Quote Originally Posted by HelderC View Post
    My question is: "...which class I have to reimplement to get mousepress and mousemove events?"
    It can be either QGraphicsView, QGraphicsScene or one of QGraphicsItem subclasses, depending on what you want to do with the event.

    You are getting 0 because calling pos() doesn't make sense in context of the scene. use scenePos() as already advised.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    HelderC (2nd October 2012)

  9. #7
    Join Date
    Mar 2010
    Location
    Brazil
    Posts
    39
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing shapes using GraphicsView, GraphicsScene and GraphicsItem(s)

    Ah, ok... finally I think I got it.

    Thank you guys.

Similar Threads

  1. Replies: 1
    Last Post: 10th February 2010, 12:07
  2. GraphicsView/GraphicsScene does not update
    By robicjedi in forum Qt Programming
    Replies: 5
    Last Post: 2nd February 2010, 23:33
  3. Replies: 21
    Last Post: 3rd April 2009, 12:22
  4. Drawing shapes
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 31st October 2008, 09:19
  5. Positinoning in GraphicsScene without using GraphicsItem
    By salmanmanekia in forum Qt Programming
    Replies: 2
    Last Post: 16th June 2008, 07:11

Tags for this Thread

Bookmarks

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.