1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- #include "ColorDelegate.h"
- #include <qchar.h>
- #include <qcolor.h>
- void ColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
- {
- QStyleOptionViewItem opt = option;
- initStyleOption(&opt, index);
- // 获取文本
- QString text = index.data().toString();
- // 获取传入的数据
- bool isConnected = index.data(Qt::UserRole + 1).toBool();
- // 设置颜色
- if (isConnected)
- {
- opt.palette.setColor(QPalette::Text, QColor("#D2D2D2"));
- }
- else
- {
- opt.palette.setColor(QPalette::Text, QColor("#D21F21"));
- }
- // 清空 opt 的文本以避免重复绘制
- opt.text.clear();
- // 调用基类的 paint 方法以应用 QSS 样式
- // QStyledItemDelegate::paint(painter, opt, index);
- // 自定义绘制逻辑
- painter->save();
-
- // 设置悬浮效果
- if (option.state & QStyle::State_MouseOver)
- {
- painter->fillRect(option.rect, QColor("#438EFF"));
- }
- painter->setPen(opt.palette.color(QPalette::Text));
- // 获取样式表中的 padding-left 值
- QRect textRect = opt.rect.adjusted(12, 0, 0, 0);
- painter->drawText(textRect, Qt::AlignVCenter | Qt::TextSingleLine, text);
- painter->restore();
- }
|