Results 1 to 3 of 3

Thread: Reference QGraphicsPixmapItem without adding it

  1. #1

    Default Reference QGraphicsPixmapItem without adding it

    What I want to happen is this. When a certain key is pressed.

    QPixmap image ("path");
    QGraphicsPixmapItem *pixmap = addPixmap(image);
    pixmap->setRotation(x);

    This works as it is but every time a key is pressed instead of rotating the current image it adds a new one rotated. So i need to be able to declare what pixmap is without adding it. I think I just need to change this line.

    QGraphicsPixmapItem *pixmap = addPixmap(image);

    And I could initialialy add it somewhere else.
    Here is a portion of my current code.

    void Scene::keyPressEvent( QKeyEvent *e )
    {

    QPixmap image ("path");
    QGraphicsPixmapItem *pixmap = addPixmap(image);
    switch ( e->key() )
    {
    case Qt::Key_Left:
    {
    pixmap->setRotation(x);
    break;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Reference QGraphicsPixmapItem without adding it

    Create the pixmap item in your constructor and store the pointer to it you a private member variable. Then you can easily access it.
    Qt Code:
    1. class Scene : /*...*/
    2. {
    3. //...
    4. private:
    5. }
    6.  
    7. Scene::Scene(/*...*/) : /*...*/
    8. {
    9. pixmap = addPixmap(QPixmap("path"));
    10. }
    11.  
    12. void Scene::keyPressEvent( QKeyEvent *e )
    13. {
    14. switch ( e->key() )
    15. {
    16. case Qt::Key_Left:
    17. {
    18. m_pixmap->setRotation(x);
    19. break;
    20. }
    To copy to clipboard, switch view to plain text mode 

  3. #3

    Default Re: Reference QGraphicsPixmapItem without adding it

    Thanks a lot you're awesome. I knew it had to be something that simple but I just couldn't think of it.

Similar Threads

  1. Simplest example for QGraphicsPixmapItem
    By sincnarf in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2012, 14:24
  2. Using QPropertyAnimation with QGraphicsPixmapItem
    By Luc4 in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2010, 09:47
  3. QGraphicsPixmapItem to QGraphicsScene
    By strateng in forum Newbie
    Replies: 5
    Last Post: 27th March 2010, 01:50
  4. Replies: 1
    Last Post: 15th January 2010, 15:14
  5. Shape of QGraphicsPixmapItem
    By StefanHirche in forum Newbie
    Replies: 4
    Last Post: 5th September 2007, 15:14

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.