feat:完成告警列表中操作按钮的三态(normal\hover\pressed)展示

This commit is contained in:
duanshengchao 2025-10-23 19:05:48 +08:00
parent 3828bd6f9c
commit 0306b1742d
2 changed files with 47 additions and 4 deletions

View File

@ -147,9 +147,12 @@ private:
int borderWidth; int borderWidth;
int borderRaius; int borderRaius;
QString text; QString text;
//int mouseState = 0; //0Normal,1Hover,2Pressed
}; };
QTableView* m_tableView; QTableView* m_tableView;
QPoint m_mousePositon;
bool m_buttonColumnIsPress;
ControlButton m_btnConfirm; ControlButton m_btnConfirm;
ControlButton m_btnReplay; ControlButton m_btnReplay;
}; };

View File

@ -4,7 +4,8 @@
#include <QPainter> #include <QPainter>
#include <QTimer> #include <QTimer>
#include <QRandomGenerator> #include <QRandomGenerator>
#include <QApplication> #include <QMouseEvent>
//#include <QApplication>
///////------AlarmEventDataFilter----- ///////------AlarmEventDataFilter-----
AlarmEventDataFilter::AlarmEventDataFilter() AlarmEventDataFilter::AlarmEventDataFilter()
@ -69,7 +70,7 @@ AlarmEventDataModel::AlarmEventDataModel(QObject* parent)
//实时数据测试 //实时数据测试
m_simulatedDataTimer = new QTimer(this); m_simulatedDataTimer = new QTimer(this);
connect(m_simulatedDataTimer, &QTimer::timeout, this, &AlarmEventDataModel::onTimeoutSimulateData); connect(m_simulatedDataTimer, &QTimer::timeout, this, &AlarmEventDataModel::onTimeoutSimulateData);
m_simulatedDataTimer->start(2000); m_simulatedDataTimer->start(5000);
} }
AlarmEventDataModel::~AlarmEventDataModel() AlarmEventDataModel::~AlarmEventDataModel()
@ -400,7 +401,16 @@ void AlarmEventDataDelegate::paint(QPainter* painter, const QStyleOptionViewItem
rectPen.setColor(m_btnConfirm.borderColor); rectPen.setColor(m_btnConfirm.borderColor);
rectPen.setWidth(m_btnConfirm.borderWidth); rectPen.setWidth(m_btnConfirm.borderWidth);
painter->setPen(rectPen); painter->setPen(rectPen);
painter->setBrush(m_btnConfirm.normalBgColor); QColor brushColor = m_btnConfirm.normalBgColor;
bool isHover = opt.state.testFlag(QStyle::State_MouseOver) && m_btnConfirm.rect.contains(m_mousePositon); //opt.state.testFlag(QStyle::State_MouseOver)表示鼠标是否悬停在该视图项(即单元格)上
//bool isPressed = opt.state.testFlag(QStyle::State_Sunken) && m_btnConfirm.rect.contains(m_mousePositon);//opt.state.testFlag(QStyle::State_Sunken)表示鼠标是否在该视图项(即单元格)上按下
bool isPressed = m_buttonColumnIsPress && m_btnConfirm.rect.contains(m_mousePositon); //opt.state.testFlag(QStyle::State_Sunken)总是为false且未找到原因
//注意判断顺序一定要先先判断press
if(isPressed)
brushColor = m_btnConfirm.pressedBgColor;
else if(isHover)
brushColor = m_btnConfirm.hoverBgColor;
painter->setBrush(brushColor);
painter->drawRoundedRect(m_btnConfirm.rect, m_btnConfirm.borderRaius, m_btnConfirm.borderRaius); painter->drawRoundedRect(m_btnConfirm.rect, m_btnConfirm.borderRaius, m_btnConfirm.borderRaius);
painter->setPen(m_btnConfirm.textColor); painter->setPen(m_btnConfirm.textColor);
painter->drawText(m_btnConfirm.rect, Qt::AlignCenter, m_btnConfirm.text); painter->drawText(m_btnConfirm.rect, Qt::AlignCenter, m_btnConfirm.text);
@ -408,7 +418,15 @@ void AlarmEventDataDelegate::paint(QPainter* painter, const QStyleOptionViewItem
rectPen.setColor(m_btnReplay.borderColor); rectPen.setColor(m_btnReplay.borderColor);
rectPen.setWidth(m_btnReplay.borderWidth); rectPen.setWidth(m_btnReplay.borderWidth);
painter->setPen(rectPen); painter->setPen(rectPen);
painter->setBrush(m_btnReplay.normalBgColor); brushColor = m_btnReplay.normalBgColor;
isHover = opt.state.testFlag(QStyle::State_MouseOver) && m_btnReplay.rect.contains(m_mousePositon);
//isPressed = opt.state.testFlag(QStyle::State_Sunken) && isHover;
isPressed = m_buttonColumnIsPress && m_btnReplay.rect.contains(m_mousePositon);
if(isPressed)
brushColor = m_btnReplay.pressedBgColor;
else if(isHover)
brushColor = m_btnReplay.hoverBgColor;
painter->setBrush(brushColor);
painter->drawRoundedRect(m_btnReplay.rect, m_btnReplay.borderRaius, m_btnReplay.borderRaius); painter->drawRoundedRect(m_btnReplay.rect, m_btnReplay.borderRaius, m_btnReplay.borderRaius);
painter->setPen(m_btnReplay.textColor); painter->setPen(m_btnReplay.textColor);
painter->drawText(m_btnReplay.rect, Qt::AlignCenter, m_btnReplay.text); painter->drawText(m_btnReplay.rect, Qt::AlignCenter, m_btnReplay.text);
@ -433,6 +451,28 @@ void AlarmEventDataDelegate::paint(QPainter* painter, const QStyleOptionViewItem
bool AlarmEventDataDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) bool AlarmEventDataDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
{ {
if(m_tableView && index.column() == m_tableView->model()->columnCount() - 1) //只处理操作按钮所在列的事件
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
m_mousePositon = mouseEvent->pos();
m_buttonColumnIsPress = false;
if(mouseEvent->type() == QEvent::MouseMove)
{
emit m_tableView->model()->dataChanged(index, index); //触发重绘
}
else if(mouseEvent->button() == Qt::LeftButton && mouseEvent->type() == QEvent::MouseButtonPress)
{
m_buttonColumnIsPress = true;
emit m_tableView->model()->dataChanged(index, index); //触发重绘
//qDebug() << option.state.testFlag(QStyle::State_Sunken);
}
else if(mouseEvent->button() == Qt::LeftButton && mouseEvent->type() == QEvent::MouseButtonRelease)
{
emit m_tableView->model()->dataChanged(index, index); //触发重绘
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index); return QStyledItemDelegate::editorEvent(event, model, option, index);
} }