void MainWindow::on_mousedoubleclick(QPoint point)
{
QPointF pointfScene = ui->gView->mapToScene(point);
QGraphicsItem *itemScene = NULL;
itemScene = scene->itemAt(pointfScene, ui->gView->transform());
if(itemScene == NULL)
return;
/*
* QGraphicsItem *itemScene相当于基类
* QGraphicsRectItem *item类似派生类
* 进行下行转换
*/
switch (itemScene->type())
{
case QGraphicsRectItem::Type:{
QGraphicsRectItem *item;
item = qgraphicsitem_cast<QGraphicsRectItem*>(itemScene);
QColor color = item->brush().color();/*自动获取当前项目图形颜色*/
color = QColorDialog::getColor(color, NULL, "choose a color ");
if(color.isValid())/*如果颜色有效则返回true*/
item->setBrush(QBrush(color));
break;
}
case QGraphicsEllipseItem::Type:{
QGraphicsEllipseItem *item;
item = qgraphicsitem_cast<QGraphicsEllipseItem*>(itemScene);
QColor color = item->brush().color();/*自动获取当前项目图形颜色*/
color = QColorDialog::getColor(color, NULL, "choose a color ");
if(color.isValid())
item->setBrush(QBrush(color));
break;
}
case QGraphicsPolygonItem::Type:{
QGraphicsPolygonItem *item;
item = qgraphicsitem_cast<QGraphicsPolygonItem*>(itemScene);
QColor color = item->brush().color();/*自动获取当前项目图形颜色*/
color = QColorDialog::getColor(color, NULL, "choose a color ");
if(color.isValid())
item->setBrush(QBrush(color));
break;
}
case QGraphicsLineItem::Type:{
QGraphicsLineItem *item;
item = qgraphicsitem_cast<QGraphicsLineItem*>(itemScene);
QColor color = item->pen().color();/*自动获取当前项目图形颜色*/
color = QColorDialog::getColor(color, NULL, "choose a color ");
if(color.isValid())
item->setPen(QPen(color));
break;
}
case QGraphicsTextItem::Type:{
QGraphicsTextItem *item;
item = qgraphicsitem_cast<QGraphicsTextItem*>(itemScene);
QColor color = item->defaultTextColor();/*自动获取当前项目图形颜色*/
color = QColorDialog::getColor(color, NULL, "choose a color ");
if(color.isValid())
item->setDefaultTextColor(color);
break;
}
default:
break;
}
}