• QTableWidget添加表头菜单 并 可以通过表头菜单隐藏选中列


    一、首先需要给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();
    }

    四、效果图

      

  • 相关阅读:
    C++多线程同步技巧(三)--- 互斥体
    Windows核心编程笔记之进程
    HTTP协议之分块传输与分段编码
    CVE-2013-2551:Internet Explore VML COALineDashStyleArray 整数溢出漏洞简单调试分析
    SQLServer数据库及注入方法
    Windows核心编程笔记之内核对象
    Windows核心编程笔记之错误处理
    Windows核心编程笔记之处理字符串
    CVE-2012-0774:Adobe Reader TrueType 字体整数溢出漏洞调试分析
    CVE-2012-1876:Internet Exporter MSHTML.DLL CaculateMinMax 堆溢出简单分析
  • 原文地址:https://www.cnblogs.com/jiangson/p/9119410.html
Copyright © 2020-2023  润新知