QTableView/QTableWidget单元格自适应拉伸

QTableView及QTableWidget实现自适应调整表格单元格的的宽度以及高度,具体实现效果如下:

一、宽度自适应调整:

1、当表格的宽度大于内容的总宽度时,能够根据每个单元格内容的宽度比例,自动拉伸每列的宽度,来填满整个表格视图宽度;
2、如果表格宽度小于单元格内容或最小宽度之和,单元格宽度将不再继续缩小,将自动显示水平滚动条来显示表格内容。
实现效果如下:
表格宽度自适应调整
具体实现代码如下:

void CustomTableView::autoAdjustTableItemWidth()
{
	// 设置水平滚动条策略为按需显示
	this->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	// 获取水平表头
	QHeaderView *hHeaderView = this->horizontalHeader();
	// 设置列的调整模式为根据内容自动调整大小
	hHeaderView->setSectionResizeMode(QHeaderView::ResizeToContents);
	// 计算所有列的总宽度
	int section_total_width = 0;
	for (int col = 0; col < hHeaderView->count(); ++col)
	    section_total_width += hHeaderView->sectionSize(col);
	
	// 恢复列的调整模式为交互式
	hHeaderView->setSectionResizeMode(QHeaderView::Interactive);
	// 获取水平表头的宽度
	int hHeaderWidth = hHeaderView->width();
	// 如果水平表头的宽度大于所有列的总宽度
	if (hHeaderWidth > section_total_width){
	    // 计算缩放比例
	    float scale = hHeaderWidth / float(section_total_width);
	    int width_sum = 0;
	    // 根据缩放比例调整每列的宽度
	    for (int col = 0; col < hHeaderView->count() - 1; ++col) {
	        int cell_width = hHeaderView->sectionSize(col) * scale;
	        hHeaderView->resizeSection(col, cell_width);
	        width_sum += cell_width;
	    }
	    // 调整最后一列的宽度,确保总宽度与表头宽度一致
	    hHeaderView->resizeSection(hHeaderView->count() - 1, hHeaderWidth - width_sum);
	}
}

二、高度自适应调整:

1、当表格高度大于单元格最小高度之和,并且小于单元格最大高度之和时,将自动拉伸表格单元格的高度,来填满整个表格视图高度;
2、当表格高度小于单元格最小高度之和,单元格高度将不再继续缩小,将自动显示垂直滚动条来显示表格内容;
3、当表格高度大于单元格最大高度之和,单元格高度将不再继续放大,表格下方留白显示。
实现效果如下:
表格高度自适应调整
具体实现代码如下:

void CustomTableView::autoAdjustTableItemHeight()
{
    // 获取垂直表头
    QHeaderView *vHeaderView = this->verticalHeader();
    // 获取垂直表头的高度
    int vHeaderHeight = vHeaderView->height();
    // 计算每行的理论高度,以保证均匀分配表头高度
    int cell_height = vHeaderHeight / vHeaderView->count();
    
    // 如果每行的理论高度大于最大值
    if(cell_height > vHeaderView->maximumSectionSize()){
        // 将表头的调整模式设置为交互式
        vHeaderView->setSectionResizeMode(QHeaderView::Interactive);

        // 将所有行的高度设置为最大值
        for(int row = 0; row < vHeaderView->count(); row++)
            vHeaderView->resizeSection(row, vHeaderView->maximumSectionSize());
    }
    else{
        // 如果理论高度小于等于最大值,将表头的调整模式设置为拉伸(Stretch)
        vHeaderView->setSectionResizeMode(QHeaderView::Stretch);
    }
}

然后在表格的显示事件(showEvent)以及大小改变事件(resizeEvent)中调用这两个自适应调整函数,即可实现表格单元格自适应拉伸效果。完整代码如下:

  • CustomTableView.h:
#pragma once
#include <QTableView>

class CustomTableView : public QTableView
{
    Q_OBJECT
public:
    CustomTableView(QWidget *parent=nullptr);
    void autoAdjustTableItemWidth();
    void autoAdjustTableItemHeight();

protected:
    void resizeEvent(QResizeEvent *event);
    void showEvent(QShowEvent *event);
};
  • CustomTableView.cpp:
#include "CustomTableView.h"
#include <QHeaderView>

CustomTableView::CustomTableView(QWidget *parent) : QTableView (parent)
{
    this->horizontalHeader()->setMinimumWidth(60);//设置水平单元格最小宽度
    this->verticalHeader()->setMinimumSectionSize(18);//设置垂直单元格最小高度
    this->verticalHeader()->setMaximumSectionSize(30);//设置垂直单元格最大高度
}

void CustomTableView::resizeEvent(QResizeEvent *event)
{
    QTableView::resizeEvent(event);
    autoAdjustTableItemWidth();
    autoAdjustTableItemHeight();
}

void CustomTableView::showEvent(QShowEvent *event)
{
    QTableView::showEvent(event);
    autoAdjustTableItemWidth();
    autoAdjustTableItemHeight();
}

void CustomTableView::autoAdjustTableItemWidth()
{
    // 设置水平滚动条策略为按需显示
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    // 获取水平表头
    QHeaderView *hHeaderView = this->horizontalHeader();
    // 设置列的调整模式为根据内容自动调整大小
    hHeaderView->setSectionResizeMode(QHeaderView::ResizeToContents);
    // 计算所有列的总宽度
    int section_total_width = 0;
    for (int col = 0; col < hHeaderView->count(); ++col)
        section_total_width += hHeaderView->sectionSize(col);

    // 恢复列的调整模式为交互式
    hHeaderView->setSectionResizeMode(QHeaderView::Interactive);
    // 获取水平表头的宽度
    int hHeaderWidth = hHeaderView->width();
    // 如果水平表头的宽度大于所有列的总宽度
    if (hHeaderWidth > section_total_width){
        // 计算缩放比例
        float scale = hHeaderWidth / float(section_total_width);
        int width_sum = 0;
        // 根据缩放比例调整每列的宽度
        for (int col = 0; col < hHeaderView->count() - 1; ++col) {
            int cell_width = hHeaderView->sectionSize(col) * scale;
            hHeaderView->resizeSection(col, cell_width);
            width_sum += cell_width;
        }
        // 调整最后一列的宽度,确保总宽度与表头宽度一致
        hHeaderView->resizeSection(hHeaderView->count() - 1, hHeaderWidth - width_sum);
        // 关闭水平滚动条,因为此时表头宽度已经足够显示所有列
        this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    }
}

void CustomTableView::autoAdjustTableItemHeight()
{
    // 获取垂直表头
    QHeaderView *vHeaderView = this->verticalHeader();
    // 获取垂直表头的高度
    int vHeaderHeight = vHeaderView->height();
    // 计算每行的理论高度,以保证均匀分配表头高度
    int cell_height = vHeaderHeight / vHeaderView->count();

    // 如果每行的理论高度大于最大值
    if(cell_height > vHeaderView->maximumSectionSize()){
        // 将表头的调整模式设置为交互式
        vHeaderView->setSectionResizeMode(QHeaderView::Interactive);

        // 将所有行的高度设置为最大值
        for(int row = 0; row < vHeaderView->count(); row++)
            vHeaderView->resizeSection(row, vHeaderView->maximumSectionSize());
    }
    else{
        // 如果理论高度小于等于最大值,将表头的调整模式设置为拉伸(Stretch)
        vHeaderView->setSectionResizeMode(QHeaderView::Stretch);
    }
}
  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值