简体   繁体   中英

Qt Drag and Drop QTreeView

My TreeView has various folder with children. each child has a icon and text-name. the user can select one or more item and drag them into QMDI area. as image for my darg drop i have a local picture drag->setPixmap(QPixmap(myPixImage)). What I need is: how i make the selected item with icon and text as myPixImage dynamically.

It's easy to set the pixmap when your own code create the QDrag object, however using QXxxViews (not only QTreeView) you have only control on the QMimeData (when overriding QAbstractItemModel::mimeData()).

Therefore if you really need that, the only way I now is to subclass QTreeView, overriding (well... reimplenting) mouseMoveEvent() and the like. If you do so, you can get the selected items when creating the QDrag through eg selectionModel()->selectedItems().

First of all: there's no simple way to do this. Basically because QDrag::exec is started when you (as developer) allow the start of the drag (example here ) and it doesn't return until the drag is finished.

You want to change your QDrag object while dragging.

Your best option here is:

  1. After creating the Drag object and before running QDrag::exec store the object in some pool that can be accessed anywhere in the code. Probably something like static map<QDrag *> pool
  2. Set up a recurrent QTimer event that constantly updates the the QDrag object, which can be obtained from the pool. Do not try using Qthread s as you'll end up with some "QObject can't be moved from/to thread" error.
  3. When the drag's ended (as user), clean up the object from the pool

Do mind that you'll have to visually tune the QTimer repeated events accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM