Results 1 to 9 of 9

Thread: How to communicate btw QGraphicsScene and QTreeWidget

  1. #1
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to communicate btw QGraphicsScene and QTreeWidget

    Hey,

    i wanna know whats the best way to communicate between a QGraphicScene and a QTreeWidget.

    I have a QTreeWidget containing some QTreeWidgetItems and a QGraphicsView containing some QGraphicsItems, representing the positions of the items shown in the QTreeWidget.

    What i want to do: if you select an item in the TreeView, the corresponding GraphicsItem in the GraphicsView should be selected. and the other way around: select an item in the graphics view and the corresponding QTreeWidgetItem should be highlighted.

    I created a class which inherits both QGraphicItem and QTreeWidgetItem, but i couldnt figure out how to connect the view and the treewidget properly.

    Maybe you have some ideas?

    Greetings,
    Lutz
    Last edited by Cal; 20th November 2009 at 02:16.

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to communicate btw QGraphicsScene and QTreeWidget

    Quote Originally Posted by Cal View Post
    Hey,

    i wanna know whats the best way to communicate between a QGraphicScene and a QTreeWidget.

    I have a QTreeWidget containing some QTreeWidgetItems and a QGraphicsView containing some QGraphicsItems, representing the positions of the items shown in the QTreeWidget.
    What i want to do: if you select an item in the TreeView, the corresponding GraphicsItem in the GraphicsView should be selected. and the other way around: select an item in the graphics view and the corresponding QTreeWidgetItem should be highlighted.
    I created a class which inherits both QGraphicItem and QTreeWidgetItem, but i couldnt figure out how to connect the view and the treewidget properly.
    Maybe you have some ideas?
    SIGNAL AND SLOT.
    As Qt always says, signals and slots are used for interobject communication.
    And here your graphics item and tree widgt are just 2 objects wanna talk to each other.

  3. #3
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry Re: How to communicate btw QGraphicsScene and QTreeWidget

    a QGraphicsItem is not a QObject. So no way to define and connect slots

    You have to write your own item derived both from QObject and QGraphicsItem.
    Note that QObject must be the first derived object, in order to let the macro Q_OBJECT work well.

    Qt Code:
    1. class MyItem : public QObject, public QgraphicsItem {
    2. Q_OBJECT
    3. //then you can define slots ready to be connected
    4. public slots:
    5. void mySlot();
    6. };
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to communicate btw QGraphicsScene and QTreeWidget

    Quote Originally Posted by scascio View Post
    a QGraphicsItem is not a QObject. So no way to define and connect slots
    But QGraphicsView and QGraphicsScene are objects and that's where one probably wants to place the connections anyway.
    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.


  5. #5
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to communicate btw QGraphicsScene and QTreeWidget

    Yeah thats he point, instead of making thousands of connections btw the items and the views i wanted to handle the selection with connections between the QGraphicsView and QTreeWidget.

    Selecting an Item in the GraphicsView is easy, just by calling QGraphicsItem::setSelected(true). I simply connect the signal QTreeWidget::currentItemChanged() with this slot function:
    (nodes contains all the GraphicItems, table_root is the parent of the QTableWidgetItems)
    Qt Code:
    1. void changeViewSelection(QTreeWidgetItem *current, QTreeWidgetItem *previous) {
    2. view->scene()->clearSelection();
    3. int c = table_root->indexOfChild(current);
    4. int p = table_root->indexOfChild(previous);
    5. if (c>-1&&c<nodes.size())
    6. nodes[c]->QGraphicsItem::setSelected(true);
    7. if (p>-1&&p<nodes.size())
    8. nodes[p]->QGraphicsItem::setSelected(false);
    9. }
    To copy to clipboard, switch view to plain text mode 

    The other way around is more difficult, because i cant figure out the Index of the QGraphicsItems, thats why i cannot call QTreeWidget::SetCurrentItem(). I can get the selected Items of a QGraphicsScene by calling
    Qt Code:
    1. QList<QGraphicsItem *> list = view->scene()->selectedItems();
    To copy to clipboard, switch view to plain text mode 
    but i dont have any idea on which position in my nodes the items belong to. I could figure that out with a for loop testing for equality, but that doesnt seem nice and fast at all...

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

    Default Re: How to communicate btw QGraphicsScene and QTreeWidget

    Either have a global hash (or event two) to map between objects or have pointers to appropriate graphicsitems in tree items and vice-versa.
    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.


  7. #7
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to communicate btw QGraphicsScene and QTreeWidget

    Quote Originally Posted by wysota View Post
    But QGraphicsView and QGraphicsScene are objects and that's where one probably wants to place the connections anyway.
    If the need is to manage selection, sure I agree with you. The way Item->View is obvious and for View->Item you need to manage a map of items.

    So it depends on the kind of messages you need.
    For my own case, I derived my items from QObject because I need to do complex computing when painting item images in an MVC design, that relies on GUI independant processes.

  8. #8
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to communicate btw QGraphicsScene and QTreeWidget

    Quote Originally Posted by wysota View Post
    Either have a global hash (or event two) to map between objects or have pointers to appropriate graphicsitems in tree items and vice-versa.
    ah yeah, youre right... a hash table should work.

    But the other method (storing the pointers inside the items) sounds even better.
    Lets say i derived a new Class "Node" from QGraphicsItem which stores a pointer to the corresponding QTreeWidgetItem. I find out my selected QGraphicsItem by calling view->scene()->selectedItems(), how can i get the stored pointer now? Because this function just returns me items of the base class QGraphicsItem.

    What i could do, is to reimplement the setSelected function in my class "Node" and call the QTreeWidget::setCurrentItem() with the stored pointer from there. is that the way to go? cause it doesnt seem to work... isnt the QGraphicsView calling the setSelected() function on an Item, if you click on it in the view (its flag is set to QGraphicsItem::ItemIsSelectable)?
    Last edited by Cal; 20th November 2009 at 14:01.

  9. #9
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to communicate btw QGraphicsScene and QTreeWidget

    Since your have got 2 views that are reflecting the selection, is it not better to use a model, and manage some Ids for your items instead of pointers directly?

    Then, each view cans have a map of [Id, item] to manage selection.

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.