Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to drag and drop QStandardItems containing file paths in a Treeview to a file on the desktop or another application?
Forum Update on Monday, May 27th 2025

How to drag and drop QStandardItems containing file paths in a Treeview to a file on the desktop or another application?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 181 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ralphtink
    wrote on last edited by
    #1

    I have a treeview which is modeling a file tree. Each item in the TreeView is a QstandardItem that holds a file path. I would like to be able to get the file it referes to and drag the item into another application. The files are all video files so I would like to add the ability to drag and drop into VLC, Adobe Premier etc.

    There are a lot of posts with similar questions but the answers are kind of all over the place so any resources will be of great help.

    Thanks!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Subclass your QStandardItemModel with something like this:

      class FileItemModel : public QStandardItemModel{
          Q_OBJECT
          Q_DISABLE_COPY(FileItemModel)
      public:
          using QStandardItemModel::QStandardItemModel;
          QMimeData *mimeData(const QModelIndexList &indexes) const override{
              QMimeData *result  = QStandardItemModel::mimeData(indexes);
              if(result){
                  QList<QUrl> urls;
                  for (auto&& idx : indexes){
                      urls << QUrl::fromLocalFile(idx.data()); /*this assumes the full path to the file is stored in DisplayRole, change according to your needs*/
                  }
                  result->setUrls(urls);
              }
              return result; 
          }
          QStringList mimeTypes() const override{
              return QStandardItemModel::mimeTypes() << QStringLiteral("text/uri-list");
          }
      };
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved