Results 1 to 8 of 8

Thread: adding QGraphicsItem to multiple scenes

  1. #1
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default adding QGraphicsItem to multiple scenes

    My problem is as follows...I have a QGraphicsItem that I would like to have in more then one scene. So I add the QGraphicsItem to scene1, then I add the same QGraphicsItem to scene2. The code would look something like this:
    Qt Code:
    1. scene1->addItem(theQGraphicsItem);
    2. scene2->addItem(theQGraphicsItem);
    To copy to clipboard, switch view to plain text mode 
    The problem is that the QGraphicsItem will only show up in scene2. I know that addItem removes the graphicsItem from any previous scene before adding it to the new one, but is there any way to make it add it to both?

    Thanks!

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

    Default Re: adding QGraphicsItem to multiple scenes

    You have to create a second item.
    Qt Code:
    1. QGraphicsItem *item1 = new MyGraphicsItem;
    2. scene1->addItem(item1);
    3. QGraphicsItem *item2 = new MyGraphicsItem(*item1);
    4. scene2->addItem(item2);
    To copy to clipboard, switch view to plain text mode 
    Note, that this requires a copy constructor. If your item doesn't have one, simply create the second item with the same parameters as the first one.

  3. #3
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: adding QGraphicsItem to multiple scenes

    Thanks Wysota,

    I was hoping I didn't have to do that considering the amount of items i had, but I guess that's the main/simplest solution.

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

    Default Re: adding QGraphicsItem to multiple scenes

    It's the only solution. Each item keeps its scene dependent state in its object, so you can't share an object between multiple scenes. Do you need multiple scenes at all? Maybe a single scene with multiple views will be enough?

  5. #5
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: adding QGraphicsItem to multiple scenes

    Do you need multiple scenes at all? Maybe a single scene with multiple views will be enough?
    I was trying to think of a way to use a single scene, but I couldn't come up with a solution. Maybe if I give you a background of what I'm doing, you can give me some feedback.

    I have three seperate graphics views which need to do the following:
    1) Show a mosaic of approx 20 selectable QGraphicsItemGroup objects
    2) When the user selects 1 of the 20 images from GraphicsViewNum1, it should show that selected QGraphicsItemGroup with an altered background (this might sound a bit confusing)...let me know if I need to clarify.
    3.) When the user clicked GraphicsViewNum2, GraphicsViewNum3 should show that selected QGraphicsItemGroup zoomed in.

    So my problem has been, if I use only one scene and I add approx 20 QGraphicsItemGroup's to it. How can I make it so that I only show one of the QGraphicsItemGroup's in graphicsView2, using only 1 scene? I haven't been able to come up with a solution, so I simply created multiple scenes with duplicate QGraphicsItemGroup's for each scene.

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

    Default Re: adding QGraphicsItem to multiple scenes

    Quote Originally Posted by forrestfsu View Post
    So my problem has been, if I use only one scene and I add approx 20 QGraphicsItemGroup's to it. How can I make it so that I only show one of the QGraphicsItemGroup's in graphicsView2, using only 1 scene?
    I think you can use QGraphicsView::setSceneRect() to ask the view to only visualise a rectangle occupied by a single group.

  7. #7
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: adding QGraphicsItem to multiple scenes

    I've been attempting a different solution for part of my problem. Perhaps the following can work? To display the same QGraphicsItemGroup on different views with a different background, can I reimplement:

    QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)

    I was able to get it to display a background on all the views:
    Qt Code:
    1. void QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. painter->drawEllipse(10, 10, 10, 10);
    4. }
    To copy to clipboard, switch view to plain text mode 
    However, I need to make it unique for each view. I want something like this:
    Qt Code:
    1. void QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. if (this->ui.view1)
    4. painter->drawEllipse(10, 10, 10, 10);
    5. else if (this->ui.view2)
    6. painter->drawEllipse(20, 20, 20, 20);
    7. else if (this->ui.view3)
    8. painter->drawEllipse(30, 30, 30, 30);
    9. }
    To copy to clipboard, switch view to plain text mode 

    However, I can't seem to figure out how to code the if statement to recognize a specific graphics view on my form...any tips? I've tried:

    Qt Code:
    1. if (this->ui.view1)
    2. if (this->TheForm::ui.view1)
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: adding QGraphicsItem to multiple scenes

    Got it! This seems to work:

    Qt Code:
    1. if (this->objectName() == "view1")
    2. else if (this->objectName() == "view2")
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23

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.