Qt-如何根据鼠标移动在屏幕上移动图片

时间:2010-05-19 07:33:23

标签: image qt dynamic mouse chess

我正在尝试下棋。因此,当用户点击并拖动硬币时,我想移动国际象棋硬币图片。  我必须使用哪个班级

更新

最后我只是编辑了拖放示例中给出的拼图代码。因此,我试图了解功能。但我仍然没有得到某些东西。我正在执行下面的代码,但图片没有移动。当我关闭时,我从OS(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()));
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

对于拖放,您必须使用QDrag类(详情请查看Qt examples on drag)。 QDrag类有一个名为pixmappixmap()setPixmap())的属性,可用于设置与拖动操作关联的图像。

答案 1 :(得分:2)

看看QGraphicsView,它会为你提供一个画布来放置物品,甚至可以做动画。应该比手工绘制pixmaps更容易。