qt qtablewidget自定义表头
目前项目需要通过自定义表头,来实现某些效果。现在定义了QComBox和QCheckBox,直接上代码:
实现了QCheckBox的三态显示.
h文件如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTableView>
#include <QStandardItemModel>
#include <QHeaderView>
#include <QCheckBox>
#include <QComboBox>class CustomHeaderView:public QHeaderView
{Q_OBJECTpublic:// 默认水平方向CustomHeaderView(Qt::Orientation ori = Qt::Horizontal, QWidget*parent = 0);//自定义头部,主要实现这个函数
protected:void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;public slots:void slt_checkbox_click(bool);void slt_checkbox_stateChanged(int state);void slt_combox_item_click(QString );void slt_combox_item_click(int currentIndex);private:QCheckBox *m_checkbox;QComboBox *m_combox;
};class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = 0);~MainWindow();private:QTableView *m_tabView;CustomHeaderView *head;};#endif // MAINWINDOW_H
cpp如下
#include <QDebug>
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{this->setFixedSize(800,480);m_tabView = new QTableView(this);m_tabView->setFixedSize(800,480);QStandardItemModel *model = new QStandardItemModel();model->setColumnCount(6);m_tabView->setModel(model);/*CustomHeaderView **/head = new CustomHeaderView(Qt::Horizontal,this);head->setSectionResizeMode(QHeaderView::Fixed);m_tabView->setHorizontalHeader(head);
}MainWindow::~MainWindow()
{}CustomHeaderView::CustomHeaderView(Qt::Orientation ori, QWidget *parent):QHeaderView(ori,parent)
{m_checkbox = new QCheckBox(this);m_combox = new QComboBox(this);m_checkbox->setTristate(true);m_combox->addItem("item1");m_combox->addItem("item2");m_combox->addItem("item3");// connect(m_checkbox,SIGNAL(clicked(bool)),this, SLOT(slt_checkbox_click(bool)));connect(m_checkbox,SIGNAL(stateChanged(int)),this, SLOT(slt_checkbox_stateChanged(int)));
// connect(m_combox, SIGNAL(currentIndexChanged(QString)),this,SLOT(slt_combox_item_click(QString)));connect(m_combox, SIGNAL(currentIndexChanged(int)),this,SLOT(slt_combox_item_click(int)));
}void CustomHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{QHeaderView::paintSection(painter,rect,logicalIndex);//logicalIndex 当前第几列,也可以自定义显示其他控件;if(logicalIndex == 0){QRect tmp;tmp.setSize(QSize(20,20));tmp.moveCenter(rect.center());m_checkbox->setGeometry(tmp);}else if(logicalIndex ==2){m_combox->setGeometry(rect);}
}void CustomHeaderView::slt_checkbox_click(bool state)
{
//qDebug() << "slt_checkbox_click" << state;//此处可以自定义信号
}void CustomHeaderView::slt_checkbox_stateChanged(int state)
{m_checkbox->setCheckState((Qt::CheckState)state);
}void CustomHeaderView::slt_combox_item_click(QString itemstr)
{// slt_checkbox_stateChanged((Qt::CheckState)state);qDebug() << "slt_combox_item_click" << itemstr;//此处可以自定义信号}void CustomHeaderView::slt_combox_item_click(int currentIndex)
{slt_checkbox_stateChanged((Qt::CheckState)currentIndex);qDebug() << "slt_combox_item_click" << currentIndex;//此处可以自定义信号}
main.cpp文件
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
pro文件如下。
#-------------------------------------------------
#
# Project created by QtCreator 2020-12-30T15:16:30
#
#-------------------------------------------------QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = CustomTableHeadView
TEMPLATE = appSOURCES += main.cpp\mainwindow.cppHEADERS += mainwindow.h
可以通过这个实现一些其它的应用场景,特此记录一下。
下载链接如下:
qttablewidge自定义表头QHeaderView资源-CSDN下载