PDA

View Full Version : QGraphicsItemGroup problem



init2null
7th November 2006, 07:14
The new Graphics View framework is great, but I'm having a problem with grouping items. In the example below, only the text item not in the group can be edited. How can I allow the user to edit text items in groups?
Thanks,
Wesley


#include <QtGui>

int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QGraphicsView win;
QGraphicsScene *scene = new QGraphicsScene(&win);

QGraphicsTextItem *textItem = new QGraphicsTextItem("In a Group", NULL, scene);
textItem->setPos(50, 50);
textItem->setTextInteractionFlags(Qt::TextEditable|Qt::TextS electableByMouse);
QGraphicsItemGroup *group = new QGraphicsItemGroup(NULL, scene);
group->addToGroup(textItem);

QGraphicsTextItem *textItem2 = new QGraphicsTextItem("Not in a Group", NULL, scene);
textItem2->setPos(50, 10);
textItem2->setTextInteractionFlags(Qt::TextEditable|Qt::TextS electableByMouse);

win.setScene(scene);
win.show();
app.exec();
return 0;
}

wysota
7th November 2006, 21:11
I think you won't be able to achieve that without subclassing QGraphicsItemGroup. Basically that class is a QGraphicsItem subclass which by itself doesn't allow text interaction (as it doesn't even know what text is). So the thing you should probably do is to subclass the group item and introduce that functionality (by forwarding whatever needs to be forwarded to activate the text edition of the text item).

Bitto
20th November 2006, 22:45
There are two ways to make it work, although this isn't really what QGraphicsItemGroup is for; it's correct that you can achieve this easily with a regular QGraphicsItem parent (which already supports grouping).

Option 1) Create a group item subclass that forwards key press and release events to the text item.

Option 2) Subclass the text item, then reimplent QGraphicsTextItem::sceneEvent() to intercept the mouse and key events for the text item before they are dispatched to the group item.

Good luck :-).