1 Star 0 Fork 2

王吉祥/自绘Qt所有控件

forked from aswg/自绘Qt所有控件 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
resourcedelegate.cpp 3.24 KB
一键复制 编辑 原始数据 按行查看 历史
aswg 提交于 2021-06-11 15:48 . 包括delegate的OnPainter里绘制QCheckBox
#include "ResourceDelegate.h"
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QPen>
#include <QtDebug>
#include "dpi_util_win.h"
ResourceDelegate::ResourceDelegate(QAbstractItemView *parent)
: QStyledItemDelegate(parent)
, parent_(qobject_cast<QAbstractItemView*>(parent))
, updating(false)
{
scaleX = WinDpi::windowsDpiScaleX();
scaleY = WinDpi::windowsDpiScaleY();
// 当更改DPI的时候,更新最新的DPI
#ifdef Q_OS_WIN
connect(QApplication::primaryScreen(), &QScreen::logicalDotsPerInchChanged,
this, [this](){
scaleX = WinDpi::windowsDpiScaleX();
scaleY = WinDpi::windowsDpiScaleY();
});
#endif
}
void ResourceDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem viewOption(option);
initStyleOption(&viewOption, index);
QPen pen;
pen.setColor(QColor("#50AFDB"));
pen.setWidth(1);
painter->setPen(pen);
QRect rectViewItem = viewOption.rect;
QRect rect = QRect(rectViewItem.x() + (viewOption.rect.width() - selectWidth) / 2,
rectViewItem.y() + (viewOption.rect.height() - selectHeight) / 2,
selectWidth * WinDpi::windowsDpiScaleX(),selectHeight * WinDpi::windowsDpiScaleY());
painter->drawRoundedRect(rect, 2, 2); // round rect
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
bool data = index.model()->data(index, Qt::UserRole).toBool();
qInfo() << __FUNCTION__ << data << index.row() << index.column();
if(data)
{
QPainterPath path;
path.moveTo(rect.x() + rect.width() * 1 / 3 , rect.y() + rect.height()* 1 / 4);
path.lineTo(rect.x() + rect.width() * 1 / 2 , rect.y() + rect.width()* 3 / 5);
path.lineTo(rect.x() + rect.width() + rect.width() * 1 / 4 ,rect.y());
path.lineTo(rect.x() + rect.width() * 1 / 2 , rect.y() + rect.height() * 13 / 15);
path.lineTo(rect.x() + rect.width() * 1 / 6 , rect.y() + rect.height() * 1 / 2);
path.lineTo(rect.x() + rect.width() * 1 / 3 , rect.y() + rect.height() * 1 / 4);
painter->fillPath(path, QColor("#40B400"));
}
painter->restore();
}
bool ResourceDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
QRect decorationRect = option.rect;
if (index.column() == HeaderList::UserRole_Checkbox_Select)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if(event->type() == QEvent::MouseButtonDblClick)
{
if(!updating)
{
bool data = model->data(index, Qt::UserRole).toBool();
model->setData(index, !data, Qt::UserRole);
}
}
else if (event->type() == QEvent::MouseButtonPress && decorationRect.contains(mouseEvent->pos()))
{
if(!updating)
{
bool data = model->data(index, Qt::UserRole).toBool();
model->setData(index, !data, Qt::UserRole);
}
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
void ResourceDelegate::setUpdating(bool value)
{
updating = value;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/wjx340826/self-drawing-qt-all-widgets.git
git@gitee.com:wjx340826/self-drawing-qt-all-widgets.git
wjx340826
self-drawing-qt-all-widgets
自绘Qt所有控件
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385