QTableWidget实现鼠标悬停整行高亮显示

一、最终效果
 

QTableWidget实现鼠标悬停整行高亮显示_第1张图片

二、 重写QTableWidget类

mytablewidget.h

#ifndef MYTABLEWIDGET_H
#define MYTABLEWIDGET_H

#include 

class MyTableWidget : public QTableWidget
{
public:
    explicit MyTableWidget(QWidget* parent = nullptr);

protected:
    void leaveEvent(QEvent *event);  //鼠标离开事件

    bool eventFilter(QObject *obj, QEvent *event);  //事件过滤器

private slots:
    void itemChange();  //鼠标点击

private:
    int m_oldEnteredRow;  //上一次改变背景色的行号
};

#endif // MYTABLEWIDGET_H

mytablewidget.cpp

#include "mytablewidget.h"
#include 
#include 
#include 
#include 

MyTableWidget::MyTableWidget(QWidget *parent) : QTableWidget(parent)
{
    this->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中

    //设置表格控件不可编辑
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);
    this->setSortingEnabled(false);

    this->verticalHeader()->hide(); //隐藏竖标题栏
    this->horizontalHeader()->hide(); //隐藏横标题栏

    this->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);  //自动设置行高
    this->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);  //自动设置列宽
    //this->verticalHeader()->setStretchLastSection(true); //最后一行拉伸
    this->horizontalHeader()->setStretchLastSection(true); //最后一列拉伸

    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);  //去掉水平滚动条
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);  //去掉垂直滚动条

    this->setShowGrid(false); //不显示线框
    this->setStyleSheet(QString("QTableWidget{background-color:red;color:green;outline:0px;}"
                                "QTableWidget::item:selected{background-color:green;color:red;outline:0px;}"));

    connect(this, &MyTableWidget::itemSelectionChanged, this, &MyTableWidget::itemChange);
    m_oldEnteredRow = -1;

    //取消默认第一个item被选中
    this->setCurrentItem(NULL);
}

/**
 * @brief MyTableWidget::itemChange 鼠标点击item之外空间时,取消之前被选中的item
 */
void MyTableWidget::itemChange()
{
    //鼠标点击item之外空间时
    if(this->selectedItems().size() == 0)
    {
        this->setCurrentItem(NULL);
    }
}

/**
 * @brief MyTableWidget::leaveEvent 鼠标不在table上时,还原之前的背景色
 * @param event
 */
void MyTableWidget::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);

    //还原之前的背景色
    if(m_oldEnteredRow != -1)
    {
        QTableWidgetItem* itemOld;
        itemOld = this->item(m_oldEnteredRow, 0);
        itemOld->setBackgroundColor(Qt::red);

        itemOld = this->item(m_oldEnteredRow, 1);
        itemOld->setBackgroundColor(Qt::red);

        m_oldEnteredRow = -1;
    }
}

/**
 * @brief MyTableWidget::eventFilter 事件过滤器
 * @param obj
 * @param event
 * @return
 */
bool MyTableWidget::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::HoverMove)
    {
        QMouseEvent* mouseEvent = static_cast(event);
        QModelIndex index = this->indexAt(mouseEvent->pos());
        if(index.isValid())  //鼠标悬停在item上
        {
            if(m_oldEnteredRow != index.row())
            {
                //还原之前的背景色
                if(m_oldEnteredRow != -1)
                {
                    QTableWidgetItem* itemOld;
                    itemOld = this->item(m_oldEnteredRow, 0);
                    itemOld->setBackgroundColor(Qt::red);

                    itemOld = this->item(m_oldEnteredRow, 1);
                    itemOld->setBackgroundColor(Qt::red);
                }

                //更改当前行的背景色
                int row = index.row();
                QTableWidgetItem* item;
                item = this->item(row, 1);
                item->setBackgroundColor(Qt::yellow);
                item = this->item(row, 0);
                item->setBackgroundColor(Qt::yellow);

                m_oldEnteredRow = row;
            }
        }
        else //鼠标悬停在item之外
        {
            //还原之前的背景色
            if(m_oldEnteredRow != -1)
            {
                QTableWidgetItem* itemOld;
                itemOld = this->item(m_oldEnteredRow, 0);
                itemOld->setBackgroundColor(Qt::red);

                itemOld = this->item(m_oldEnteredRow, 1);
                itemOld->setBackgroundColor(Qt::red);

                m_oldEnteredRow = -1;
            }
        }
    }

    return QWidget::eventFilter(obj, event);
}

三、类的使用 

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include "mytablewidget.h"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    MyTableWidget* m_tableWidget;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_tableWidget = new MyTableWidget;

    //添加4行2列数据
    m_tableWidget->setRowCount(4);
    m_tableWidget->setColumnCount(2);
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            QTableWidgetItem* item = new QTableWidgetItem;
            item->setText(QString("%1%2").arg(i).arg(j));
            m_tableWidget->setItem(i, j, item);
        }
    }

    //事件过滤器
    this->installEventFilter(m_tableWidget);

    this->setCentralWidget(m_tableWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}

你可能感兴趣的:(Qt小功能,qt)