The return value of QAbstractItemModel::setData()



  • when reimplement QAbstractItemModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )
    as for the roles that we want to ignore ,what should we do ?
    return false or return true ?or anything else ?

    examples in the Qt documentation are even self-contradictory
    here the return value is false
    http://qt-project.org/doc/qt-4.8/model-view-programming.html#making-the-model-editable
    but here the return value is true
    http://qt-project.org/doc/qt-4.8/modelview.html#2-5-the-minimal-editing-example


  • Lifetime Qt Champion

    Hi,

    The simple way is to return the result of default implementation (IIRC it's false), so you should be safe.

    @return QAbstractItemModel::setData(index, value, role)@

    As for the why they return true in the example, it's explain under setData code.

    Hope it helps



  • but I think the default implementation is the same as return false
    [quote author="SGaist" date="1365509933"]Hi, The simple way is to return the result of default implementation (IIRC it's false), so you should be safe. @return QAbstractItemModel::setData(index, value, role)@ As for the why they return true in the example, it's explain under setData code. Hope it helps[/quote]


  • Lifetime Qt Champion

    That's exactly what I said

    The plus side of using the default implementation return value is that if this value should change (I highly doubt that in this case) you have the change for free.



  • so in this case both return QAbstractItemModel::setData(index, value, role) or false will be ok ,right ?



  • I do not think so. Look at doc:
    Sets the role data for the item at index to value.

    Returns true if successful; otherwise returns false.

    The dataChanged() signal should be emitted if the data was successfully set.

    The base class implementation returns false. This function and data() must be reimplemented for editable models.

    The base class returns false as it do not set any data (setting data is not successful).
    Your implementation should return true in case setting data is successful and false otherwise.



  • [quote author="Achab" date="1366534451"] Your implementation should return true in case setting data is successful and false otherwise.[/quote]
    sorry for my poor english ,what I actually want to express is that "return true in case setting data is successful ,otherwise return QAbstractItemModel::setData(index, value, role) or false has the same effect ",what about your opinion ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.