2010-05-19 64 views
1

我正在嘗試做一個國際象棋遊戲。所以我想在用戶點擊並拖動硬幣時移動棋子圖片。 我使用哪個類來Qt-如何根據鼠標移動在屏幕上移動圖片

更新

最後,我只需要編輯這是在給定拖動和刪除實例拼圖代碼。因此我試圖瞭解這些功能。但我仍然沒有得到某些東西。我正在執行下面的代碼,但圖片沒有移動。當我關閉時,我從操作系統(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

看一看的QGraphicsView,它給你一個帆布把項目上,甚至可以做動畫。應該讓事情比手工繪製像素圖更容易。

+0

@prabhakaran QGraphicsView是完美的你想要完成的,QGraphicsItem也有適當的手柄點擊和選擇 – 2010-05-19 17:53:30

+0

@Harald Scheirich \ n 我看到那QGraphicsView,但我找不到任何方法直接添加一個像素圖。 以及如何刷新屏幕。如果可以的話,你可以把一些基本的代碼放在下面。因爲我現在很沮喪,並且運行的時間 派生類:從QGraphicsScene (前TOM)然後定義如下 提前()函數//這將被調用刷新 等... – prabhakaran 2010-05-20 13:08:04

+0

請參閱更新 – prabhakaran 2010-06-15 09:38:48

2

對於拖放操作,您必須使用QDrag類(有關詳細信息,請檢查Qt examples on drag)。 QDrag類有一個名爲pixmappixmap(),setPixmap())的屬性,可用於設置與拖動操作關聯的圖像。

+0

@給所有 首先我想完成我的項目,通過使用Qt的圖形用戶界面將需要更少的時間。但是當我明白我必須在qt中嘗試很多事情後,才決定首先完成邏輯部分。因此,每個機構請等待,直到我再次回到QT部分 – prabhakaran 2010-05-29 07:35:31

+0

請參閱更新 – prabhakaran 2010-06-15 09:42:21