I learned from OOAD class that dynamic_cast is a bad design but I don't know how can I do what I want without dynamic_cast in Qt cause I can't just do polymorphism in QGraphicsItem. Here is my code.
void Scene::changeName(){ QList<QGraphicsItem*> selecitems = this->selectedItems(); if(selectedItems().size()==1){ Base* object = dynamic_cast<Base*>(selecitems[0]); bool isok; if(object){ QString name = QInputDialog::getText( views().first() , tr("Change object name") , tr("Enter a name"),QLineEdit::Normal, "name", &isok); if(isok){ object->setName(name); } } } } I want to change an item's name if it is a Base object and its the only one selected.
And I need the function "setName" in Base class. Is there anyway to do what I want without using dynamic_cast?
In normal condition , I'll percolate up the function "SetName" in QGraphicsItem, but it seems that I can't do this in Qt.
Baseis derived fromQGraphicsItem?!