tableWidget如何使用
时间: 2023-07-20 21:03:56 浏览: 42
QTableWidget是一个用于显示表格数据的Qt类,可以通过以下步骤来使用它:
1. 创建一个QTableWidget对象
```
QTableWidget *tableWidget = new QTableWidget(this);
```
2. 设置表格的行数和列数
```
tableWidget->setRowCount(rowCount);
tableWidget->setColumnCount(columnCount);
```
3. 设置表格的表头
```
tableWidget->setHorizontalHeaderLabels(horizontalHeader);
tableWidget->setVerticalHeaderLabels(verticalHeader);
```
其中,horizontalHeader和verticalHeader分别为QStringList类型的表头内容。
4. 设置表格内容
```
tableWidget->setItem(row, column, item);
```
其中,row和column为表格中的行和列,item为QTableWidgetItem类型的单元格内容。
5. 显示表格
```
tableWidget->show();
```
完整的示例代码如下:
```
QTableWidget *tableWidget = new QTableWidget(this);
tableWidget->setRowCount(3);
tableWidget->setColumnCount(2);
QStringList horizontalHeader;
horizontalHeader << "Name" << "Age";
QStringList verticalHeader;
verticalHeader << "Row 1" << "Row 2" << "Row 3";
tableWidget->setHorizontalHeaderLabels(horizontalHeader);
tableWidget->setVerticalHeaderLabels(verticalHeader);
QTableWidgetItem *item1 = new QTableWidgetItem("John");
QTableWidgetItem *item2 = new QTableWidgetItem("25");
tableWidget->setItem(0, 0, item1);
tableWidget->setItem(0, 1, item2);
tableWidget->show();
```
相关推荐
![](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)