QT QTableView QTableWidget 复杂表头(多行表头) 、(冻结、固定特定的行)

22 篇文章 2 订阅

https://blog.csdn.net/xiezhongyuan07/article/details/82857631

Qt 中,QTableView 控件的多表头可以通过 QHeaderView 控件来实现。QHeaderView 控件可以设置为水平和垂直两种模式,并且可以在两个方向上同时存在。 下面是一个简单的示例,演示如何在 QTableView 控件中添加多表头: ```cpp // 创建 QTableView 控件 QTableView* tableView = new QTableView(this); // 设置数据模型 QStandardItemModel* model = new QStandardItemModel(this); tableView->setModel(model); // 添加数据 model->setColumnCount(3); model->setRowCount(5); for (int i = 0; i < model->rowCount(); i++) { for (int j = 0; j < model->columnCount(); j++) { QModelIndex index = model->index(i, j); model->setData(index, QString("Cell %1,%2").arg(i).arg(j)); } } // 设置水平表头 QHeaderView* horizontalHeader = tableView->horizontalHeader(); horizontalHeader->setSectionsClickable(true); horizontalHeader->setStyleSheet("QHeaderView::section {" "background-color:#f0f0f0;" "border:1px solid #d8d8d8;" "padding:2px; }"); horizontalHeader->resizeSection(0, 100); horizontalHeader->resizeSection(1, 150); horizontalHeader->resizeSection(2, 200); // 添加竖直表头 QHeaderView* verticalHeader = tableView->verticalHeader(); verticalHeader->setSectionsClickable(true); verticalHeader->setStyleSheet("QHeaderView::section {" "background-color:#f0f0f0;" "border:1px solid #d8d8d8;" "padding:2px; }"); verticalHeader->resizeSection(0, 50); verticalHeader->resizeSection(1, 75); verticalHeader->resizeSection(2, 100); verticalHeader->resizeSection(3, 125); verticalHeader->resizeSection(4, 150); // 将 QTableView 控件添加到布局中 layout->addWidget(tableView); ``` 在上面的示例中,首先创建了一个 QTableView 控件和一个数据模型。然后,使用 setColumnCount() 和 setRowCount() 函数设置了表格的数和列数,并添加了一些示例数据。 接下来,使用 horizontalHeader() 函数获取 QTableView 的水平表头,使用 verticalHeader() 函数获取竖直表头。然后,分别设置表头的样式和大小。 最后,将 QTableView 控件添加到布局中。 通过这种方式,就可以在 QTableView 控件中添加多表头了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值