C++ (Qt)void TreeModelBrowser::updateModel1(QModelIndex index){ int row = rowCount(index); row++; beginInsertRows(index,row,row); CommonElement *ce = reinterpret_cast<CommonElement*>(index.internalPointer()); ce->appendChild(); endInsertRows();}
class CommonElement: public QObject{ Q_OBJECTpublic: explicit CommonElement(QObject*parent = 0); virtual ~CommonElement(); virtual CommonElement* appendChild() = 0; virtual void removeChild(CommonElement*) = 0; virtual void function() = 0;public: QString str; QList<CommonElement*> listChildren;};
class Station: public CommonElement{ Q_OBJECTpublic: explicit Station(CommonElement *parent = 0); virtual ~Station(); CommonElement* appendChild(); void removeChild(CommonElement *); void function();};Station::Station(CommonElement *parent): CommonElement(parent){ str = "MyStantion";}Station::~Station(){}CommonElement* Station::appendChild(){ Device *device = new Device(this); listChildren.append(device); return device;}void Station::removeChild(CommonElement *device){ listChildren.removeOne(device); delete device;}void Station::function(){ qDebug()<<"Station->str = " + str;}
C++ (Qt)...connect(treeview,SIGNAL(clicked(QModelIndex)),this,SLOT(clickedTreeView(QModelIndex)));connect(this,SIGNAL(appendChild(QModelIndex)),treemodel,SLOT(updateModel(QModelIndex)));...void MainWindow::appendElement(){ qDebug()<<"appendElement"; if(currentElement != NULL) { Q_EMIT appendChild(currentModelIndex); }} void MainWindow::clickedTreeView(QModelIndex index){ if(index.isValid()) { currentModelIndex = index; currentElement = reinterpret_cast<CommonElement*>(currentModelIndex.internalPointer()); listmodel->setRoot(currentElement); setPathLabel(currentElement); }}
C++ (Qt)void TreeModelBrowser::updateModel(QModelIndex index){ if(index.isValid()) { beginResetModel(); CommonElement *ce = reinterpret_cast<CommonElement*>(index.internalPointer()); ce->appendChild(); endResetModel(); }}