Qt World Summit: Submit your Presentation

Solved: Problem with const in dragMoveEvent

  • I'm a beginner and struggling with const, so I'm hoping someone here can help me "over the hump". I am trying to subclass QTextEdit to intercept a drag from a QListWidget. I need to determine what the text is in the QListItem and set the QMimeData data to text/plain. Here is what I have so far:

    @void TextEditDrop::dragEnterEvent(QDragEnterEvent *e)
    const QMimeData *mimeData = e->mimeData();
    QByteArray encoded = mimeData->data("application/x-qabstractitemmodeldatalist");
    QDataStream stream(&encoded, QIODevice::ReadOnly);
    QString item;
    int row, col;
    QMap<int, QVariant> roleDataMap;
    while (!stream.atEnd())
    stream >> row >> col >> roleDataMap;
    item = roleDataMap.value( Qt::DisplayRole ).toString();

    // Next statement generates error
    // error: C2662: 'QMimeData::setText' : cannot convert 'this' pointer from 
    //               'const QMimeData' to 'QMimeData &'
    // this also does not work:
    mimeData->setText(const_cast<QString>(item));  // cannot convert from QString to QString


    I believe I have to const_cast item but I have not been able to discover a syntax that works. Any help much appreciated.

  • First of all you need to perform your operations in dropEvent(), Secondly why are you using const QMimeData you can use QMimeData instead.

    Check "Drag and Drop Puzzle Example":http://qt-project.org/doc/qt-4.8/draganddrop-puzzle.html

  • Thanks. I'll check out the example.

  • I solved this issue by subclassing QListWidget::startDrag and creating a QMimeData->setText.

Log in to reply