Is it possible for a QTableView to accept a drop in the empty area below its last row?
-
I have a QTableView that accepts drops in order to rearrange and create new rows. So far, everything is working, except that if the user performs a drop in the area below the final row of the table, it doesn't accept the drop. What I'd like it to do in that case is draw an indicator as though the user was hovering over the lower half of the final row and then accept the drop, placing it after the last row.
Since a picture is worth a thousand words:
Here is what it looks like when the user drops an item in the lower half of the last row. An indicator appears correctly, and when the user releases the mouse, a new row is added to the table. This is what I want to happen if the user drops an item on to the QTableView in the area below the last row.
Here is where I also want the user to be able to drop an item, but currently it does not work. Note the lack of indicator.
So far I've tried subclassing QTableView and reimplementing dragMoveEvent() and dropEvent() methods in such a way that it calls into QTableView's version of these function, but I pass in a new QDragMoveEvent or QDropEvent with a position that is in the lower half of the last row. While it does draw an indicator like I want, it doesn't accept the drop so this method doesn't seem to work.
I'm also aware I can set the stretchLastSection property to true for the vertical header of my QTableView, but this is unacceptable, as I need all the rows of my table view to be the same height.
Is there any way to accomplish this? At this point I'm stumped!
-
-
@Guy-Gizmo said:
Is there any way to get the table view to display a drop indicator in this case, like when the dropping between existing rows?
Return
QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled
fromQAbstractItemModel::flags(QModelIndex const& index) const
whereindex
is invalid. -
@Guy-Gizmo said:
@bsomervi I am already doing that, and while the cursor does change to show that the item can be dropped, it doesn't display the horizontal line indicator showing that it will be put below the last line of the table view.
I do not think that is easy. The lines around/above items are drawn by the QStyle in drawPrimitive() when the primitive is
PE_IndicatorItemViewItemDrop
so that can be changed with a QProxyStyle but that mechanism is for view items. I can't see an easy way of indicating in the viewport below the items when dragging there.