Pogel
Pogel

Reputation: 61

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.

Upvotes: 1

Views: 2094

Answers (2)

Jim
Jim

Reputation: 638

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 e.g. selectionModel()->selectedItems().

Upvotes: 2

Felipe Lema
Felipe Lema

Reputation: 2718

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 Qthreads 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.

Upvotes: 0

Related Questions