I am trying to do a chess game. So I want to move the chess coin picture when the user clicks and drags the coin. Which class I have to use
update
At last I am just editing the puzzle code which is given in drag and drop examples. Thereby I am trying to know the functions. But Still I am not getting certain things. I am executing the code below but the picture is not moving. And when I am closing I am getting a question from the OS(Windows XP), that there is an unhandled win32 exception in my program, so whether you want to debug or not. Here the code
#include<QApplication>
#include<QMainWindow>
#include<QWidget>
#include<QMenu>
#include<QMenuBar>
#include<QPainter>
#include<QFrame>
#include<QHBoxLayout>
#include<QScrollBar>
#include<QLabel>
#include<QScrollArea>
#include<QListWidgetItem>
#include<QByteArray>
#include<QDataStream>
#include<QMimeData>
#include<QDrag>
#include<QMouseEvent>
#include<iostream>
using namespace std;
class MyWindow:public QMainWindow
{
public:
MyWindow();
};
class MyWidget:public QWidget
{
QPixmap picture;
QPixmap temp;
public:
MyWidget();
void paintEvent(QPaintEvent * event);
void mousePressEvent(QMouseEvent * mouse);
void dragEnterEvent(QDragEnterEvent * dragEnterEvent);
void dragLeaveEvent(QDragLeaveEvent * event);
void dragMoveEvent(QDragMoveEvent * event);
void dropEvent(QDropEvent * event);
};
int main(int argc,char *argv[])
{
Q_INIT_RESOURCE(puzzle);
QApplication app(argc,argv);
MyWindow mainWindow;
mainWindow.show();
return app.exec();
}
MyWindow::MyWindow():QMainWindow()
{
setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
QMenu * fileMenu=menuBar()->addMenu(QObject::tr("Options"));
QAction * restartAction = fileMenu->addAction(tr("NewGame"));
QAction * exitAction = fileMenu->addAction(tr("Exit"));
exitAction->setShortcuts(QKeySequence::Quit);
QWidget * tempWidget=new MyWidget();
QFrame * newFrame=new QFrame();
QHBoxLayout * horizontal= new QHBoxLayout(newFrame);
horizontal->addWidget(tempWidget);
setCentralWidget(newFrame);
}
MyWidget::MyWidget():QWidget()
{
setMinimumSize(10,10);
setMaximumSize(1000,1000);
}
void MyWidget::dragEnterEvent(QDragEnterEvent * dragEnterEvent)
{
if(dragEnterEvent->mimeData()->hasFormat("chess"))
dragEnterEvent->accept();
else
dragEnterEvent->ignore();
}
void MyWidget::dragLeaveEvent(QDragLeaveEvent *event)
{
update(QRect(0,0,picture.width(),picture.height()));
event->accept();
}
void MyWidget::dragMoveEvent(QDragMoveEvent *event)
{
if(event->mimeData()->hasFormat("chess"))
{
event->setDropAction(Qt::MoveAction);
event->accept();
}
else
event->ignore();
update(QRect(0,0,picture.width(),picture.height()));
}
void MyWidget::dropEvent(QDropEvent *event)
{
if(event->mimeData()->hasFormat("chess"))
{
event->setDropAction(Qt::MoveAction);
event->accept();
}
else
event->ignore();
update(QRect(0,0,picture.width(),picture.height()));
}
void MyWidget::paintEvent(QPaintEvent * event)
{
QPainter painter;
painter.begin(this);
picture=QPixmap("C:\\Board").scaled(600,600,Qt::KeepAspectRatioByExpanding,Qt::SmoothTransformation);
setFixedSize(picture.size());
painter.drawPixmap(0,0,picture.height(),picture.width(),picture);
temp=QPixmap("C:\\blackElephant").scaled(60,30,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
painter.drawPixmap(0,0,temp.height(),temp.width(),temp);
painter.end();
}
void MyWidget::mousePressEvent(QMouseEvent * mouse)
{
QByteArray array;
QDataStream stream(&array,QIODevice::WriteOnly);
stream << temp;
QMimeData mimeData;
mimeData.setData("chess",array);
QDrag * newDrag=new QDrag(this);
newDrag->setMimeData(&mimeData);
newDrag->setHotSpot(mouse->pos());
newDrag->setPixmap(temp);
update(QRect(0,0,picture.width(),picture.height()));
}
any help will be appreciated.