add setText AlignCenter
This commit is contained in:
@@ -1,22 +1,28 @@
|
||||
#include "ordertypedelegate.h"
|
||||
|
||||
// ==================== OrderTypeDelegate ====================
|
||||
/*
|
||||
==================== Order Type Delegate ====================
|
||||
订单类型代理:
|
||||
根据不同订单类型,设置单元格格背景颜色
|
||||
*/
|
||||
OrderTypeDelegate::OrderTypeDelegate(QObject *parent)
|
||||
: QStyledItemDelegate(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void OrderTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid()) return;
|
||||
if (!index.isValid()) return;
|
||||
|
||||
// 保存原始渲染状态
|
||||
painter->save();
|
||||
QStyleOptionViewItem opt = option;
|
||||
initStyleOption(&opt, index);
|
||||
|
||||
// 获取单元格文本
|
||||
|
||||
opt.displayAlignment = Qt::AlignCenter; // 设置文字居中
|
||||
|
||||
// 获取订单类型
|
||||
QString type = index.data(Qt::DisplayRole).toString();
|
||||
|
||||
// 设置背景色
|
||||
if (type == "买入") {
|
||||
painter->fillRect(option.rect, m_buyColor);
|
||||
@@ -24,20 +30,20 @@ void OrderTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt
|
||||
else if (type == "卖出") {
|
||||
painter->fillRect(option.rect, m_sellColor);
|
||||
}
|
||||
|
||||
// // 添加悬停效果
|
||||
// if (option.state & QStyle::State_MouseOver) {
|
||||
// QColor hoverColor = (type == "买入")
|
||||
// ? m_buyColor.lighter(120)
|
||||
// : m_sellColor.lighter(120);
|
||||
// painter->fillRect(option.rect, hoverColor);
|
||||
// }
|
||||
|
||||
// 恢复原始渲染状态
|
||||
painter->save();
|
||||
painter->setFont(opt.font);
|
||||
painter->setPen(opt.palette.color(QPalette::Text));
|
||||
painter->drawText(opt.rect, Qt::AlignCenter, opt.text);
|
||||
painter->restore();
|
||||
|
||||
// 添加悬停效果
|
||||
if (option.state & QStyle::State_MouseOver) {
|
||||
QColor hoverColor = (type == "买入")
|
||||
? m_buyColor.lighter(120)
|
||||
: m_sellColor.lighter(120);
|
||||
painter->fillRect(option.rect, hoverColor);
|
||||
}
|
||||
|
||||
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
|
||||
|
||||
@@ -83,6 +89,9 @@ void HighlightDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt
|
||||
QStyleOptionViewItem opt = option;
|
||||
initStyleOption(&opt, index);
|
||||
|
||||
// 设置文字居中
|
||||
opt.displayAlignment = Qt::AlignCenter;
|
||||
|
||||
// 检查是否需要高亮 - 通过映射到源模型获取高亮状态
|
||||
QAbstractItemModel *model = const_cast<QAbstractItemModel*>(index.model());
|
||||
QSortFilterProxyModel *proxyModel = qobject_cast<QSortFilterProxyModel*>(model);
|
||||
@@ -111,5 +120,12 @@ void HighlightDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt
|
||||
else {
|
||||
// 正常绘制
|
||||
QStyledItemDelegate::paint(painter, opt, index);
|
||||
|
||||
// 正常绘制(居中)
|
||||
painter->save();
|
||||
painter->setFont(opt.font);
|
||||
painter->setPen(opt.palette.color(QPalette::Text));
|
||||
painter->drawText(opt.rect, Qt::AlignCenter, opt.text);
|
||||
painter->restore();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user