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); }