Results 1 to 2 of 2

Thread: How to add a pixmap drawn in QGraphicsItem to QGraphicsScene?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to add a pixmap drawn in QGraphicsItem to QGraphicsScene?

    How to add a pixmap drawn in QGraphicsItem to QGraphicsScene in the following example?

    Qt Code:
    1. #!/usr/bin/env python
    2.  
    3. from PyQt5.QtCore import (QLineF, QPointF, QRectF, pyqtSignal)
    4. from PyQt5.QtGui import (QIcon, QBrush, QColor, QPainter, QPixmap)
    5. from PyQt5.QtWidgets import (QAction, QMainWindow, QApplication, QGraphicsObject, QGraphicsView, QGraphicsScene, QGraphicsItem,
    6.  
    7. class TicTacToe(QGraphicsItem):
    8. def __init__(self, helper):
    9. super(TicTacToe, self).__init__()
    10.  
    11. self.mypixmap = QPixmap("exit.png")
    12.  
    13. self.painte = QPainter()
    14.  
    15. def paint(self, painter, option, widget):
    16. self.painte.setOpacity(0.1)
    17. self.painte.drawPixmap(0,0, 300, 300, self.mypixmap)
    18.  
    19.  
    20. def boundingRect(self):
    21. return QRectF(0,0,300,300)
    22.  
    23.  
    24. class MyGraphicsView(QGraphicsView):
    25. def __init__(self):
    26. super(MyGraphicsView, self).__init__()
    27. scene = QGraphicsScene(self)
    28.  
    29. self.tic_tac_toe = TicTacToe(self)
    30.  
    31. scene.addItem(self.tic_tac_toe)
    32.  
    33. scene.addPixmap(self.tic_tac_toe)
    34.  
    35. self.setScene(scene)
    36.  
    37. class Example(QMainWindow):
    38. def __init__(self):
    39. super(Example, self).__init__()
    40.  
    41. self.y = MyGraphicsView()
    42. self.setCentralWidget(self.y)
    43.  
    44. if __name__ == '__main__':
    45. import sys
    46. app = QApplication(sys.argv)
    47. w = Example()
    48. w.show()
    49. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to add a pixmap drawn in QGraphicsItem to QGraphicsScene?

    You're confused about ownership. You are creating your TicTacToe instance as a child of QGraphicsView; QGraphicsView instances don't own the objects displayed in them, the QGraphicsScene does.

    Likewise, you should not be creating your QGraphicsScene as a child of QGraphicsView; if anything, it should be created in your QMainWindow (Example) __init__ constructor and owned by the Example instance.

    So, you probably don't need a MyGraphicsView class at all - the out of the box QGraphicsView will do everything you want, I think. So get rid of that class completely.

    In addition, QGraphicsPixmapItem does exactly what your TicTacToe class does. So either get rid of the TicTacToe class and use QGraphicsPixmapItem directly or derive your TicTacToe class from it if you need special code to handle mouse events, etc. If you do that, get rid of the paint() and boundingRect() methods from your TicTacToe class - the base class handles that.

    In Example __init__ do the following:

    - create the QGraphicsView (as that, not a derived class), owned by the Example instance as the central widget.
    - create a QGraphicsScene as a child of the Example instance
    - create a QGraphicsPixmapItem (or TicTacToe instance) using QGraphicsScene::addPixmap() (for a QGraphicsPixmapItem) or QGraphicsScene::addItem() (for a TicTacToe instance)
    - call QGraphicsView::setScene() on your view instance.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 28th March 2016, 18:11
  2. How to get size of pixmap in qgraphicsitem?
    By darktorres in forum Newbie
    Replies: 7
    Last Post: 24th July 2014, 23:12
  3. QGraphicsItem is not drawn again?
    By zgulser in forum Qt Programming
    Replies: 12
    Last Post: 19th October 2009, 17:26
  4. AddLine over QGraphicsscene Pixmap
    By Qt Coder in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2009, 09:12
  5. QGraphicsScene - order of images drawn
    By bjh in forum Qt Programming
    Replies: 2
    Last Post: 29th November 2007, 16:41

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.