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
}
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.
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.
精彩评论