QTableWidget能满足大部分需求,但有时我们需要给QTableWidget增加一些小功能:

部分效果图:

QTableWidget表头中增加checkbox以及QTableWidget各种小知识_QTableWidget

1、可以在表头中增加checkbox,方法如下:

①、可以引用头文件scheckboxheaderview.h

#ifndef SCHECKBOXHEADERVIEW_H
#define SCHECKBOXHEADERVIEW_H
#include <QtGui>
#include <QPainter>
#include <QHeaderView>
#include <QStyleOptionButton>
#include <QStyle>
class SCheckBoxHeaderView : public QHeaderView
{
Q_OBJECT
private:
bool isChecked;
int m_checkColIdx;
public:
SCheckBoxHeaderView( int checkColumnIndex, Qt::Orientation orientation, QWidget * parent = 0) :
QHeaderView(orientation, parent) {
m_checkColIdx = checkColumnIndex;
isChecked = false;
}
signals:
void checkStausChange(bool);
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const {
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if (logicalIndex == m_checkColIdx) {
QStyleOptionButton option;
int width = 10;
for (int i=0; i<logicalIndex; ++i)
width += sectionSize( i );
option.rect = QRect(3, 5, 21, 21);
if (isChecked)
option.state = QStyle::State_On;
else
option.state = QStyle::State_Off;
this->style()->drawControl(QStyle::CE_CheckBox, &option, painter);
}
}
void mousePressEvent(QMouseEvent *event) {
if (visualIndexAt(event->pos().x()) == m_checkColIdx) {
isChecked = !isChecked;
this->updateSection(m_checkColIdx);
emit checkStausChange(isChecked);
}
QHeaderView::mousePressEvent(event);
}
};
#endif // SCHECKBOXHEADERVIEW_H

②、引用并使用   

m_checkHeader = new SCheckBoxHeaderView(0, Qt::Horizontal, this);
this->setHorizontalHeader(m_checkHeader); // 这个this指针的父为QTableWidget
connect(m_checkHeader, &SCheckBoxHeaderView::checkStausChange, [=](bool check){
qDebug() << "is:" <<check;
});

2、在表格中设置某列(或某行)的最小(或最大)大小

QTableWidget提供了setColumnWidth()和setRowHeight()两个方法来设置某列(或某行)大小

QHeaderView提供了sectionResized信号,它的参数是这样的:void sectionResized(int logicalIndex, int oldSize, int newSize);

我们可以自建类(也可以直接用上述的1中我建好的类),然后继承(或组合)QHeaderView类,然后通过信号与槽来做处理,这里提供了一个方法。

这里以设置某列最小值为例:

bool m_min = false;
int m_minColumn = 0;
int m_minWidth = 20;

// m_checkHeader 为上述1中建好的类

connect(m_checkHeader, &SCheckBoxHeaderView::sectionResized, [=](int logicalIndex, int     
oldSize, int newSize){
if(m_min) {
if(m_minColumn == logicalIndex) {
if(newSize < m_minWidth) {
this->setColumnWidth(m_minColumn, m_minWidth);
}
}
}

});


void setColumnMinWidth(int column, int width) {
this->setColumnWidth(column, width);
m_min = true;
m_minWidth = width;
m_minColumn = column;
}

3、显示表格线 (QTableWidget提供了此方法)

setShowGrid(true);

4、设置表格内容不可编辑(QTableWidget提供了此方法)

setEditTriggers(QAbstractItemView::NoEditTriggers);

5、设置表格内表头字体加粗或者怎样,这里以加粗为例

QFont font = this->horizontalHeader()->font();
font.setBold(true);
this->horizontalHeader()->setFont(font); // 这个this指针的父为QTableWidget

6、设置表头背景色

this->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");  //  这个this指针的父为QTableWidget

7、设置表格列宽均等分

this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//使列完全填充并平分

8、设置表格某单元块内容,主要是QTableWidget的setCellWidget方法,用法可以参考下面的:

STableWidgetItem* title = new STableWidgetItem(row, QSize(wid - 1, 29),this);
title->setText(str);
title->setDeleteVisible(delVisible);
this->setCellWidget(row, i, title);

这里STableWidgetItem类是我自己创建的QWidget,可以自己添加QWidget中的内容,比如复选框、按钮等

9、表格单元格内字体居中 QTableWidgetItem

QTableWidgetItem* item = new QTableWidgetItem(str);
item->setTextAlignment(Qt::AlignCenter);
this->setItem(row, i, item); // 这个this指针的父为QTableWidget

STableWidgetItem* title = new STableWidgetItem(row, QSize(wid - 1, 29),this);