add setText AlignCenter
This commit is contained in:
@@ -361,8 +361,17 @@ void QMainwindow::updateCodeTable()
|
|||||||
m_model->removeRows(0, m_model->rowCount());
|
m_model->removeRows(0, m_model->rowCount());
|
||||||
for (const auto& order : m_replyCodes) {
|
for (const auto& order : m_replyCodes) {
|
||||||
QList<QStandardItem*> rowItems;
|
QList<QStandardItem*> rowItems;
|
||||||
rowItems << new QStandardItem(order->stockCode);
|
|
||||||
rowItems << new QStandardItem(QString::number(order->stockQuantityTh/1000, 'f', 1) + "K");
|
QStandardItem* codeItem = new QStandardItem(order->stockCode);
|
||||||
|
codeItem->setTextAlignment(Qt::AlignCenter);
|
||||||
|
rowItems << codeItem;
|
||||||
|
|
||||||
|
//rowItems << new QStandardItem(order->stockCode);
|
||||||
|
|
||||||
|
QStandardItem* quanityItem = new QStandardItem(QString::number(order->stockQuantityTh / 1000, 'f', 1) + "K");
|
||||||
|
quanityItem->setTextAlignment(Qt::AlignCenter);
|
||||||
|
rowItems << quanityItem;
|
||||||
|
|
||||||
m_model->appendRow(rowItems);
|
m_model->appendRow(rowItems);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -1,22 +1,28 @@
|
|||||||
#include "ordertypedelegate.h"
|
#include "ordertypedelegate.h"
|
||||||
|
|
||||||
// ==================== OrderTypeDelegate ====================
|
/*
|
||||||
|
==================== Order Type Delegate ====================
|
||||||
|
订单类型代理:
|
||||||
|
根据不同订单类型,设置单元格格背景颜色
|
||||||
|
*/
|
||||||
OrderTypeDelegate::OrderTypeDelegate(QObject *parent)
|
OrderTypeDelegate::OrderTypeDelegate(QObject *parent)
|
||||||
: QStyledItemDelegate(parent)
|
: QStyledItemDelegate(parent)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void OrderTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
void OrderTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||||
const QModelIndex &index) const
|
const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
if (!index.isValid()) return;
|
if (!index.isValid()) return;
|
||||||
|
|
||||||
// 保存原始渲染状态
|
QStyleOptionViewItem opt = option;
|
||||||
painter->save();
|
initStyleOption(&opt, index);
|
||||||
|
|
||||||
// 获取单元格文本
|
|
||||||
|
opt.displayAlignment = Qt::AlignCenter; // 设置文字居中
|
||||||
|
|
||||||
|
// 获取订单类型
|
||||||
QString type = index.data(Qt::DisplayRole).toString();
|
QString type = index.data(Qt::DisplayRole).toString();
|
||||||
|
|
||||||
// 设置背景色
|
// 设置背景色
|
||||||
if (type == "买入") {
|
if (type == "买入") {
|
||||||
painter->fillRect(option.rect, m_buyColor);
|
painter->fillRect(option.rect, m_buyColor);
|
||||||
@@ -24,20 +30,20 @@ void OrderTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt
|
|||||||
else if (type == "卖出") {
|
else if (type == "卖出") {
|
||||||
painter->fillRect(option.rect, m_sellColor);
|
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();
|
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;
|
QStyleOptionViewItem opt = option;
|
||||||
initStyleOption(&opt, index);
|
initStyleOption(&opt, index);
|
||||||
|
|
||||||
|
// 设置文字居中
|
||||||
|
opt.displayAlignment = Qt::AlignCenter;
|
||||||
|
|
||||||
// 检查是否需要高亮 - 通过映射到源模型获取高亮状态
|
// 检查是否需要高亮 - 通过映射到源模型获取高亮状态
|
||||||
QAbstractItemModel *model = const_cast<QAbstractItemModel*>(index.model());
|
QAbstractItemModel *model = const_cast<QAbstractItemModel*>(index.model());
|
||||||
QSortFilterProxyModel *proxyModel = qobject_cast<QSortFilterProxyModel*>(model);
|
QSortFilterProxyModel *proxyModel = qobject_cast<QSortFilterProxyModel*>(model);
|
||||||
@@ -111,5 +120,12 @@ void HighlightDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt
|
|||||||
else {
|
else {
|
||||||
// 正常绘制
|
// 正常绘制
|
||||||
QStyledItemDelegate::paint(painter, opt, index);
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,6 +95,11 @@ void QBigOrderViewer::initUI()
|
|||||||
<< "时间"
|
<< "时间"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 设置表头文字居中
|
||||||
|
for (int col = 0; col < m_model->columnCount(); ++col) {
|
||||||
|
m_model->setHeaderData(col, Qt::Horizontal, Qt::AlignCenter, Qt::TextAlignmentRole);
|
||||||
|
}
|
||||||
|
|
||||||
// 表格属性
|
// 表格属性
|
||||||
m_tableView->setItemDelegateForColumn(2, m_typeDelegate);
|
m_tableView->setItemDelegateForColumn(2, m_typeDelegate);
|
||||||
m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||||
@@ -169,28 +174,48 @@ void QBigOrderViewer::initConnections()
|
|||||||
|
|
||||||
void QBigOrderViewer::updateView()
|
void QBigOrderViewer::updateView()
|
||||||
{
|
{
|
||||||
m_model->removeRows(0, m_model->rowCount());
|
m_model->removeRows(0, m_model->rowCount());
|
||||||
for (const auto& order : m_currentOrders) {
|
for (const auto& order : m_currentOrders) {
|
||||||
QList<QStandardItem*> rowItems;
|
QList<QStandardItem*> rowItems;
|
||||||
rowItems << new QStandardItem(order->code);
|
|
||||||
rowItems << new QStandardItem(order->name);
|
// 创建项并设置文本居中
|
||||||
|
QStandardItem* codeItem = new QStandardItem(order->code);
|
||||||
|
codeItem->setTextAlignment(Qt::AlignCenter);
|
||||||
|
rowItems << codeItem;
|
||||||
|
|
||||||
|
QStandardItem* nameItem = new QStandardItem(order->name);
|
||||||
|
nameItem->setTextAlignment(Qt::AlignCenter);
|
||||||
|
rowItems << nameItem;
|
||||||
|
|
||||||
int nType = order->nBigOrderType;
|
int nType = order->nBigOrderType;
|
||||||
if(nType)
|
QStandardItem* typeItem = new QStandardItem(nType ? "多单" : "空单");
|
||||||
rowItems << new QStandardItem("多单");
|
typeItem->setTextAlignment(Qt::AlignCenter);
|
||||||
else
|
rowItems << typeItem;
|
||||||
rowItems << new QStandardItem("空单");
|
|
||||||
rowItems << new QStandardItem(QString::number(std::fabs(order->volume/1000)) + "K");
|
QStandardItem* volumeItem = new QStandardItem(QString::number(std::fabs(order->volume / 1000)) + "K");
|
||||||
rowItems << new QStandardItem(QString::number(order->price, 'f', 2));
|
volumeItem->setTextAlignment(Qt::AlignCenter);
|
||||||
rowItems << new QStandardItem(QString::number(order->level));
|
rowItems << volumeItem;
|
||||||
|
|
||||||
|
QStandardItem* priceItem = new QStandardItem(QString::number(order->price, 'f', 2));
|
||||||
|
priceItem->setTextAlignment(Qt::AlignCenter);
|
||||||
|
rowItems << priceItem;
|
||||||
|
|
||||||
|
QStandardItem* levelItem = new QStandardItem(QString::number(order->level));
|
||||||
|
levelItem->setTextAlignment(Qt::AlignCenter);
|
||||||
|
rowItems << levelItem;
|
||||||
|
|
||||||
QString str = order->svrRecvTime.mid(11);
|
QString str = order->svrRecvTime.mid(11);
|
||||||
if (str == nullptr)
|
if (str == nullptr)
|
||||||
{
|
{
|
||||||
QDateTime dateTime = QDateTime::currentDateTime();
|
QDateTime dateTime = QDateTime::currentDateTime();
|
||||||
str = dateTime.toString("hh:mm:ss");
|
str = dateTime.toString("hh:mm:ss");
|
||||||
}
|
}
|
||||||
rowItems << new QStandardItem(str);
|
QStandardItem* timeItem = new QStandardItem(str);
|
||||||
m_model->appendRow(rowItems);
|
timeItem->setTextAlignment(Qt::AlignCenter);
|
||||||
}
|
rowItems << timeItem;
|
||||||
|
|
||||||
|
m_model->appendRow(rowItems);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void QBigOrderViewer::applyFilters()
|
void QBigOrderViewer::applyFilters()
|
||||||
@@ -279,11 +304,6 @@ void QBigOrderViewer::onBigOrderAdded(const BigOrderInfo &order)
|
|||||||
break; // 找到后退出循环
|
break; // 找到后退出循环
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//// 通知视图更新
|
|
||||||
//QModelIndex topLeft = m_model->index(row, 0);
|
|
||||||
//QModelIndex bottomRight = m_model->index(row, m_model->columnCount() - 1);
|
|
||||||
//emit m_model->dataChanged(topLeft, bottomRight);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 确保代理模型按时间降序排序,新数据在顶部
|
// 确保代理模型按时间降序排序,新数据在顶部
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
00581,1.02e+06
|
00581,1.02e+06
|
||||||
03383,800000
|
03383,800000
|
||||||
02666,500000
|
02666,500000
|
||||||
00839,500000
|
00839,450000
|
||||||
06098,600000
|
06098,600000
|
||||||
06865,50000
|
06865,50000
|
||||||
00700,8000
|
00700,8000
|
||||||
|
|||||||
|
Reference in New Issue
Block a user