Results 1 to 4 of 4

Thread: QStandardItemModel with QIdentityProxyModel

  1. #1
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default QStandardItemModel with QIdentityProxyModel

    Hi , i have a problem with QStandardItemModel and QIdentityProxyModel.

    I've implemented a QIdentityProxyModel to be able to change the way of displaying the model. Here is the data() function of QIdentityProxyModel :

    Qt Code:
    1. QVariant data(const QModelIndex &index, int role) const
    2. {
    3. if (role == Qt::DisplayRole && index.column() == AlarmLogViewIndex_RecovTime){
    4. qDebug()<< sourceModel()->data(index);
    5. qint64 sinceEpoch = sourceModel()->data(index).toLongLong();
    6. const QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(sinceEpoch);
    7.  
    8. return dateTime.toString(m_formatString);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    This works like a charm when source model is a QSqlTableModel. But i need to use the same QIdentityProxyModel for a QStandardItemModel. When i use standartItemModel as the source of QIdentityProxyModel , i get QVariant(Invalid) values from the above data() function (in qDebug output...) . I was suspicious about the way i created the StandardItemModel. But it works well without the QIdentityProxyModel .

    Here is how i implement the StandardItemModel,

    adding a row to the model:
    Qt Code:
    1. void TagChannelItemModel::addToTagModel(pEtiketDef_t tg)
    2. {
    3. int row = tagModel->rowCount();
    4.  
    5. QStandardItem* item = new QStandardItem(QString::number(tg.Id));
    6. tagModel->setItem(row, TgIndex_Id, item);
    7.  
    8. item = new QStandardItem(tg.Name);
    9. tagModel->setItem(row, TgIndex_Name, item);
    10.  
    11. item = new QStandardItem(QString::number(tg.PollFrequency));
    12. tagModel->setItem(row, TgIndex_PollFreq, item);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Should i do anything else for this item model to work with ProxyModel ?

    Thanks in advance...
    Last edited by yagabey; 6th December 2014 at 21:56.

  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: QStandardItemModel with QIdentityProxyModel

    You are passing a wrong index to the source model, an index of the proxy instead of an index of the source model itself.

    See QAbstractProxyModel::mapToSource().

    Cheers,
    _

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

    yagabey (7th December 2014)

  4. #3
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QStandardItemModel with QIdentityProxyModel

    Hi, thanks you were right.. It works now.. Shouldn't the indexes be same since it is "identity" proxt model ?
    Last edited by yagabey; 7th December 2014 at 14:00.

  5. #4
    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: QStandardItemModel with QIdentityProxyModel

    Quote Originally Posted by yagabey View Post
    Hi, thanks you were right.. It works now.. Shouldn't the indexes be same since it is "identity" proxt model ?
    No. They will just have the same structure, but a QModelIndex is always directly tied to the model that created it.

    There is usually very little difference when models are list or tables, since the cell can be addressed unambiguously through row and column, but once you have a tree structure, the model needs to store some model specific data in the index object to know which subtree it is working on.

    QStandardItemModel is capable of being a tree model, so it does that and will then try to retrieve that additional data when getting an index as a function argument.
    It very likely even checks if index.model() is equal to itself before attempting to do anything with an index.

    Your test with the SQL table model worked by chance, because as a table it doesn't need to be as careful (it should though)

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 22nd March 2014, 15:57
  2. Replies: 3
    Last Post: 12th July 2013, 21:27
  3. how to use QIdentityProxyModel?
    By ecanela in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2012, 20:29
  4. using QStandardItemModel
    By GrahamLabdon in forum Newbie
    Replies: 1
    Last Post: 6th March 2011, 09:13
  5. QStandardItemModel Help
    By frenk_castle in forum Newbie
    Replies: 1
    Last Post: 16th January 2010, 17:54

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.