Get file last saved time qt - qt

I'm trying to get the last modified time of a text file I am saving in a notepad app I built.
I can get the file directory using this code:
QString file = QFileDialog::getOpenFileName(this, "Open a file");
ui->filePathLabel->setText(file);
How can I extract a last saved time from this?
Thanks.

Use QFileInfo::lastModified():
const QFileInfo info(file);
const QDateTime lastModified = info.lastModified();
...

Related

excess "" in Qt CSV file creation

i'm coding in qt. i'm using this to create a csv file.
QtCSV::StringData strData;
QStringList strList0;
int a=10; int b=20;
QStringList strList;
strList << QString::number(a) << QString::number(b);
strData.addRow(strList);
QFile my_file(fileName);
if(my_file.open(QFile::WriteOnly ))
QtCSV::Writer::write(my_file, strData);
the file created and data is writed. the problem is this method of writing create this line of data:
""10","20""
the inner "" is excess. how can i write so that it remove? the other standard csv file does not have this and my file cannot open in some programs.

QFile: new file name append to tho last saved

I'm looking for the error I made on this code, but I can not find any solution since hours..
This function should simpli save a file to a directory:
void MyClass::saveSettingsToFile(QString file_name)
{
QString path;
path = dir.append(file_name);
QFile my_file(path);
if (!my_file.open(QFile::WriteOnly))
{
qDebug() << "Could not open file for writing";
}
QTextStream out(& my_file);
out << "some text \n"
my_file.flush();
my_file.close();
path = "";
file_name ="";
}
Where dir is a QString containing the directory, file_name is gathered from a lineEdit field.
When I first call the function with, for example file_name = "aaaa.txt", I find this aaaa.txt in the specified directory. All right.
When then I call again the function with file_name = "bbbb.txt", I find in the specified directory this file: aaaa.txtbbbb.txt, instead of I
bbbb.txt
It seems to me a very s****d error, but I cannot find what!
EDITED: there was this mistake QTextStream out(& path); instead of QTextStream out(& my_file);
You are modifying dir variable with QString::append. Variable dir is obviously a class member of MyClass. Try this instead:
void MyClass::saveSettingsToFile(QString file_name)
{
QString path(dir);
path.append(file_name);
QFile my_file(path);
//...
}
The QString::append function modify the parameter value itself as you can see in the documentation: http://doc.qt.io/qt-5/qstring.html#append
Example:
QString x = "free";
QString y = "dom";
x.append(y);
// x == "freedom"
So, what happens is that it keeps appending the content to the dir variable, not only assigning the result to path.

QTextStream atEnd() is returning true when starting to read from a file

I want to read and parse contents of the /proc/PID/status file on a linux machine, but the QTextStream.atEnd is always returning true when starting to read. The code:
QString procDirectory = "/proc/";
procDirectory.append(QString::number(PID));
procDirectory.append("/status");
QFile inputFile(procDirectory);
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
QString line;
while (!in.atEnd())
{
line = in.readLine();
File exists and if I read lines manually without the while expression, the files are read normally.
Did I miss something obvious?
(Debian 8 x64, QT 5.4.1 x64, gcc 4.9.2)
Nevermind found out I needed to read one line before the while clause, now it works.
The preferred way oft looping over these streams is with a do/while loop. This is for allowing the stream to detect Unicode correctly before any queries (like atEnd) are made.
QTextStream stream(stdin);
QString line;
do {
line = stream.readLine();
} while (!line.isNull());

check if a file extension is part of a given list

I am trying to make a simple widget which contains a lineedit which shows the file name and a button to open a filedialog.
and now I want to check if the file-extension is valid, in this case, a image file ending with jpg, png or bmp. I solved this with QFileInfo and QList, this code is in my btn_clicked slot:
QString filename = QFileDialog::getOpenFileName(this, tr("Select an image File", "", tr("Image Files (*.bmp *.jpg *.png);; All Files(*)"));
QList<QString> ext_list;
ext_list<<"bmp"<<"jpg"<<"png";
QFileInfo fi(filename);
QString ext = fi.suffix();
if (ext_list.contains(ext)){
// lineedit->setText(filename);
}
else {
QMessageBox msgBox;
msgBox.critical(0, "Error", "You must select a valid image file");
it works, but is there a more simple/elegant way to achieve the goal? Thx for your help.
You might be interested by the setNameFilters function : http://doc.qt.io/qt-5/qfiledialog.html#setNameFilters
Update
If you want to filter images without naming each extensions, you should use QMimeDatabase. This will allow you to define your filter for the QFileDialog and then get a list of extensions to check.
For example with jpeg and png:
QStringList mimeTypeFilters;
mimeTypeFilters << "image/jpeg" << "image/png";
QFileDialog fd;
fd.setFileMode(QFileDialog::ExistingFile);
fd.setMimeTypeFilters(mimeTypeFilters);
fd.exec();
QString filename = fd.selectedFiles().count() == 1 ? fd.selectedFiles().at(0) : "";
QMimeDatabase mimedb;
if(!mimeTypeFilters.contains(mimedb.mimeTypeForFile(filename).name()))
{
// something is wrong here !
}

How to split large style file (qss file) in QT to small file and load them all

What I am trying to do I need to split my large style file to small style files
because now it is hard to read the style file(qss file) and add new styling to it.
after that i need to load those small qss files to apply them all
I am loading my Big file by calling function on main I have created
void Utilities::loadEnglishStyle()
{
QFile file(":/EnglishClasses.qss");
file.open(QFile::ReadOnly);
QString StyleSheet = QLatin1String(file.readAll());
qApp->setStyleSheet(StyleSheet);
file.close();
}
I thought about split the big file, add the small files to the resources, open all of them by QFile and after that concatenate them all in one string
but every time when adding new qss file i still need to do the same process again
Is there any efficient way to do this ?!!
Your approach sounds correct. The process is not as complex as it sounds. The only thing you need is to add the "smaller" qss file in the resources, under a specific prefix (eg. stylesheets), and then automatically load and concatenate all these files. Sample code follows:
QDir stylesheetsDir(":/stylesheets");
QFileInfoList entries = stylesheetsDir.entryInfoList();
QString completeStylesheet = "";
foreach (QFileInfo fileInfo, entries)
{
QFile file(":/stylesheets/" + fileInfo.fileName(););
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QTextStream in(&file);
completeStylesheet += in.readAll()
}
you means some API shoud add as:
QWidget::addStyleSheet(StyleSheet);
QWidget::clearStyleSheet(StyleSheet);

Resources