PDA

View Full Version : how to get qstandarditem from qmodelindex using qitemdelegate (qabstractitemmodel)



qsurvae
1st July 2015, 11:54
I have a qstandarditemmodel and a qtableview but when using QItemDelegate to get QLineEdit editor I must declare QAbstractItemModel rather than QStandardItemModel when using Delegate::setModelData, thus I cannot use QstandardItem::itemFromIndex within the setModelData function of the delegate.

How can I get a QStandardItem from a QmodelIndex in this case?

Or how can I use the delegate with QStandardItemModel rather than QAbstractItemModel so that I can access model->itemFromIndex...



void Delegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{


QStandardItem *item = model->itemFromIndex(index); //itemFromIndex does not exist in this context!

anda_skoa
1st July 2015, 13:54
I am afraid I don't understand what you've written, but in order to make that code work you need to cast model to QStandardItemModel*.

What do you need the item for?

Cheers,
_

qsurvae
1st July 2015, 19:44
Thank you, working.



void Delegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{

QStandardItemModel *sModel = qobject_cast<QStandardItemModel *>(model);
QStandardItem *item = sModel->itemFromIndex(index.model()->index(0,0));

QList<QStandardItem *> newRows;
newRows.append( new QStandardItem("one") );
newRows.append( new QStandardItem("two") );

item->appendRows( newRows );