一、最终效果
二、 重写QTableWidget类
mytablewidget.h
#ifndef MYTABLEWIDGET_H
#define MYTABLEWIDGET_H#include <QTableWidget>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 <QTableWidgetItem>
#include <QHeaderView>
#include <QDebug>
#include <QMouseEvent>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<QMouseEvent*>(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 <QMainWindow>
#include "mytablewidget.h"
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic: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 <QHBoxLayout>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;
}