1

I'm overloading the paint() function in QAbstractItemDelegate (my own Item delegate class).

When dragging, it paints the contents of the entire cell, which I don't want. I'm assuming that the paint() function is called with something specific while dragging, but I don't seem to find it.

The closest I've been able to find is a QState variable in the owning view class (access function QTableView::state() is protected.) By creating a function on my QTableView-derived class called 'isDragging()' which calls that function and returns whether dragging or no, I can determine within my delegate class whether I'm dragging or not, and can modify the paint() function.

This almost works.

The problem is that it shows the modified paint image in the original cell, which I don't want - I want to leave the image in the original cell untouched.

Have to scour the examples, I guess, and see if there's something that does this...

I have crawled through the Qt source and I can see where it sets the drag pixmap by calling the QItemDelegate::paint() function, but the only thing it changes is it forces QStyle::State_Selected in the item option style. That's not enough, since the item is selected, already.

Any way to know how to draw a cell's contents explicitly when dragging?

rickb
  • 601
  • 5
  • 19

2 Answers2

2

Ok, the ultimate answer on this was to, yes, set the flag on 'startDrag', but rather than leaving it around and unsetting it on mouse release button event, simply call the base method and then unset.

The reason is that the image for the cursor is only requested (and painted) once - not continuously during the drag, as I had first thought. Leaving the flag set meant the cursor image would get drawn at inappropriate times.

So, the implementation looks like:

MyClass::dragStart(Qt::DropActions supportedActions)
{
  __dragStart = true;
  TableView::dragStart(supportedActions);
                   // request for drag cursor image happens here
  __dragStart = false;
}
rickb
  • 601
  • 5
  • 19
0

Why don't you do that yourself? Set a flag when dragging starts and remember the active ModelIndex, do some special painting when the flag is set, and clear the flag when dragging is finished. You can do this by overriding QAbstractItemView::startDrag.

hmuelner
  • 8,093
  • 1
  • 28
  • 39
  • Yeah, that's what I wound up doing. Have to clear the flag on a mouse up. – rickb Mar 25 '11 at 13:49
  • Works on Mac. Doesn't work on win. Same as if I just check the QTableView::state() flag for DraggingState. Next? – rickb Mar 25 '11 at 18:40
  • Tried setting an initial mouse position on mouse down and checking that with current mouse position on mouse move. Works fine after first drag, but first drag is still screwed up. – rickb Mar 27 '11 at 13:38
  • Urk. That didn't completely fix it. I had to force a 'dataChanged()' signal on the model to force a refresh. Blinks, but it works. – rickb Mar 29 '11 at 00:05