Results 1 to 3 of 3

Thread: how to get qstandarditem from qmodelindex using qitemdelegate (qabstractitemmodel)

  1. #1
    Join Date
    Jul 2015
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default how to get qstandarditem from qmodelindex using qitemdelegate (qabstractitemmodel)

    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...


    Qt Code:
    1. void Delegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
    2. {
    3.  
    4.  
    5. QStandardItem *item = model->itemFromIndex(index); //itemFromIndex does not exist in this context!
    To copy to clipboard, switch view to plain text mode 
    Last edited by qsurvae; 1st July 2015 at 12:01.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to get qstandarditem from qmodelindex using qitemdelegate (qabstractitemmodel

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    qsurvae (2nd July 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to get qstandarditem from qmodelindex using qitemdelegate (qabstractitemmodel

    Thank you, working.


    Qt Code:
    1. void Delegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
    2. {
    3.  
    4. QStandardItemModel *sModel = qobject_cast<QStandardItemModel *>(model);
    5. QStandardItem *item = sModel->itemFromIndex(index.model()->index(0,0));
    6.  
    7. QList<QStandardItem *> newRows;
    8. newRows.append( new QStandardItem("one") );
    9. newRows.append( new QStandardItem("two") );
    10.  
    11. item->appendRows( newRows );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 21st June 2011, 06:25
  2. getting data & QModelIndex
    By alexandernst in forum Newbie
    Replies: 3
    Last Post: 6th August 2009, 19:43
  3. Problem with QModelIndex
    By Kesy in forum Qt Programming
    Replies: 6
    Last Post: 30th October 2008, 08:44
  4. QModelIndex problem!
    By landonmkelsey in forum Qt Programming
    Replies: 10
    Last Post: 28th August 2008, 19:46
  5. Replies: 3
    Last Post: 7th May 2006, 17:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.