QTableWidget 冻结某列,不让这列随着水平滚动条滚动
时间: 2023-07-20 15:43:11 浏览: 169
可以使用 QHeaderView 的 setSectionResizeMode 方法来实现冻结某列,不让这列随着水平滚动条滚动。
具体实现方法如下:
```python
# 获取要冻结的列号
column_index = 1
# 获取 QTableWidget 的水平头部视图
header_view = self.tableWidget.horizontalHeader()
# 设置列宽为固定大小
header_view.setSectionResizeMode(column_index, QtWidgets.QHeaderView.Fixed)
# 获取要冻结的列的宽度
column_width = self.tableWidget.columnWidth(column_index)
# 设置冻结列的宽度
header_view.resizeSection(column_index, column_width)
# 禁止用户调整冻结列的宽度
header_view.setSectionResizeMode(column_index, QtWidgets.QHeaderView.Fixed)
```
其中,self.tableWidget 表示 QTableWidget 实例,可以根据实际情况进行替换。
相关问题
c++ QTableWidget 冻结一列,不让这列随着水平滚动条滚动而滚动
在 C++ 中,可以使用 `QHeaderView` 的 `setSectionResizeMode` 方法来实现冻结一列,不让这列随着水平滚动条滚动。
具体实现方法如下:
```cpp
// 获取要冻结的列号
int column_index = 1;
// 获取 QTableWidget 的水平头部视图
QHeaderView* header_view = ui->tableWidget->horizontalHeader();
// 设置列宽为固定大小
header_view->setSectionResizeMode(column_index, QHeaderView::Fixed);
// 获取要冻结的列的宽度
int column_width = ui->tableWidget->columnWidth(column_index);
// 设置冻结列的宽度
header_view->resizeSection(column_index, column_width);
// 禁止用户调整冻结列的宽度
header_view->setSectionResizeMode(column_index, QHeaderView::Fixed);
// 获取 QTableWidget 的垂直滚动条
QScrollBar* vertical_scrollbar = ui->tableWidget->verticalScrollBar();
// 获取 QTableWidget 的垂直头部视图
QHeaderView* vertical_header_view = ui->tableWidget->verticalHeader();
// 设置垂直头部视图的宽度
vertical_header_view->setFixedWidth(vertical_scrollbar->width());
```
其中,`ui->tableWidget` 表示 `QTableWidget` 实例,可以根据实际情况进行替换。
c++ qtablewidget冻结某一列不会随着滚动条滚动
如果您想要冻结某一列,使其不会随着滚动条滚动,您可以将该列添加到QTableView的左侧(即在表格的最左侧),并设置QTableView的水平滚动条的初始值为该列的宽度。这样,当用户滚动表格时,左侧的列将保持固定,而其他列将随着滚动而移动。
以下是一个示例代码,其中第一列被冻结:
```cpp
// 创建表格视图
QTableView *tableView = new QTableView(this);
// 创建模型
QStandardItemModel *model = new QStandardItemModel(this);
model->setColumnCount(4);
model->setRowCount(10);
// 添加数据
for (int row = 0; row < model->rowCount(); ++row) {
for (int col = 0; col < model->columnCount(); ++col) {
QStandardItem *item = new QStandardItem(QString("%1,%2").arg(row).arg(col));
model->setItem(row, col, item);
}
}
// 设置列宽
tableView->setColumnWidth(0, 100);
tableView->setColumnWidth(1, 100);
tableView->setColumnWidth(2, 100);
tableView->setColumnWidth(3, 100);
// 将第一列添加到左侧
tableView->setModel(model);
tableView->horizontalHeader()->moveSection(0, tableView->horizontalHeader()->count()-1);
// 设置水平滚动条初始值为第一列的宽度
tableView->horizontalScrollBar()->setValue(tableView->columnWidth(0));
// 显示表格视图
tableView->show();
```
请注意,此方法只适用于QTableView,而不适用于QTableWidget。如果您使用的是QTableWidget,您可以考虑将其放置在QTableView中,并按照上述方法进行设置。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)