Get the file information in Qt
Article Metadata
Tested with
Devices(s): Creator IDE V 4.5 and Emulator
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Article
Keywords: QFileInfo
Created: mind_freak
(10 Jun 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This article shows how to use QFileInfo to get name and modification information about a file in the file system. QFileInfo objects are used to hold information about files and directories.
The method used in the case are given below :
- fileName() : Returns the name of the file, excluding the path.
- lastModified() :Returns the date and time when the file was last modified.
- suffix() : Returns the suffix of the file.The suffix consists of all characters in the file after (but not including) the last '.'.
Preconditions
- Download and install the Qt SDK.
Property
- Returns the canonical path including the file name, i.e. an absolute path without symbolic links or redundant "." or ".." elements.
str=Info->canonicalFilePath ();
- Returns the complete base name of the file without the path.
QFileInfo fi("/tmp/archive.tar.gz");
QString base = fi.completeBaseName(); // base = "archive.tar"
Source File
Main.cpp
#include <QtGui/QApplication>
#include "fileinfo.h"
#include<QFileInfo>
#include<QWidget>
#include<QLabel>
#include<QString>
#include<QVBoxLayout>
#include<QDateTime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *win=new QWidget();
QDateTime time;
QFileInfo info("C://demos//launcher//install.txt");
QString str=info.fileName();
time=info.lastModified();
QString str1;
str1=time.toString();
QString ext = info.suffix();
QLabel *lbl=new QLabel(str);
QLabel *lbl1=new QLabel(str1);
QLabel *lbl2=new QLabel(ext);
QVBoxLayout *lay=new QVBoxLayout();
lay->addWidget(lbl);// Name of file "Intall.txt"
lay->addWidget(lbl1);//Last Modified Date
lay->addWidget(lbl2);//File Extension
win->setLayout(lay);
win->show();
return a.exec();
}
Screen shot
Creator IDE
Author - User:Mind freak


(no comments yet)