PDA

View Full Version : Tracking Drag & Drop mouse cursor position?



mclark
29th August 2006, 15:01
I am trying to expand an existing drag & drop implementation. When dragging a row element from one QTableWidget to another, I need to be able to identify the type of object the row under the mouse cursor represents to determine if a drop is allowed.

For example, I am dragging a row representing an object of type X. The drop table contains objects of type X, Y and Z. When the drag cursor is above objects NOT of type X the cursor should change and a drop not allowed.

Using dragEnterEvent() and dragMoveEvent() I can tell what the row type is when the cursor enters the QTableWidget but not when it moves to another row in the same table. How can I track the position of the curson during a drag operation?

wysota
29th August 2006, 15:17
Using dragEnterEvent() and dragMoveEvent() I can tell what the row type is when the cursor enters the QTableWidget but not when it moves to another row in the same table. How can I track the position of the curson during a drag operation?
dragMoveEvent does allow what you seek. Use the indexAt() method to get the item index. It contains both the row and column number of the hovered item.

mclark
29th August 2006, 16:53
dragMoveEvent does allow what you seek. Use the indexAt() method to get the item index.

I cannot find an available function called indexAt() for QDragMoveEvent, although pos() will give me the mouse coordinates as a QPoint object.

mclark
29th August 2006, 17:07
I found what you were referring to. Many thanks for your help.

mclark
29th August 2006, 20:04
The cursor changes to Qt::ForbiddenCursor when the mouse cursor crosses the row or column header of another QTableWidget. I does not change back while in that table. If entering the table from the right or bottom, a drop can be completed because the cursor is the regular drag cursor but not when the Qt::ForbiddenCursor is showing.

How can the cursor be changed back once the mouse has finished passing over the row or column header?

wysota
29th August 2006, 21:40
Did you reimplement dragEnterEvent? Can you show me the code you wrote concerning dragging?

mclark
30th August 2006, 15:34
These are the implementations of dragEnterEvent() and dragMoveEvent() I'm working with:


void MyTable::dragEnterEvent( QDragEnterEvent* pEvent )
{
if ( pEvent->mimeData()->hasFormat( MY_MIME_TYPE ) )
{
if ( pEvent->source() == this ) // same table, move entry
{
pevent->setDropAction( Qt::MoveAction );
pevent->accept();
}
else // different table, add entry
{
QTableWidgetItem* pItem = itemAt( pEvent->pos() );
if ( pItem != 0 )
{
int nDropRow = row( pItem );
if ( nDropRow != -1 )
{
QString sType = item( nDropRow, COL_DeviceType )->text();

if ( sType.compare( m_parent->m_sDragDeviceType ) != 0 )
pEvent->ignore();
}
}
else
pEvent->acceptProposedAction();
}
}
else
pEvent->ignore();
}

void MyTable::dragMoveEvent( QDragMoveEvent* pEvent )
{
if ( pEvent->mimeData()->hasFormat( MY_MIME_TYPE ) )
pEvent->acceptProposedAction();
}

mclark
8th September 2006, 16:19
Solved this one. Some debugging code was biting me in the backside :o! Thanks for the help wysota!