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)
view->scene()->clearSelection();
int c = table_root->indexOfChild(current);
int p = table_root->indexOfChild(previous);
if (c>-1&&c<nodes.size())
nodes[c]->QGraphicsItem::setSelected(true);
if (p>-1&&p<nodes.size())
nodes[p]->QGraphicsItem::setSelected(false);
}
void changeViewSelection(QTreeWidgetItem *current, QTreeWidgetItem *previous) {
view->scene()->clearSelection();
int c = table_root->indexOfChild(current);
int p = table_root->indexOfChild(previous);
if (c>-1&&c<nodes.size())
nodes[c]->QGraphicsItem::setSelected(true);
if (p>-1&&p<nodes.size())
nodes[p]->QGraphicsItem::setSelected(false);
}
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
QList<QGraphicsItem *> list = view->scene()->selectedItems();
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...
Bookmarks