Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 571869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:33:10+00:00 2026-05-13T13:33:10+00:00

I have a class which inherits QAbstractTableModel, and holds some complex structs in a

  • 0

I have a class which inherits QAbstractTableModel, and holds some complex structs in a QMap. The QVariant data(QModelIndex index, ...) method just returns an enum which describes how a custom item delegate should draw the contents of a cell. I would like to implement drag and drop functionality in this model so that users can reorder these structs in the QMap, but can’t quite figure our how Qt would like me to do this. All I need is to see the source and destination indices of the drag/drop operation and I can take care of the rest, but the closest thing I’ve found in QAbstractItemModel is the dropMimeData() function. DropMimeData() doesn’t give me the source index and requires me to convert the data into some MIME type (plaintext, etc.), which it is definitely not. I can hack my way through this by creating a QMimeData that just contains the source index, but I would like to really learn to use Qt as it’s meant to be used, and I feel like I’m missing something. Any thoughts?

Just to help clarify a bit, the application is an animation program which acts sort of like Adobe Flash. The class which inherits QAbstractTableModel has a QMap<int, FrameState> (with struct FrameState{QPointF pos; bool visible;}) to hold keyframes. This state of this QMap is what I would like to display and have users edit. I draw a green circle if there is a visible key frame, a red circle if there is an invisible keyframe, a line if the previous keyframe was visible, and nothing if the previous keyframe was invisible. I would like users to be able to drag the keyframes around to change their QMap key.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:33:11+00:00Added an answer on May 13, 2026 at 1:33 pm

    You can use the views dragEnterEvent to get the indices that were selected initially:

    void DropTreeView::dragEnterEvent(QDragEnterEvent *event)
    {
        QTreeView::dragEnterEvent(event);
    
        const QItemSelectionModel * sm = selectionModel();
        if (!sm)
            return;
    
        dragStartIndicies = sm->selectedIndexes();
    }
    

    You’ll need to use the mime-types for the drag and drop, but C++ Qt provides a nice way to do that using QDataStream:

    QMimeData *YourModel::mimeData( const QModelIndexList &indexes ) const
    {
        QByteArray encodedData;
        QDataStream stream( &encodedData, QIODevice::WriteOnly );
    
        stream << yourQMap /* OR almost any Qt data structure */;
    
        QMimeData *mData = new QMimeData();
        mData->setData( YOUR_MIME_TYPE, encodedData );
    
        return mData;
    }
    

    On the receiving end, you can get your data structure (i.e. QMap if that’s what you want to use) back out of the QDataStream:

    QByteArray encodedData = yourMimeData->data( YOUR_MIME_TYPE );
    QDataStream stream( &encodedData, QIODevice::ReadOnly );
    QMap decodedMap;
    stream >> decodedMap;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.