Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从场景中标识选定项目的QT_Qt_Qt4_Qt4dotnet - Fatal编程技术网

从场景中标识选定项目的QT

从场景中标识选定项目的QT,qt,qt4,qt4dotnet,Qt,Qt4,Qt4dotnet,我一直在创建一个QT应用程序,但在某个地方遇到了麻烦。我已经创建了自己的自定义场景类,该类派生自QGraphicscene,从中我将汽车、公交车等项目添加到屏幕上 void Scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) { if (mouseEvent->button() != Qt::LeftButton) return; DiagramItem *item; swit

我一直在创建一个QT应用程序,但在某个地方遇到了麻烦。我已经创建了自己的自定义场景类,该类派生自QGraphicscene,从中我将汽车、公交车等项目添加到屏幕上

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;
从上面的代码中可以看到,我有一个DiagramItem类,它是从QGraphicsPixmapItem派生的,用于向场景添加不同的项

 switch (myDiagramType) {
        case Bus:
             setPixmap( QPixmap( Dir+"/images/bus1.jpg"  ));
            break;
        case Car:
            setPixmap( QPixmap( Dir+"/images/car4scene.png"  ));
            break;
        case Truck:
我想在这里实现的是,当我从场景中选择我的项目(汽车或公共汽车)时,我想知道选择了哪辆汽车,汽车、公共汽车或卡车。我不知道该怎么做。有人能帮我吗。我从场景中获得这样的选定项目

void main window::itemSelected(QGraphicsItem*item)//从场景发送的信号。 {

DiagramItem*ItemSelect=qgraphicsitem\u cast(项目);
//想知道“ItemSelect”是汽车、公共汽车或其他任何车辆吗


}

如果DiagramItem是您自己设计的,只需在内部保留该类型并提供查询该类型的方法即可。或者,保留一个散列,其中key是DiagramItem*,value是类型。

如果DiagramItem是您自己设计的,只需在内部保留该类型,并提供一个查询该类型的方法即可。或者,保留一个散列,其中key是DiagramItem*,value是type。

在QGraphicsItem中存储自定义数据而不派生自定义类的方法是使用和
setData()
。您可以使用存储的数据进行标识。

在QGraphicsItem中存储自定义数据而不派生自定义类的方法是使用和
setData()
。您可以使用存储的数据进行标识

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