Qt拖拽实现拼图【官⽅demo源码超级详细解读】
前⾔:
如果不了解Qt drag-drop 的建议先看⼀下
否则看起来会⼀头雾⽔
看⼀下官⽅的介绍:
译⽂:这个例⼦是⼀个简单的拼图游戏的实现,它使⽤了Qt的模型/视图框架提供的对拖放的内置⽀持。拖放拼图的例⼦展⽰了许多相同的特性,但是采⽤了另⼀种⽅法,即在应⽤程序级别使⽤Qt的拖放API来处理拖放操作。
这个拼图的demo 还是能学到东西的
项⽬叫做 puzzle ⼤家可以去官⽅demo ⾥⼀下 玩⼀玩
项⽬的结构如下
main
mainwindow 主界⾯piecesmodel 拼图块模型puzzlewidget 拼图窗⼝
main :
初始化了资源
实例化了主窗体
加载了⼀个图⽚
图⽚就是下⾯
mainWindow.h
class MainWindow :public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent =0);
public slots:
void openImage();
void loadImage(const QString &path); void setupPuzzle();
private slots:
void setCompleted();
private:
void setupMenus();
void setupWidgets();
QPixmap puzzleImage;
QListView *piecesList;
PuzzleWidget *puzzleWidget;
PiecesModel *model;
};
类也不复杂
private:
⼀个 存放 拼图照⽚的 pixmap
⼀个 存放左边拼图块的 listview
右边的拼图窗⼝
拼图的块模型
他是⾃定义了 模型 等会看
整个的结构是这样的
#include"mainwindow.h"
#include"piecesmodel.h"
#include"puzzlewidget.h"
#include<QDebug>
#include<QtWidgets>
#include<stdlib.h>
MainWindow::MainWindow(QWidget *parent)
:QMainWindow(parent)
{
setupMenus();
setupWidgets();
model =new PiecesModel(puzzleWidget->pieceSize(),this);
piecesList->setModel(model);
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); setWindowTitle(tr("Puzzle"));
}
void MainWindow::openImage()
{
const QString fileName =
QFileDialog::getOpenFileName(this,
tr("Open Image"),QString(),
tr("Image Files (*.png *.jpg *.bmp)"));
if(!fileName.isEmpty())
loadImage(fileName);
}
void MainWindow::loadImage(const QString &fileName)
{
QPixmap newImage;
if(!newImage.load(fileName)){
QMessageBox::warning(this,tr("Open Image"),
tr("The image file could not be loaded."),
QMessageBox::Cancel);
return;
}
puzzleImage = newImage;
setupPuzzle();
}
void MainWindow::setCompleted()
{
QMessageBox::information(this,tr("Puzzle Completed"),
tr("Congratulations! You have completed the puzzle!\n"
"Click OK to start again."),
QMessageBox::Ok);
setupPuzzle();
}
qt listviewvoid MainWindow::setupPuzzle()
{
int size =qMin(puzzleImage.width(), puzzleImage.height());
puzzleImage = py((puzzleImage.width()- size)/2,
(puzzleImage.height()- size)/2, size, size).scaled(puzzleWidget->imageSize(),
puzzleWidget->imageSize(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); qsrand(QCursor::pos().x()^ QCursor::pos().y());
model->addPieces(puzzleImage);
puzzleWidget->clear();
}
void MainWindow::setupMenus()
{
QMenu *fileMenu =menuBar()->addMenu(tr("&File"));
QAction *openAction = fileMenu->addAction(tr("&"));
openAction->setShortcuts(QKeySequence::Open);
QAction *exitAction = fileMenu->addAction(tr("E&xit"));
exitAction->setShortcuts(QKeySequence::Quit);
QMenu *gameMenu =menuBar()->addMenu(tr("&Game"));
QAction *restartAction = gameMenu->addAction(tr("&Restart"));
connect(openAction,&QAction::triggered,this,&MainWindow::openImage);
connect(exitAction,&QAction::triggered, qApp,&QCoreApplication::quit);
connect(restartAction,&QAction::triggered,this,&MainWindow::setupPuzzle);
}
void MainWindow::setupWidgets()
{
QFrame *frame =new QFrame;
QHBoxLayout *frameLayout =new QHBoxLayout(frame);
puzzleWidget =new PuzzleWidget(400);
piecesList =new QListView;
piecesList->setDragEnabled(true);
piecesList->setViewMode(QListView::IconMode);
piecesList->setIconSize(QSize(puzzleWidget->pieceSize()-20, puzzleWidget->pieceSize()-20)); piecesList->setGridSize(QSize(puzzleWidget->pieceSize(), puzzleWidget->pieceSize()));
piecesList->setSpacing(10);
piecesList->setMovement(QListView::Snap);
piecesList->setAcceptDrops(true);
piecesList->setDropIndicatorShown(true);
PiecesModel *model =new PiecesModel(puzzleWidget->pieceSize(),this);
piecesList->setModel(model);
connect(puzzleWidget,&PuzzleWidget::puzzleCompleted,
this,&MainWindow::setCompleted, Qt::QueuedConnection);
frameLayout->addWidget(piecesList);
frameLayout->addWidget(puzzleWidget);
setCentralWidget(frame);
}
把这个⼏个函数的实现都挨个看吧
构造:
setupMenus()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论