
Originally Posted by
gyre
wel...but I have to reimplement drag/drop mouse events hm ?
No, you don't reimplement them either. Model's mimeData() and dropMimeData() will get called by the framework when appropriate. However, the default implementation of those is sufficient, you don't need to reimplement them either.
First of all, make sure to drop all of these
- QVcaMsgSeqModel::mimeData()
- QVcaMsgSeqModel::dropMimeData()
- QVcaMsgSeqModel::mimeTypes()
Secondly, define a custom role to handle can messages. For example:
enum
{
QVcaCanMsgRole = Qt::UserRole
};
enum
{
QVcaCanMsgRole = Qt::UserRole
};
To copy to clipboard, switch view to plain text mode
Then, reimplement itemData() as already advised:
QMap<int, QVariant> QVcaMsgSeqModel
::itemData(const QModelIndex &index
) const{
// add additional custom data
roles.
insert(QVcaCanMsgRole,
QVariant::fromValue(msgList.
at(index.
row())));
return roles;
}
QMap<int, QVariant> QVcaMsgSeqModel::itemData(const QModelIndex &index) const
{
QMap<int, QVariant> roles = QAbstractTableModel::itemData(index);
// add additional custom data
roles.insert(QVcaCanMsgRole, QVariant::fromValue(msgList.at(index.row())));
return roles;
}
To copy to clipboard, switch view to plain text mode
Now, make setData() handle the custom role and you're all set:
{
...
if (index.isValid() && role == QVcaCanMsgRole)
{
msgList[row] = value.value<QVcaCanMsg>();
}
...
}
bool QVcaMsgSeqModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
...
if (index.isValid() && role == QVcaCanMsgRole)
{
msgList[row] = value.value<QVcaCanMsg>();
}
...
}
To copy to clipboard, switch view to plain text mode
Notice that QVcaCanMsg must be declared to Qt's meta system to be able to use it with QVariant:
Q_DECLARE_METATYPE(QVcaCanMsg);
Q_DECLARE_METATYPE(QVcaCanMsg);
To copy to clipboard, switch view to plain text mode
Bookmarks