C++ (Qt) //----------------------------------Delegate.cpp------------------------------------------------------------------------- QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QLineEdit *lineEdit = new QLineEdit(parent); return lineEdit;} void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const{ QString string = index.model()->data(index,Qt::EditRole).toString(); QLineEdit *le = static_cast<QLineEdit* >(editor); le->setText(string);} //-------------------mainwindow.cpp-------------------------- Delegate *mydelegate = new Delegate(this); QTreeView*view = new QTreeView;view->resize(400,600);view->show(); ObjectBrowserModel*model = new ObjectBrowserModel(base);view->setModel(model);view->setItemDelegate(mydelegate);
C++ (Qt)view->setEditTriggers( QAbstractItemView::DoubleClicked );
C++ (Qt)Qt::ItemFlags ObjectBrowserModel::flags(const QModelIndex &index) const{ if(index.isValid()) { return Qt::ItemIsEditable|Qt::ItemIsSelectable|Qt::ItemIsEnabled; } else return 0;}
C++ (Qt) void Delegate::drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const{ QIcon icon; icon.setThemeName("D:\images\networkee.png"); QItemDelegate::drawDecoration(painter, option, option.rect, icon.pixmap(QSize(22,22),QIcon::Normal,QIcon::On) );}
C++ (Qt)model->setData( index, icon, Qt::DecorationRole );
C++ (Qt)bool ObjectBrowserModel::setData(const QModelIndex &index, const QVariant &value, int role){ QObject *obj; if(index.isValid()) { obj = reinterpret_cast<QObject* >(index.internalPointer()); } else { obj = base; } if(role == Qt::EditRole) { if(index.column() == 0 && QVariant(obj->metaObject()->className()).toString() == "Station") { Station*s = dynamic_cast<Station*>(obj); s->str = value.toString(); } else if(index.column() == 1 && QVariant(obj->metaObject()->className()).toString() == "Device") { Device*d = dynamic_cast<Device*>(obj); d->str = value.toString(); } return true; } else if(role == Qt::DecorationRole) { if(index.column() == 0 && QVariant(obj->metaObject()->className()).toString() == "Station") { QAbstractItemModel::setData(index,QIcon("D:/images/networkee.png"),Qt::DecorationRole); } return true; } return false;}
C++ (Qt)QVariant ObjectBrowserModel::data( const QModelIndex& index, int role ) const{ if( role == Qt::DecorationRole && index.column() == 0 && obj->metaObject()->className() == "Station" ) { static QIcon icon( ":/images/networkee.png" ); // icon better place in the resources return QVariant::fromValue< QIcon >( icon ); } return QAbstractItemModel::data( index, role );}