Results 1 to 6 of 6

Thread: QGraphicsPixmapItem to QGraphicsScene

  1. #1
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsPixmapItem to QGraphicsScene

    Hey,

    I've been reading through some example coding online such as the following:

    Qt Code:
    1. scene = new QGraphicsScene;
    2. QPixmap pixmap = QPixmap::fromImage(image);
    3. pi.setPixmap (pixmap);
    4. scene.addItem(pi);
    To copy to clipboard, switch view to plain text mode 

    The problem I'm having is that it does not compile with the following error "error: no matching function for call to ‘QGraphicsScene::addItem(QGraphicsPixmapItem &)".

    Thanks,
    Strateng

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem to QGraphicsScene

    From the docs - void QGraphicsScene::addItem ( QGraphicsItem * item )
    addItem takes a pointer, not a reference. Also its better to create the item on heap rather than on stack. Because once the scope goes out, your item will get deleted and erased from the scene !

  3. #3
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem to QGraphicsScene

    Hey,

    Oh I never knew that but I'm not sure as to how you mean create the item on a heap. Would you be able to provide me with an example coding?

    Thanks,
    Strateng

    Quote Originally Posted by aamer4yu View Post
    From the docs - void QGraphicsScene::addItem ( QGraphicsItem * item )
    addItem takes a pointer, not a reference. Also its better to create the item on heap rather than on stack. Because once the scope goes out, your item will get deleted and erased from the scene !

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem to QGraphicsScene

    Creating a object on heap means creating with new operator.
    So instead of QGraphicsPixmapItem pi; you do something like - QGraphicsPixmapItem *pi = new QGraphicsPixmapItem();
    If you are using QGraphicsPixmapItem pi; make sure the scope is well defined. ie. something like member variable.

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

    strateng (26th March 2010)

  6. #5
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem to QGraphicsScene

    Oh I thought I did that before but it seems I didn't. Thanks for the help.

    Quote Originally Posted by aamer4yu View Post
    Creating a object on heap means creating with new operator.
    So instead of QGraphicsPixmapItem pi; you do something like - QGraphicsPixmapItem *pi = new QGraphicsPixmapItem();
    If you are using QGraphicsPixmapItem pi; make sure the scope is well defined. ie. something like member variable.

  7. #6
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem to QGraphicsScene

    Whats the problem with directly using

    Qt Code:
    1. QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(img));
    To copy to clipboard, switch view to plain text mode 

    as we did before?

    http://www.qtcentre.org/threads/2923...-QGraphicsView

    Johannes

Similar Threads

  1. Simplest example for QGraphicsPixmapItem
    By sincnarf in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2012, 14:24
  2. Scaling QGraphicsPixmapItem
    By johnsoga in forum Qt Programming
    Replies: 9
    Last Post: 7th May 2009, 17:04
  3. Bug in QGraphicsPixmapItem with QGLWidget
    By spud in forum Qt Programming
    Replies: 9
    Last Post: 27th February 2009, 05:31
  4. Shape of QGraphicsPixmapItem
    By StefanHirche in forum Newbie
    Replies: 4
    Last Post: 5th September 2007, 15:14
  5. Rotate QGraphicsPixmapItem
    By durbrak in forum Qt Programming
    Replies: 7
    Last Post: 15th April 2007, 14:51

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.