1

我正在尝试下棋。所以我想在用户点击并拖动硬币时移动国际象棋硬币图片。我必须使用哪个类

更新

最后,我只是在编辑拖放示例中给出的拼图代码。因此,我试图了解这些功能。但我仍然没有得到某些东西。我正在执行下面的代码,但图片没有移动。当我关闭时,我从操作系统(Windows XP)收到一个问题,即我的程序中有一个未处理的 win32 异常,所以无论你是否要调试。这里的代码

#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()));
}

任何帮助将不胜感激。

4

2 回答 2

2

对于拖放,您必须使用QDrag类(有关详细信息,请查看有关拖放的 Qt 示例)。QDrag 类有一个名为pixmap( pixmap(), setPixmap()) 的属性,可用于设置与拖动操作关联的图像。

于 2010-05-19T07:46:46.520 回答
2

看看 QGraphicsView,它给你一个画布来放置项目,甚至可以做动画。应该比手工绘制像素图容易得多。

于 2010-05-19T15:37:28.507 回答