一、首先需要给QTableWidget安装事件
二、重新实现eventFilter(QObject *obj, QEvent *ev)函数
三、下面是实现代码
① .h文件
#ifndef TABLEHEADERMENU_H #define TABLEHEADERMENU_H #include <QtWidgets/QMainWindow> #include "ui_tableheadermenu.h" #include <QStringList> #include <QMenu> #include <QEvent> class TableHeaderMenu : public QMainWindow { Q_OBJECT public: TableHeaderMenu(QWidget *parent = 0); ~TableHeaderMenu(); //设置表头菜单 void setHeaderMenu(); public slots: //表头菜单响应槽函数 void slotVisibleItemsChanged(bool checked); protected: //重写事件 bool eventFilter(QObject *obj, QEvent *ev); private: Ui::TableHeaderMenuClass ui; //水平表头 QStringList horHeaderStr; //表头菜单 QMenu *m_headerMenu; }; #endif // TABLEHEADERMENU_H
② .cpp文件
#include "tableheadermenu.h" #include <QContextMenuEvent> TableHeaderMenu::TableHeaderMenu(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); //设置列数 ui.tableWidget->setColumnCount(5); //表头 horHeaderStr << QString::fromLocal8Bit("姓名") << QString::fromLocal8Bit("年龄") << QString::fromLocal8Bit("性别") << QString::fromLocal8Bit("兴趣") << QString::fromLocal8Bit("特长"); //设置表头 ui.tableWidget->setHorizontalHeaderLabels(horHeaderStr); //安装水平表头事件 ui.tableWidget->horizontalHeader()->installEventFilter(this); setHeaderMenu(); } TableHeaderMenu::~TableHeaderMenu() { } /* 实现表头菜单 */ void TableHeaderMenu::setHeaderMenu() { m_headerMenu = new QMenu(this); QAction *actionName = new QAction(QString::fromLocal8Bit("姓名"), this); QAction *actionAge = new QAction(QString::fromLocal8Bit("年龄"), this); QAction *actionSex = new QAction(QString::fromLocal8Bit("性别"), this); QAction *actionInterset = new QAction(QString::fromLocal8Bit("兴趣"), this); QAction *actionSpeciality = new QAction(QString::fromLocal8Bit("特长"), this); actionName->setObjectName(QString::number(0)); //0隐藏第一列 actionName->setCheckable(true); actionName->setChecked(true); actionAge->setObjectName(QString::number(1)); //1隐藏第二列 actionAge->setCheckable(true); actionAge->setChecked(true); actionSex->setObjectName(QString::number(2)); //2隐藏第三列 actionSex->setCheckable(true); actionSex->setChecked(true); actionInterset->setObjectName(QString::number(3)); //3隐藏第四列 actionInterset->setCheckable(true); actionInterset->setChecked(true); actionSpeciality->setObjectName(QString::number(4));//4隐藏第五列 actionSpeciality->setCheckable(true); actionSpeciality->setChecked(true); //连接菜单响应槽函数 connect(actionName, &QAction::toggled, this, &TableHeaderMenu::slotVisibleItemsChanged); connect(actionAge, &QAction::toggled, this, &TableHeaderMenu::slotVisibleItemsChanged); connect(actionSex, &QAction::toggled, this, &TableHeaderMenu::slotVisibleItemsChanged); connect(actionInterset, &QAction::toggled, this, &TableHeaderMenu::slotVisibleItemsChanged); connect(actionSpeciality, &QAction::toggled, this, &TableHeaderMenu::slotVisibleItemsChanged); //添加菜单 m_headerMenu->addAction(actionName); m_headerMenu->addAction(actionAge); m_headerMenu->addAction(actionSex); m_headerMenu->addAction(actionInterset); m_headerMenu->addAction(actionSpeciality); } /* 表头菜单响应函数 */ void TableHeaderMenu::slotVisibleItemsChanged(bool checked) { QObject *obj = dynamic_cast<QObject *>(sender()); int n = obj->objectName().toInt(); ui.tableWidget->setColumnHidden(n, !checked); } /* 重写事件 */ bool TableHeaderMenu::eventFilter(QObject *obj, QEvent *ev) { if (obj == ui.tableWidget->horizontalHeader()){ if (ev->type() == QEvent::ContextMenu){ QContextMenuEvent *menuEvent = static_cast<QContextMenuEvent *>(ev); m_headerMenu->popup(menuEvent->globalPos()); return true; } else{ return false; } } else{ return eventFilter(obj, ev); } }
③ 主函数
#include "tableheadermenu.h" #include <QtWidgets/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); TableHeaderMenu w; w.show(); return a.exec(); }
四、效果图