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. Drag a QFileSystemModel item and drop it into Windows Desktop

Drag a QFileSystemModel item and drop it into Windows Desktop

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 1.7k 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.
  • F Offline
    F Offline
    franc
    wrote on last edited by franc
    #1

    Hi all,

    I am trying to develop a MainWindow containing a TreeView in the central widget. The TreeView is based on a QFileSystemModel. The QFileSystemModel root is located in a defined directory on the desktop.

    The peculiar thing is that I want to drag a file with a custom mime type.
    The code is keeped as clean as possible.
    In order to provide custom mime type, a class called "DraggableModel" inherits from QFileSystemModel. It overrides 4 functions of QAbstractItemModel:

    1. supportedDragActions() which simply returns Qt::MoveAction;

    2. flags() which returns Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled | defaultFlags - or - Qt::ItemIsDropEnabled | defaultFlags whether the index is valid or not;

    3. mimeType() is as follows:

    QStringList DraggableModel::mimeTypes() const
    {
        QStringList types;
        types << "application/myType";
        return types;
    }
    
    1. mimeData(), which is completely copied-and-pasted from the source code of QAbstractItemModel.cpp:
    QMimeData * DraggableModel::mimeData(const QModelIndexList &indexes) const
    {
        if (indexes.count() <= 0)
            return nullptr;
        QStringList types = mimeTypes();
        if (types.isEmpty())
            return nullptr;
        QMimeData *data = new QMimeData();
        QString format = types.at(0);
        QByteArray encoded;
        QDataStream stream(&encoded, QIODevice::WriteOnly);
        encodeData(indexes, stream);
        data->setData(format, encoded);
        return data;
    }
    

    Please, note that I have added a the new mime data type "application/myType" on Windows registry, and I have associated an application to the .myType extension. So that, Windows should be able to detect it.

    The code as it is does not allow me to drop an item with extension .myType (and others, but I am interested only in .myType files). Dragging works, but the mouse cursor takes graphically the form of a stop signal, and I can't perform any drop in any part of the screen (both application, other applications, and Windows desktop).

    If I do not override mimeData() function (i.e., I do not declare it), it works perfectly, but the unwanted mime type "text/uri-list" is set. Note also that when mimeData() is overridden, setting "text/uri-list" in the mimeTypes() function anyway does not solve the issue.

    How can I solve this issue? Is this issue solvable?

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

      You are trying to drag from the model to somewhere else or from somewhere else to the model?

      If the second is true you need to also reimplement the 2 methods:

      • http://doc.qt.io/Qt-5/qabstractitemmodel.html#canDropMimeData
      • http://doc.qt.io/Qt-5/qabstractitemmodel.html#dropMimeData

      "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

      F 1 Reply Last reply
      0
      • VRoninV VRonin

        You are trying to drag from the model to somewhere else or from somewhere else to the model?

        If the second is true you need to also reimplement the 2 methods:

        • http://doc.qt.io/Qt-5/qabstractitemmodel.html#canDropMimeData
        • http://doc.qt.io/Qt-5/qabstractitemmodel.html#dropMimeData
        F Offline
        F Offline
        franc
        wrote on last edited by
        #3

        @VRonin from the model to somewhere else

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

          Then it's not clear what you are trying to do.
          I think you have some confusion in mind on what the mime data and types are vs what file association is.

          Could you describe a little bit more in detail where you are dragging this data to and what would you like to happen when dropped?

          "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

          F 1 Reply Last reply
          3
          • VRoninV VRonin

            Then it's not clear what you are trying to do.
            I think you have some confusion in mind on what the mime data and types are vs what file association is.

            Could you describe a little bit more in detail where you are dragging this data to and what would you like to happen when dropped?

            F Offline
            F Offline
            franc
            wrote on last edited by franc
            #5

            @VRonin What I would like to do is the following: I have a Qt Application which shows the content of a local directory.
            I want to take a file shown in the view, drag it outside the application, and drop it, for instance, on the Windows File Explorer or the Desktop.
            Say that the root of the QFileSystemModel is

            C:\Users\Admin\Desktop\test

            and I want the drop site being:

            C:\Users\Admin\Desktop

            and the drop file being:

            myFile.myType

            Once it is dropped, I want the program associated being launched automatically, opening that file.
            That's the reason why I need the mime type, but I do not know how this information is treated when a Qt application interacts with Windows environment . More in general, I do not know how a Qt application interacts with Windows environment.
            I would like to proceed step by step, and this justifies the incomplete question I made.

            JonBJ VRoninV 2 Replies Last reply
            0
            • F franc

              @VRonin What I would like to do is the following: I have a Qt Application which shows the content of a local directory.
              I want to take a file shown in the view, drag it outside the application, and drop it, for instance, on the Windows File Explorer or the Desktop.
              Say that the root of the QFileSystemModel is

              C:\Users\Admin\Desktop\test

              and I want the drop site being:

              C:\Users\Admin\Desktop

              and the drop file being:

              myFile.myType

              Once it is dropped, I want the program associated being launched automatically, opening that file.
              That's the reason why I need the mime type, but I do not know how this information is treated when a Qt application interacts with Windows environment . More in general, I do not know how a Qt application interacts with Windows environment.
              I would like to proceed step by step, and this justifies the incomplete question I made.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @franc

              Once it is dropped, I want the program associated being launched automatically, opening that file.
              That's the reason why I need the mime type

              I don't know about the drag/drop part, but why do you need the mime type to cause the associated application to open on the end file? That's what the Windows OS "shell open" does for you?

              F 1 Reply Last reply
              0
              • F franc

                @VRonin What I would like to do is the following: I have a Qt Application which shows the content of a local directory.
                I want to take a file shown in the view, drag it outside the application, and drop it, for instance, on the Windows File Explorer or the Desktop.
                Say that the root of the QFileSystemModel is

                C:\Users\Admin\Desktop\test

                and I want the drop site being:

                C:\Users\Admin\Desktop

                and the drop file being:

                myFile.myType

                Once it is dropped, I want the program associated being launched automatically, opening that file.
                That's the reason why I need the mime type, but I do not know how this information is treated when a Qt application interacts with Windows environment . More in general, I do not know how a Qt application interacts with Windows environment.
                I would like to proceed step by step, and this justifies the incomplete question I made.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                @franc said in Drag a QFileSystemModel item and drop it into Windows Desktop:

                That's the reason why I need the mime type

                Nope that's wrong. You are trying to move a file so the mime type must be compatible with it. The event is not managed by the sender application but by the receiving object (the file explorer in your case) if it doesn't recognise the mime type then it will not accept the drop because it doesn't know what to do with it.

                All you can do is to call QDesktopServices::openUrl once the drop is completed

                "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

                F 1 Reply Last reply
                3
                • JonBJ JonB

                  @franc

                  Once it is dropped, I want the program associated being launched automatically, opening that file.
                  That's the reason why I need the mime type

                  I don't know about the drag/drop part, but why do you need the mime type to cause the associated application to open on the end file? That's what the Windows OS "shell open" does for you?

                  F Offline
                  F Offline
                  franc
                  wrote on last edited by
                  #8

                  @JonB Thanks for the question. That's because the file myFile.myType should be opened with a specific handler application (say myApplication), which detects the mime application/myType.

                  Summing up, I have four ingredients:

                  1. a Qt Application which shows a directory in its central widget;
                  2. a custom mime, application/myType, already contained in the Windows Register;
                  3. a custom format, myFyle.myType;
                  4. a second application, myApplication, which is registered as handler of the type myType.

                  I want to pick myFile.myType from the Qt Application TreeView, drag it outside the Qt Application, drop it on Windows File Explorer, and once it is dropped, launching myApplication to open myFile.myType.

                  JonBJ 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    @franc said in Drag a QFileSystemModel item and drop it into Windows Desktop:

                    That's the reason why I need the mime type

                    Nope that's wrong. You are trying to move a file so the mime type must be compatible with it. The event is not managed by the sender application but by the receiving object (the file explorer in your case) if it doesn't recognise the mime type then it will not accept the drop because it doesn't know what to do with it.

                    All you can do is to call QDesktopServices::openUrl once the drop is completed

                    F Offline
                    F Offline
                    franc
                    wrote on last edited by
                    #9

                    @VRonin Sorry for the incoming out of topic: the File Manager should be able to recognize the mime type once I have added it on the Register, shouldn't it?

                    I thank you for the reference.

                    VRoninV 1 Reply Last reply
                    0
                    • F franc

                      @JonB Thanks for the question. That's because the file myFile.myType should be opened with a specific handler application (say myApplication), which detects the mime application/myType.

                      Summing up, I have four ingredients:

                      1. a Qt Application which shows a directory in its central widget;
                      2. a custom mime, application/myType, already contained in the Windows Register;
                      3. a custom format, myFyle.myType;
                      4. a second application, myApplication, which is registered as handler of the type myType.

                      I want to pick myFile.myType from the Qt Application TreeView, drag it outside the Qt Application, drop it on Windows File Explorer, and once it is dropped, launching myApplication to open myFile.myType.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @franc
                      Sorry, I just don't understand questions as often phrased here as well as some of the Qt experts! I just read what is written and respond....

                      If you drop onto Windows Explorer, it is Windows Explorer which acts on that.

                      The stuff @VRonin is saying is right.

                      1 Reply Last reply
                      0
                      • F franc

                        @VRonin Sorry for the incoming out of topic: the File Manager should be able to recognize the mime type once I have added it on the Register, shouldn't it?

                        I thank you for the reference.

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        @franc said in Drag a QFileSystemModel item and drop it into Windows Desktop:

                        File Manager should be able to recognize the mime type once I have added it on the Register, shouldn't it?

                        @VRonin said in Drag a QFileSystemModel item and drop it into Windows Desktop:

                        I think you have some confusion in mind on what the mime data and types are vs what file association is.

                        The mime type is not the file extension. The mime type text/uri-list is recognised by the file system as a list of files that it can copy/move/create a link to.

                        "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
                        3

                        • Login

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