cardsdelegate.cpp

来自「程序代码使用说明: (1)所有源代码目录下都提供了Makefile(非Qt)」· C++ 代码 · 共 56 行

CPP
56
字号
#include "CardsDelegate.h"#include "CardsModel.h"#include <QModelIndex>#include <QPainter>QSize CardsDelegate::cellSize(10,10);QPolygon CardsDelegate::border;void CardsDelegate::paint(QPainter *p, const QStyleOptionViewItem &o, const QModelIndex &i) const{    QVariant v = i.model()->data(i, Qt::BackgroundColorRole);    QColor color = v.value<QColor>();    QVariant font = i.model()->data(i, Qt::FontRole);    if (!font.isNull())        p->setFont(font.value<QFont>());    p->setBrush(color);    p->setPen(Qt::NoPen);    int number = i.model()->data(i, Qt::DisplayRole).toInt();    if (number == CardsModel::MAX_CARDS_NUM)     {        p->drawRect(o.rect);        return;    }    // get image from model    // scale and draw the image    v = i.model()->data(i, Qt::UserRole);    QImage img = v.value<QImage>();            int w = o.rect.width();    int h = o.rect.height();    QImage img2=img.scaled(w, h);    p->drawImage(o.rect, img2);    // draw border    QPolygon pol = border;    pol.translate(o.rect.topLeft());    p->setBrush(Qt::darkYellow);    p->drawPolygon(pol);    // if "show number", draw numbers    v = i.model()->data(i, Qt::UserRole + 1);    bool bShowNumber = v.toBool();     if( bShowNumber )     {        int x2 = w - 1;        int y2 = h - 1;                p->setPen(Qt::red);        p->drawText(o.rect.x(), o.rect.y(), x2, y2, Qt::AlignLeft | Qt::AlignTop, QString::number(number));    }            }

⌨️ 快捷键说明

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