Qt读取文件信息及时间输出到表格中
时间: 2023-11-27 16:51:51 浏览: 41
可以使用Qt提供的QFileInfo类来读取文件的信息,包括文件名、大小、修改时间等,然后将这些信息输出到表格中。以下是一个示例代码:
```cpp
QFileInfo fileInfo("path/to/file");
QString fileName = fileInfo.fileName();
qint64 fileSize = fileInfo.size();
QDateTime lastModified = fileInfo.lastModified();
// 将文件信息输出到表格中
QTableWidgetItem *nameItem = new QTableWidgetItem(fileName);
QTableWidgetItem *sizeItem = new QTableWidgetItem(QString::number(fileSize));
QTableWidgetItem *modifiedItem = new QTableWidgetItem(lastModified.toString());
int row = tableWidget->rowCount();
tableWidget->insertRow(row);
tableWidget->setItem(row, 0, nameItem);
tableWidget->setItem(row, 1, sizeItem);
tableWidget->setItem(row, 2, modifiedItem);
```
需要注意的是,上述代码中的`tableWidget`是一个QTableWidget对象,用于显示表格内容。你需要在程序中创建一个QTableWidget对象,并将其添加到界面中。然后,每次读取一个文件的信息时,就可以通过insertRow()和setItem()方法向表格中添加一行数据。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_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)