mardi 21 avril 2015

Qt I want to encode a pointer as a string and decode it later

In my Qt app I'd like to encode a pointer to an object as a string, pass it to another bit of code then decode it so that I can access the object.

This is part of internal drag and drop with a QTreeView. In my mimeData() method I have:

QMimeData * TreeModel::mimeData(const QModelIndexList &indexes) const
{
    QMimeData *mimeData = new QMimeData();
    QByteArray encodedData;

    QDataStream stream(&encodedData, QIODevice::WriteOnly);

    foreach (QModelIndex index, indexes)
    {
        QString colText;
        if (index.isValid()) {
            TreeItem *item = getItem(index);

            // grab the text from each column 
            for(int cc=0; cc < item->columnCount(); cc++ ) {
                colText = item->data(cc).toString();
                stream << colText;
            }
            // add the pointer to the item 
            qDebug() << quint64(&item); 
            stream << quint64(&item);
        }
    }
    mimeData->setData("application/vnd.text.list", encodedData);

    return mimeData;
}

The qDebug() line produces a number like 140736277471632 which could be right, but is probably wrong.

How should I encode a pointer as a string so that it can be fed into a stream. And how should I then decode it and get the pointer to the original object?

Thank you.

Aucun commentaire:

Enregistrer un commentaire