Drag and drop with Qt: Knowing target application on hover - is it possible?
I'm currently exploring the possibilities of drag-and-drop from a Qt application, into an unknown target application. The question is whether it is possible for the Qt application to receive information about the application that is about to receive the drop (e.g. process name, or title).
A (made up) example could be dragging a plot from a Qt window to e开发者_运维知识库ither a text editor or a spreadsheet editor. In the former, it would provide the data as an image, in the latter as the data points.
It seems Qt is somewhat aware of what the underlying target of the drop is while hovering, as the actionChanged signal is emitted when changing DropAction state. If I'm not mistaken, it seems that the target application looks at the mime-type contained in the drag object, and signals what the action would be, if dropped there. I'm also not getting anything out of the targetChanged signal (which as far as I can tell is for dropping within the current Qt application).
Any pointers would be helpful, and I'll gladly follow up.
You can add several data types to your drag mime data. While creating dragable data you can put image and points data (with proper mime-type) and each application will decide what data it accepts and how to interpret it.
For more details check https://doc.qt.io/qt-5/dnd.html#dragging
You can't really know which application is going to receive your drop. However, that doesn't prevent you from including different format of data in the buffer associated to the drag and drop. Class QMimeData provides the method setData, which takes mimeType as a first argument. You can put some data for a given mimetype, and some other for a different mimeType. It doesn't erase the first one. Custom mimeData are typically more useful for drops inside your application, but there are some relatively standard ones as : setText, setHtml, setUrls, setImageData, and setColorData.
Typically, you can put both some text and some image in the drag at the same time, and the receiving application should take the one it needs.
精彩评论