0

I have been creating an QT application but struck in a place. I have created own custom scene class deriving from QGraphicsScene from where I add my items like car,bus etc to the screen .

void Scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{

    if (mouseEvent->button() != Qt::LeftButton)
        return;

    DiagramItem *item;
    switch (myMode) {
        case InsertItem:
            item = new DiagramItem(myItemType, myItemMenu);
            addItem(item);
            item->setPos(mouseEvent->scenePos());
            emit itemInserted(item);
            break;

As you can see from the above code I have a DiagramItem class which is derived from QGraphicsPixmapItem for adding different item to scene.

 switch (myDiagramType) {
        case Bus:
             setPixmap( QPixmap( Dir+"/images/bus1.jpg"  ));
            break;
        case Car:
            setPixmap( QPixmap( Dir+"/images/car4scene.png"  ));
            break;
        case Truck:

What I want to achieve here is,When I select my item from the scene (car or bus ), I want to know which vehicle has been selected either car or bus or truck . I have no clue how to go on this . Can any one help me . I get the selected item like this from scene .

void MainWindow::itemSelected(QGraphicsItem *item) // signal sent from scene. {

DiagramItem *ItemSelect = qgraphicsitem_cast<DiagramItem *>(item);

// like to know 'ItemSelect' is car or bus or anyother vehicle

}

Verve Innovation
  • 2,006
  • 6
  • 29
  • 48

2 Answers2

3

A way to store custom data in a QGraphicsItem without deriving a custom class is to use data() and setData(). You can use the stored data for identification.

Stephen Chu
  • 12,724
  • 1
  • 35
  • 46
1

If DiagramItem is of your own design, merely keep the type internally and provide a method to query it. Alternatively, keep a hash where key is DiagramItem * and value is the type.

xcramps
  • 1,203
  • 1
  • 9
  • 9