PDA

View Full Version : Problem with a cursor during Drag and Drop



mtrpoland
28th December 2007, 15:24
Hi,
I have got a tiny problem connected to a drag and drop mechanism.
My QWidget subclass (lets call it A) is intended to be a source of elements that are copied to other type of QWidget subclass(lets call it B), thus in A I reimplement only

void PawnBox::mousePressEvent(QMouseEvent *event) {
//we do not need to distinguish click from a drag
//and we implement drag mechanism in a mousePressEvent
QPoint clickPt = event->pos();
//we recognize color of the dragged item
QRect pawnArea = QRect(0,0,20,20);
int pawnKind = -1; //index of a global pawn vector
for(int i=0;i<4;++i) {
pawnArea.setRect(25,45+i*30,20,20);
if(pawnArea.contains(clickPt)) { pawnKind = i; break; }
pawnArea.setRect(55,45+i*30,20,20);
if(pawnArea.contains(clickPt)) { pawnKind = i+4; break; }
}
//if a click happened on a non-pawn area we ignore it
if(pawnKind == -1) return;
qDebug(qPrintable(QString::number(pawnKind)));
//we assign the QColor object to a QByteArray
QByteArray bytes;
QDataStream out(&bytes,QIODevice::WriteOnly);
out << internalVectorPtr->at(pawnKind);
//we pack it
QMimeData *mimeData = new QMimeData;
mimeData->setData("qt-class/qcolor", bytes);
//we prepare cursor graphics
QPixmap draggedPawnImage = QPixmap(24,24);
draggedPawnImage.fill(Qt::transparent);
QPainter pixPainter(&draggedPawnImage);
Pawn tmpPawn = Pawn(internalVectorPtr->at(pawnKind));
MTR::Global::paintPawnAt(2,2,tmpPawn,pixPainter);
//we establish QDrag object, draw cursor and embed data
QDrag *drag = new QDrag(this);
drag->setPixmap(draggedPawnImage);
//QApplication::setOverrideCursor(QCursor(*cursorPx, 10,10));
drag->setDragCursor(*cursorPx,Qt::MoveAction);
drag->setDragCursor(*cursorPx,Qt::CopyAction);
drag->setMimeData(mimeData);
drag->setHotSpot(QPoint(12,12));
//and we send it
Qt::DropAction dropAction = drag->exec(Qt::CopyAction,Qt::CopyAction);
//QApplication::restoreOverrideCursor();

}

and everything is fine apart from the fact that over a pawn image instead of a small and transparent dot of *cursorPx initialized in a constructor of A:


//we prepare small point-pixmap which will be our mini cursor when dragging a pawn
cursorPx = new QPixmap(1,1);
cursorPx->fill();

there is Qt::ForbiddenCursor.

How to establish desired cursor during drag and drop?

jpn
28th December 2007, 15:46
You'll see a forbidden cursor over everything that does not accept the proposed action of QDragMoveEvent.



void MyWidget::dragMoveEvent(QDragMoveEvent* event)
{
if (event->mimeData()->hasFormat("qt-class/qcolor"))
event->acceptProposedAction();
}