Results 1 to 4 of 4

Thread: A list of QGraphicsItems

  1. #1
    Join Date
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default A list of QGraphicsItems

    Hi,
    I'm struggling to get this working:
    I have a QGraphicsView with a QGraphicsScene. The scene is filled with QGraphicsRectItems. I am able to fill in a QComboBox with strings (box1, box2 etc.) corresponding to the number of the rectangles. I also use the rectangle's position as the QVariant in the QComboBox...
    This is where the problem starts. I would like to be able to select the appropriate rectangle on the scene by selecting the 'box..' entry in the QComboBox. This works when the rectangles do not change position. When someone moves a rectangle, its position changes thus rendering the QVariant in the QComboBox useless...

    So - is there a better way of achieving a selectable list of QGraphicsItems? I know that QGraphicsItem has the itemChange event, but I don't have an idea how to propagate the new position to the QComboBox (which is a part of the main window)...

    Thanks in advance for your help,
    Regards,
    Bill

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

    Default Re: A list of QGraphicsItems

    Wouldn't it be easier to associate combobox entries with items themselves? Then upon choosing an entry, you could ask the item about its current position or even select it directly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Bill (22nd July 2009)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: A list of QGraphicsItems

    Position is not very good identifier for graphics items. How about adding direct pointers as user data?

    Qt Code:
    1. QGraphicsItem* item = ...;
    2. comboBox->addItem("...", QVariant::fromValue(item));
    3. ...
    4. QGraphicsItem* item = comboBox->itemData(i).value<QGraphicsItem*>();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Bill (22nd July 2009)

  6. #4
    Join Date
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A list of QGraphicsItems

    Hi,
    This is what I was thinking of doing in the first place, but wasn't too comfortable with the type casting... Now I see that it's not that painful ;-).

    Thanks for your help,
    Regards,

    Bill

Similar Threads

  1. Replies: 2
    Last Post: 19th September 2008, 05:21
  2. how to get a list of QGraphicsItems with no parent?
    By mooreaa in forum Qt Programming
    Replies: 6
    Last Post: 30th July 2008, 10:36
  3. How to make a list of QGraphicsItems on the scene
    By Holy in forum Qt Programming
    Replies: 8
    Last Post: 10th June 2008, 13:43
  4. Single slot for multiple list boxes
    By Sheetal in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2008, 06:53
  5. Replies: 1
    Last Post: 22nd October 2007, 02:04

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
  •  
Qt is a trademark of The Qt Company.