Results 1 to 7 of 7

Thread: Get a QGraphicsPixmapItem from a Scene and modify(change the image).

  1. #1
    Join Date
    Mar 2014
    Posts
    27
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Get a QGraphicsPixmapItem from a Scene and modify(change the image).

    Hi, I have to update every second (QTimer) the QGraphicsView, the QGraphicsView only have QGraphicsPixmapItems at specific positions in the whole frame.

    I have to modify every time like five or seven images on this QGraphicsView, but I don't know how to do it.

    Here's an example of my main code.
    Qt Code:
    1. scene->setSceneRect(0.0,0.0,400.0,400.0);
    2. for(int i=0;i<5;i++){
    3. image->setPixmap(icon);
    4. scene->addItem(image);
    5. image->setPos(i*icon.width(),0);
    6. }
    7. QGraphicsPixmapItem *image2 = new QGraphicsItem(); // with this default QGraphicsPixmapItem I'm trying to get the item at specific position, and set to this value (image2).
    8. //I know I'm not doing anything now, I just deleted the things I tried to do before posting this thread.
    9.  
    10. view->setScene(scene);
    11. view->show();
    To copy to clipboard, switch view to plain text mode 

    I want to change the image located in a specific position for another one, and move this image to the next position (left,right,top,bot).
    I tried using QGraphicsScene::itemat(); But I think Dont work with QGraphicsPixmapItem.

    If my question is vague, please let me know, I can explain it better, just that I don't have time right now.
    Thanks for your time.
    Last edited by vitaR; 22nd March 2014 at 03:01.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get a QGraphicsPixmapItem from a Scene and modify(change the image).

    Quote Originally Posted by vitaR View Post
    I tried using QGraphicsScene::itemat(); But I think Dont work with QGraphicsPixmapItem.
    Why should it not work with QGraphicsPixmapItem?

    Can you show the code that you have tried?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2014
    Posts
    27
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get a QGraphicsPixmapItem from a Scene and modify(change the image).

    Quote Originally Posted by anda_skoa View Post
    Why should it...
    Well I just said that... I'm bad I know, that function returns an item, but not a PixmapItem right?
    And anyways I couldn't call the function cause I never worked with QTranform before, so I don't know what to place there.

    The line number eleven should be something like this:
    image2->setPixmap(scene->itemAt(180,0,deviceTransform));

    Well I guess is not gonna work anyways, cause the function is not returning a Pixmap, and anyways the variable deviceTransform is not working, like I said, I don't know what to place there. Sorry for not answering early, I was busy. Thanks for the reply.
    Last edited by vitaR; 23rd March 2014 at 00:16.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Get a QGraphicsPixmapItem from a Scene and modify(change the image).

    Use qgraphicsitem_cast on pointer returned by itemAt method.
    Qt Code:
    1. QGraphicsPixmapItem * pixmap = qgraphicsitem_cast<QGraphicsPixmapItem*>(scene->itemAt(pos));
    2. if (pixmap){
    3. ....
    4. }
    To copy to clipboard, switch view to plain text mode 

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

    vitaR (23rd March 2014)

  6. #5
    Join Date
    Mar 2014
    Posts
    27
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get a QGraphicsPixmapItem from a Scene and modify(change the image).

    Quote Originally Posted by stampede View Post
    Use qgraphicsitem_cast on pointer...
    Thanks for the reply and Thanks for your time, everything works fine now FINALLY.

  7. #6
    Join Date
    Jan 2018
    Location
    Vadodara
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get a QGraphicsPixmapItem from a Scene and modify(change the image).

    Quote Originally Posted by stampede View Post
    Use qgraphicsitem_cast on pointer returned by itemAt method.
    Qt Code:
    1. QGraphicsPixmapItem * pixmap = qgraphicsitem_cast<QGraphicsPixmapItem*>(scene->itemAt(pos));
    2. if (pixmap){
    3. ....
    4. }
    To copy to clipboard, switch view to plain text mode 
    I have multiple QGraphicsPixmapItems in the QGraphicsScene and this is for an HMI application.
    In order to write the unit test to verify the QPixmaps and their locations i need to fetch the QGraphicsPixmapItems.

    I tried exactly as suggested by stampede but the problem is that pixmap returns NULL and if(pixmap) is always false.

    Please suggest.

    Thanks
    Anuj

  8. #7
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get a QGraphicsPixmapItem from a Scene and modify(change the image).

    Hi, a common problem is that the position is wrong or in the wrong coordinate system so depending on where you got the value for pos you might have to convert it to scene coordinates.

    Ginsengelf

Similar Threads

  1. Replies: 4
    Last Post: 15th May 2013, 14:58
  2. Modify image to have round corners
    By migel in forum Newbie
    Replies: 1
    Last Post: 4th September 2012, 12:38
  3. How to delete a QGraphicsPixmapItem in a scene.
    By newqtuser in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2009, 09:07
  4. Removing a QGraphicsPixmapItem from a scene
    By newqtuser in forum Qt Programming
    Replies: 2
    Last Post: 12th July 2008, 23:54
  5. Replies: 32
    Last Post: 30th March 2008, 21:00

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.