PDA

View Full Version : drag and drop



Bennieboj
15th November 2011, 07:14
Hello everyone,

I have made a chessgame, now I wanted to create a GUI for it, using model-view.
Every time I click a chesspiece (to dragn and drop) I want to store the original location of the DnDPixmap into 2 variables, so I still have the original location when I drop my chesspiece.
Then I can use those x and y values to see if the move is possible using my normal c++code.
The problem is, when I drag the adress of the DnDPixmap I hold seems to change, at least that's what my messageboxes are saying.



DnDPixmap::DnDPixmap(const QPixmap &pixmap, uitzicht* a)
: QGraphicsPixmapItem(pixmap)
{
// Make sure we allow drops.
setAcceptDrops(true);
connect(this, SIGNAL(moveMade( int, int, int, int)), a, SLOT(myslot( int, int, int, int)));

}


void DnDPixmap::mousePressEvent(QGraphicsSceneMouseEven t* event)
{


// We need to overload this method to make sure that
// nothing happens on a mouse press (e.g. select, move).
//
// This causes e.g. setFlag(QGraphicsItem::ItemIsMovable)
// to stop working, since flags modifies the mousePressEvent
// method of the base class, which is not called anymore now.
if (event->button() != Qt::LeftButton) {
event->ignore();
return;
}
}

void DnDPixmap::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
// Check if move distance is above standard treshold.
if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
.length() < QApplication::startDragDistance())
{
return;
}




this->oudeX = event->lastScenePos().rx()/50;
this->oudeY = event->lastScenePos().ry()/50;

// Create a drag object with mimetype data.
QDrag *drag = new QDrag(event->widget());
QMimeData *mime = new QMimeData;
drag->setMimeData(mime);

// Add text to the QMimeData object.
// QVariant is like a union, it can
// contain different kinds of types.
mime->setImageData(QVariant(pixmap()));

// Show a scaled icon of the current image next to
// the cursor when dragging. Scale it to be 30px wide.
drag->setPixmap(pixmap().scaledToWidth(30, Qt::FastTransformation));
// Set the cursor's tip (or hot spot) to point to
// the center of the pixmap's bottom edge.
drag->setHotSpot(QPoint(drag->pixmap().width()/2, drag->pixmap().height()));

QLabel* label = new QLabel;
QString p=QString::number((int)this,16);

label->setText("pointerINMOUSEMOVEEVENT="+p);
label->show();


// Start the drag operation.
drag->exec();
}

void DnDPixmap::dragEnterEvent(QGraphicsSceneDragDropEv ent *event)
{
// Is the data correct (an image that can be loaded)?
bool imageLoaded = false;
const QMimeData *mime = event->mimeData();
// Are there URLs present?
if (mime->hasUrls())
{
// Fetch the list of URLs dragged over.
const QList<QUrl> &urls = mime->urls();
if (!urls.empty())
{
// Try to load the first URL as an image.
// Modify imageLoaded according to
// the success of this operation.
imageLoaded = pix.load(urls[0].toLocalFile());
}
}
// Is there an image present?
else if (mime->hasImage())
{
// Convert the data to an QImage.
// The construct qvariant_cast is used to convert
// a stored value to another type.
QImage img = qvariant_cast<QImage>(mime->imageData());
// Load the QImage in a QPixmap.
pix = QPixmap::fromImage(img);
// Check if the pixmap is not blank.
imageLoaded = !pix.isNull();
}

QLabel* label = new QLabel;
QString p=QString::number((int)this,16);

label->setText("pointerINDRAGENTEREVENT="+p);
label->show();

// Only allow drops if the image loaded succesfully.
event->setAccepted(imageLoaded);
}

void DnDPixmap::dropEvent(QGraphicsSceneDragDropEvent *event)
{
QLabel* label = new QLabel;
QString p=QString::number((int)this,16);

label->setText("pointerINDROPEVENT="+p);
label->show();
emit moveMade(/*x location where i drop my pixmap, havent found a simple way to get this*/,/*y location, description to the left, oudeX, oudeY);

// Set the currently displayed pixmap to the dropped pixmap.
//setPixmap(pix);
}


When I'm dragging and dropping my chesspiece the value of the this pointer changes, which to me aint logical.
Can someone clarify this to me please?
Thanks in advance!