PDA

View Full Version : How can I get the date out of QFileInfo?



emp1953
20th March 2020, 18:31
I am using the find file dialog code at https://doc.qt.io/archives/qt-4.8/qt...s-example.html

I added the lines:

QDateTime fdate = QFileInfo(file).lastModified();
QString sdate = fdate.toString("mm/dd/yyyy");
qDebug() << sdate;

QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("")
.arg(fdate.toString()));

QFileInfo(file).lastModified() gives the the following info:

Wed March 18 16:15:33 2020
Seems like kind of a jumbled way of providing a date time.

So I want just 03/18/2020 I feed it into QString sdate = fdate.toString("mm/dd/yyyy");
But this gives me "33/18/2020"

First How do I get the file info lastModified to appear as 03/18/2020?

Second I changed the declaration for sizeItem to:
QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("")
.arg(fdate.toString()));

I want the date to be printed out instead of the file size. File size is useless to my needs.

How can I accomplish that part?

Thanks to all

emp1953

ChristianEhrlicher
20th March 2020, 19:04
Because 'mm' are the minutes according the documentation: https://doc.qt.io/qt-5/qdatetime.html#toString

d_stranz
20th March 2020, 20:07
Second I changed the declaration for sizeItem to:
QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("").arg(fdate.toString()));


And didn't I tell you in reply to your last post (in the "General Discussion" section) how to properly format a QString().arg() expression?

We aren't going to write your code for you, and you have to read and understand the answers so you can write it yourself.