I have a app which displays the Date & Time of a file and the filepath in a QTableView. I use the following code to get the Date & Time of a file and display the path and Date & Time.
The directory with the files is not empty.
void printOutput(std::set<std::string> files, QStandardItemModel* model)
{
model->setRowCount(files.size());
int row = 0;
for (const auto& str : files) {
QFileInfo info(QString::fromStdString(str));
QStandardItem* fileItem = new QStandardItem(QString::fromStdString(str));
//QString dateformat = "dd/MM/yyyy h:mm";
//QDateTime dt = QDateTime::fromString(info.birthTime().toString(), dateformat);
//QString dtstr = dt.toString();
//QStandardItem* dt_item = new QStandardItem(dtstr);
QDateTime datetime = info.lastModified();
QString formattedDateTime = datetime.toString();
QStandardItem* dateItem = new QStandardItem(formattedDateTime);
//to check the ouput --> is empty
QMessageBox msg;
msg.setText(formattedDateTime);
msg.exec();
model->setItem(row, 0, dateItem);
model->setItem(row, 1, fileItem);
row++;
}
}
The output for the filepath works but the Date & Time doesn’t.
New contributor
user26432708 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
How about adding checks. I would change something similar like
void printOutput(const std::set<std::string>& files, QStandardItemModel* model)
{
model->setRowCount(files.size());
int row = 0;
for (const auto& str : files) {
const auto filename = QString::fromStdString(str);
QFileInfo info(filename);
if (!info.exists()) {
QMessageBox::warning(nullptr, "printOutput", QString("%1 don't exist").arg(filename));
continue;
}
const auto datetime = info.lastModified();
auto* dateItem = new QStandardItem(datetime.toString());
auto* fileItem = new QStandardItem(filename);
model->setItem(row, 0, dateItem);
model->setItem(row, 1, fileItem);
++row;
}
}