views:

253

answers:

2

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.

+2  A: 

For drag and drop, you have to use QDrag class (check Qt examples on drag for details). QDrag class has a property called pixmap (pixmap(), setPixmap()) that can be used to set the image associated with the drag operation.

Cătălin Pitiș
@ to allFirst I thought complete the GUI of my project through qt will take less time. But after I understood that I have to experiment with many things in qt, then I decided to complete the logic part first. So every body please wait until I come back again to qt part
prabhakaran
please see the update
prabhakaran
+2  A: 

Have a look at QGraphicsView, it gives you a canvas to put items on, and even can do animations. Should make things a lot easier than painting pixmaps by hand.

Frank
@prabhakaran QGraphicsView is perfect for what you are trying to accomplish, QGraphicsItem also has appropriate handles for clicking and selection
Harald Scheirich
@Harald Scheirich\nI saw that QGraphicsView, but I can't find any way to add a pixmap directly into that.And How to refresh the screen. If you can, can you please put some basic code like below. Because I am frustrated now, and running out of timeDerive a class(ex: tom) from QGraphicsSceneThen define the functions belowadvance() // This will be called for refreshetc...
prabhakaran
please see the update
prabhakaran
@Frank , Thank you. It took me into a new level. I tried with labels and widgets, but QGraphicsScene and QGraphicsView only satisfied my expectation.
prabhakaran