Results 1 to 4 of 4

Thread: Problems with selection models & views

  1. #1
    Join Date
    May 2009
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Problems with selection models & views

    I am looking for a good/elegant way of making two different objects aware of eachother. Here is what I want to accomplish:
    I have a QGraphicsScene that can contain any number of QGraphicsItems, and a QTreeView that lists the names of the QGraphicsItems within the scene in Z order (similar to photoshop's layers box)

    I would like to be able to select an item in the scene and have the corresponding line be selected in the layers view, and vice versa.

    Right now I have a semi-working solution using setData() to store a pointer to the other item. So the QGraphicsItem stores a pointer to the QStandardItem and vice versa. Here is an abstract of the code:

    Qt Code:
    1. QStandardItem *standardItem = new QStandardItem();
    2.  
    3. QVariant graphicsItemVariant = QVariant::fromValue(graphicsItem);
    4. QVariant standardItemVariant = QVariant::fromValue(standardItem);
    5.  
    6. int key = 0;
    7.  
    8. graphicsItem->setData(key, standardItemVariant);
    9. standardItem->setData(graphicsItemVariant);
    10.  
    11. standardItem->data().value<QGraphicsPixmapItem*>(); //pointer to the graphics item
    12. graphicsItem->data(key).value<QStandardItem *>(); //pointer to the standard item
    To copy to clipboard, switch view to plain text mode 

    This seems to work fine when implementing the proper signals & slots, etc.
    Except... When I use the above method, my program crashes when I go to internally drag & drop an item within the QTreeView. I've narrowed it down to actually setting the data on the QStandardItem. If I comment out line 10 above, the drag&drop behaviour is as expected (but obviously unable to select the corresponding QGraphicsItem within the scene)

    Does anyone have any suggestions for the way I have done it or perhaps a better way to accomplish what I am trying to do? Thanks in advance!

  2. #2
    Join Date
    May 2009
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with selection models & views

    I think I'm over complicating this... I think I am just going to subclass QStandardItem and QGraphicsPixmapItem and have have private pointers to the corresponding item.

    But the issue with setting the data (using setData() ) on the QStandardItem and getting the unexpected internal drag&drop behaviour... Any ideas?

  3. #3
    Join Date
    Feb 2009
    Posts
    11
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Problems with selection models & views

    I think you should subclass QGraphicsItem and emit a signal in mousePressEvent() with the graphics item's unique id as the argument of the signal.

  4. #4
    Join Date
    May 2009
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with selection models & views

    nimaweb: Thanks, that's an interesting idea. I'll see if I can work up a signal/slot solution...

Similar Threads

  1. QSqlTableModel and QTableView selection problems
    By innerhippy in forum Qt Programming
    Replies: 5
    Last Post: 19th December 2008, 05:48
  2. Models, delegates or views and how?
    By maddogg in forum Newbie
    Replies: 3
    Last Post: 9th November 2007, 13:59

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.