优化TableView显示效果

This commit is contained in:
duanshengchao 2025-04-01 18:10:48 +08:00
parent e0f33eb2de
commit dcd10a2590
4 changed files with 30 additions and 11 deletions

View File

@ -2,7 +2,7 @@
level=DEBUG level=DEBUG
maxSize=10485760 maxSize=10485760
backups=5 backups=5
consoleOutput=true consoleOutput=false
fileOutput=true fileOutput=true
[Database-250303170015088] [Database-250303170015088]
@ -14,3 +14,13 @@ database=postgres
username=postgres username=postgres
password=123456 password=123456
comment= comment=
[Database-250327111518571]
connName=MySQL
host=127.0.0.1
port=3306
dbType=QMYSQL
database=postgres
username=postgres
password=123456
comment=

View File

@ -12,19 +12,25 @@ AttributeTableDelegate::~AttributeTableDelegate()
void AttributeTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const void AttributeTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{ {
//先执行默认绘制(包括背景、文本等基础元素) //根据行号设置交替色
QStyledItemDelegate::paint(painter, option, index);
//处理加粗字体逻辑(在基础绘制之上叠加)
if(/*index.column() == 0 || */index.data(Qt::UserRole + 100).toBool())
{
QStyleOptionViewItem opt = option; QStyleOptionViewItem opt = option;
initStyleOption(&opt, index); initStyleOption(&opt, index);
if ((index.row() + 1) % 2 == 0)
{
//opt.backgroundBrush = QBrush(QColor(243, 245, 249));
painter->fillRect(opt.rect, QColor(240, 248, 255));
}
//处理加粗字体逻辑
if(/*index.column() == 0 || */index.data(Qt::UserRole + 100).toBool())
{
opt.font.setBold(true); opt.font.setBold(true);
//opt.palette.setColor(QPalette::Text, Qt::red); //opt.palette.setColor(QPalette::Text, Qt::red);
QStyledItemDelegate::paint(painter, opt, index);
} }
//先执行默认绘制(包括背景、文本等基础元素)
QStyledItemDelegate::paint(painter, opt, index);
//最后绘制删除线(确保位于最顶层) //最后绘制删除线(确保位于最顶层)
if(m_tableView && m_tableView->model()) if(m_tableView && m_tableView->model())
{ {

View File

@ -12,6 +12,8 @@ AttributeView::AttributeView(const ModelAttributeGroup& modelAttributeGroup, QWi
, m_modelAttributeGroup(modelAttributeGroup) , m_modelAttributeGroup(modelAttributeGroup)
{ {
m_tableView = new QTableView(this); m_tableView = new QTableView(this);
// m_tableView->setAlternatingRowColors(true);
// m_tableView->setStyleSheet("QTableView{alternate-background-color: rgb(240, 248, 255);}");
m_tableView->verticalHeader()->setVisible(false); m_tableView->verticalHeader()->setVisible(false);
//m_tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter); //m_tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);

View File

@ -68,7 +68,7 @@ QTabBar::tab:selected
QHeaderView QHeaderView
{ {
background-color: rgb(250, 250, 250); background-color: rgb(250, 250, 250);
} }
QHeaderView::section QHeaderView::section
{ {
@ -76,15 +76,16 @@ QHeaderView::section
QTableView QTableView
{ {
outline:0px; outline:0px;
} }
QTableView::item QTableView::item
{ {
border:0px; border:0px;
background-color:transparent;
} }
QTableView::item:hover QTableView::item:hover
{ {
background-color: rgb(240, 248, 255); background-color:transparent;
} }
QTableView::item:selected QTableView::item:selected
{ {
@ -94,7 +95,7 @@ QTableView::item:selected
QTreeView QTreeView
{ {
outline:0px; outline:0px;
} }
QTreeView::item QTreeView::item
{ {