QT tablewidget 固定前两行的方法
时间: 2023-08-26 12:03:45 浏览: 69
要固定QT tablewidget中的前两行,可以使用setVerticalHeaderItem和setHorizontalHeaderItem方法来设置表头项,然后使用setCellWidget方法将需要固定的单元格设置为QWidget,即可实现固定。
具体实现步骤如下:
1. 使用setVerticalHeaderItem方法设置第一、二列表头项,例如:
```
QTableWidgetItem *item1 = new QTableWidgetItem();
QTableWidgetItem *item2 = new QTableWidgetItem();
item1->setText("固定行1");
item2->setText("固定行2");
ui->tableWidget->setVerticalHeaderItem(0, item1);
ui->tableWidget->setVerticalHeaderItem(1, item2);
```
2. 使用setHorizontalHeaderItem方法设置第一行表头项,例如:
```
QTableWidgetItem *item = new QTableWidgetItem();
item->setText("固定列");
ui->tableWidget->setHorizontalHeaderItem(0, item);
```
3. 使用setCellWidget方法将需要固定的单元格设置为QWidget,例如:
```
QLabel *label1 = new QLabel();
QLabel *label2 = new QLabel();
label1->setText("固定单元格1");
label2->setText("固定单元格2");
ui->tableWidget->setCellWidget(0, 0, label1);
ui->tableWidget->setCellWidget(1, 0, label2);
```
这样,就可以实现QT tablewidget中的前两行固定了。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)