Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Drag a QFileSystemModel item and drop it into Windows Desktop



  • 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?



  • 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:



  • @VRonin from the model to somewhere else



  • 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?



  • @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.



  • @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?



  • @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



  • @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.



  • @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.



  • @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.



  • @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.


Log in to reply