⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardsview.cpp

📁 程序代码使用说明: (1)所有源代码目录下都提供了Makefile(非Qt)
💻 CPP
字号:
#include "CardsView.h"#include "CardsModel.h"#include "CardsDelegate.h"#include <QHeaderView>#include <QMenu>#include <QKeyEvent>#include <QMouseEvent>#include <QMessageBox>CardsView::CardsView(QWidget *parent) : QTableView(parent){    setFrameStyle(NoFrame);    verticalHeader()->hide();    horizontalHeader()->hide();    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);}void CardsView::resizeEvent(QResizeEvent *e){    Q_UNUSED(e);    int cell_w = contentsRect().width() / model()->columnCount();    int cell_h = contentsRect().height() / model()->rowCount();    CardsDelegate::cellSize = QSize(cell_w, cell_h);    // Calculate border polygon, 2% for border    int x_offset = cell_w - int(cell_w * 0.98);     int y_offset = cell_h - int(cell_h * 0.98);    CardsDelegate::border.setPoints(6,    0, 0,    cell_w, 0,    cell_w - x_offset, y_offset,    x_offset, y_offset,    x_offset, cell_h - y_offset,    0, cell_h);    int i;    for (i = 0; i < model()->rowCount(); ++i)        verticalHeader()->resizeSection(i, cell_h);    for (i = 0; i < model()->columnCount(); ++i)        horizontalHeader()->resizeSection(i, cell_w);}void CardsView::keyPressEvent(QKeyEvent* e){    CardsModel *pt = qobject_cast<CardsModel *>(model());    if (!pt)     {        QTableView::keyPressEvent(e);        return;    }    switch ( e->key() )     {        case Qt::Key_Up: pt->moveUp(); e->accept(); break;        case Qt::Key_Down: pt->moveDown(); e->accept(); break;        case Qt::Key_Left:            {                pt->moveLeft();                e->accept();                break;            }        case Qt::Key_Right:            {                pt->moveRight();                e->accept();                break;            }        default:            QTableView::keyPressEvent(e);        return;    }}void CardsView::setModel(QAbstractItemModel *m){    QTableView::setModel(m);    CardsModel *pt = qobject_cast<CardsModel *>(model());    if (pt)        connect(pt, SIGNAL(gameWon()), this, SLOT(announceWin()));}void CardsView::mousePressEvent(QMouseEvent* e){    CardsModel *pt = qobject_cast<CardsModel *>(model());    if (!pt) {        QTableView::mousePressEvent(e);        return;    }    pt->move( indexAt( QPoint( e->x(), e->y() ) ) );}void CardsView::announceWin(){    QMessageBox::information(this,         "Congratulations!", "You win the game!");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -