//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
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
out << internalVectorPtr->at(pawnKind);
//we pack it
mimeData->setData("qt-class/qcolor", bytes);
//we prepare cursor graphics
draggedPawnImage.fill(Qt::transparent);
Pawn tmpPawn = Pawn(internalVectorPtr->at(pawnKind));
MTR::Global::paintPawnAt(2,2,tmpPawn,pixPainter);
//we establish QDrag object, draw cursor and embed data
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
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->fill();
//we prepare small point-pixmap which will be our mini cursor when dragging a pawn
cursorPx = new QPixmap(1,1);
cursorPx->fill();
To copy to clipboard, switch view to plain text mode
there is
Bookmarks