Results 1 to 2 of 2

Thread: How to change file info shown in qt examples

  1. #1
    Join Date
    Apr 2017
    Posts
    55
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to change file info shown in qt examples

    I am using the file finder dialog example at https://doc.qt.io/archives/qt-4.8/qt...s-example.html
    In the code they grab and display the size of the file. For me having the date is more helpful than having the size.

    Here is the code that does it:

    Qt Code:
    1. void Window::showFiles(const QStringList &files)
    2. {
    3. for (int i = 0; i < files.size(); ++i) {
    4. QFile file(currentDir.absoluteFilePath(files[i]));
    5. qint64 size = QFileInfo(file).size();
    6.  
    7. QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
    8. fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
    9. QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
    10. .arg(int((size + 1023) / 1024)));
    11. sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    12. sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable);
    13.  
    14. int row = filesTable->rowCount();
    15. filesTable->insertRow(row);
    16. filesTable->setItem(row, 0, fileNameItem);
    17. filesTable->setItem(row, 1, sizeItem);
    18. }
    19. filesFoundLabel->setText(tr("%1 file(s) found").arg(files.size()) +
    20. #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
    21. (" (Select file to open it)"));
    22. #else
    23. (" (Double click on a file to open it)"));
    24. #endif
    25. filesFoundLabel->setWordWrap(true);
    26. }
    To copy to clipboard, switch view to plain text mode 


    I can't find info on what is in the list when the line
    filesTable->setItem(row, 1, sizeItem);
    is executed.

    Thanks for any help

    emp1953


    Added after 12 minutes:


    I found it in the QFileInfo class the function is .lastModified() It returns a QDateTime.

    but now I can't get that time date to print in the file dialog box, first I'd like to format it so its just mm/dd/yyyy, then I want it in the dialog box

    I added

    QDateTime fdate = (files).lastModified();
    qDebug() << fdate.toString();

    I changed:
    QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
    .arg(int((size + 1023) / 1024)));

    to:
    QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("")
    .arg(QString(files)));

    Compiles and prints all the dates & times correctly for all of the files found.

    I can't get it to print in the dialog box.
    Last edited by emp1953; 19th March 2020 at 20:58.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to change file info shown in qt examples

    QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("").arg(QString(files)));
    Should be:

    Qt Code:
    1. QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1").arg(QString(files)));
    To copy to clipboard, switch view to plain text mode 

    The "%1" is required and tells QString to put the value returned by the arg() call in place of the %1. If you wanted to place multiple values in a formatted string, you would write something like:

    Qt Code:
    1. QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("First item is %1, second item is %2, etc.").arg(QString(files)).arg( QString( "Item 2")) );
    To copy to clipboard, switch view to plain text mode 

    And if what you want to put in the second column is the date, you would obviously not want to pass a QStringList (which is what "files" is), but instead a QString formatted to contain the date for files[i]. You have already done this for your QDebug statement.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 18th March 2016, 12:51
  2. Replies: 0
    Last Post: 7th May 2011, 20:43
  3. Replies: 3
    Last Post: 4th June 2010, 09:10
  4. File version info
    By johncharlesb in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2007, 03:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.