From 0306b1742d27b3643f352b0df87b5589fb0826f1 Mon Sep 17 00:00:00 2001 From: duanshengchao <519970194@qq.com> Date: Thu, 23 Oct 2025 19:05:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AE=8C=E6=88=90=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E4=B8=AD=E6=93=8D=E4=BD=9C=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E7=9A=84=E4=B8=89=E6=80=81(normal\hover\pressed)=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/alarmEventDataView.h | 3 +++ source/alarmEventDataView.cpp | 48 ++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/include/alarmEventDataView.h b/include/alarmEventDataView.h index 5b6d971..b36e2af 100644 --- a/include/alarmEventDataView.h +++ b/include/alarmEventDataView.h @@ -147,9 +147,12 @@ private: int borderWidth; int borderRaius; QString text; + //int mouseState = 0; //0Normal,1Hover,2Pressed }; QTableView* m_tableView; + QPoint m_mousePositon; + bool m_buttonColumnIsPress; ControlButton m_btnConfirm; ControlButton m_btnReplay; }; diff --git a/source/alarmEventDataView.cpp b/source/alarmEventDataView.cpp index 13d03c7..9500b1a 100644 --- a/source/alarmEventDataView.cpp +++ b/source/alarmEventDataView.cpp @@ -4,7 +4,8 @@ #include #include #include -#include +#include +//#include ///////------AlarmEventDataFilter----- AlarmEventDataFilter::AlarmEventDataFilter() @@ -69,7 +70,7 @@ AlarmEventDataModel::AlarmEventDataModel(QObject* parent) //实时数据测试 m_simulatedDataTimer = new QTimer(this); connect(m_simulatedDataTimer, &QTimer::timeout, this, &AlarmEventDataModel::onTimeoutSimulateData); - m_simulatedDataTimer->start(2000); + m_simulatedDataTimer->start(5000); } AlarmEventDataModel::~AlarmEventDataModel() @@ -400,7 +401,16 @@ void AlarmEventDataDelegate::paint(QPainter* painter, const QStyleOptionViewItem rectPen.setColor(m_btnConfirm.borderColor); rectPen.setWidth(m_btnConfirm.borderWidth); 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->setPen(m_btnConfirm.textColor); 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.setWidth(m_btnReplay.borderWidth); 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->setPen(m_btnReplay.textColor); 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) { + if(m_tableView && index.column() == m_tableView->model()->columnCount() - 1) //只处理操作按钮所在列的事件 + { + QMouseEvent *mouseEvent = static_cast(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); }