PDA

View Full Version : QFileInfoListIterator problem



lsropia
12th March 2011, 09:16
Below given is my part of code that used to work some (long) time back. When I need this code, I find it works no more. The compilation is clean, bur when run gives the error as:

[lx@localhost listDir]$ ./listDir /home/lux/Downloads/xfrd/raw /home/lx/Downloads/xfrd/rawConv/
04. qStrDir: /home/lx/Downloads/xfrd/raw, qFileInfoDirInfo->fileName: raw01
05. Before QFileInfoListIterator filesIterator( *filesList ): 0
Segmentation fault (core dumped)

The problem seems to be inconsistency by way of new version of the Qt. Please help to make this code work.


QDir qDirFindLargest, qDirLargestFile, qDirFilesChange;
QFile qf;
QString qStrDir, qStrDir2, qStrTemp; // a.argv()[i] == argv[i]
qStrTemp = a.argv()[0];
qStrDir = a.argv()[1];
qStrDir2 = a.argv()[2];
//cout << "Total agrc: " << argc << ", Directories 0: " << qStrTemp << ", 1: " << qStrDir << ", 2: " << qStrDir2 << endl;

qDirFindLargest.setPath ( qStrDir );
qDirFindLargest.setFilter( QDir::Dirs ); // sets only the directory: shows the directories only
qDirFindLargest.setSorting( QDir::Name | QDir::Reversed );

const QFileInfoList *qFileInfoDirList = qDirFindLargest.entryInfoList();
QFileInfoListIterator qFileInfoListIteratorDir( *qFileInfoDirList );
QFileInfo *qFileInfoDirInfo;

QString qFileName;
int fileNumber = 0;

while ( ( qFileInfoDirInfo = qFileInfoListIteratorDir.current() ) != 0 ) {
if (qFileInfoDirInfo->fileName() == "." ) { // first item in the directory: . (let pass)
++qFileInfoListIteratorDir;
}
else if (qFileInfoDirInfo->fileName() == ".." ) { // first item in the directory: .. (let pass)
++qFileInfoListIteratorDir;
}
else {
qDirLargestFile.setPath ( qStrDir + qFileInfoDirInfo->fileName() );
qDirLargestFile.setFilter( QDir::Files ); // qFiles to only show the files
qDirLargestFile.setSorting( QDir::Name | QDir::Reversed ); // sorting the files in the directory in reverse order: to find the last file alphabetically
cout << "04. qStrDir: " << qStrDir << ", qFileInfoDirInfo->fileName: " << qFileInfoDirInfo->fileName() << endl;
const QFileInfoList *filesList = qDirLargestFile.entryInfoList();
cout << "05. Before QFileInfoListIterator filesIterator( *filesList );" << endl;

QFileInfoListIterator filesIterator( *filesList );
cout << "06. After QFileInfoListIterator filesIterator( *filesList );" << endl;
QFileInfo *files;
cout << "07. After QFileInfo *files;" << endl;
files = filesIterator.current();
cout << "07A. After files = filesIterator.current(), files = filesIterator.current(): " << filesIterator.current() << endl;
cout << "08. After files = filesIterator.current(), files->fileName: " << files->fileName() << endl;

... }

stampede
12th March 2011, 10:25
QFileInfoListIterator filesIterator( *filesList );
You are dereferencing a pointer without checking if it's not NULL, this could be dangerous.

I'm also curious, what is your Qt version and compiler, because as far as I'm concerned, this code should not compile:

const QFileInfoList *filesList = qDirLargestFile.entryInfoList();
QDir::entryInfoList() (http://doc.qt.nokia.com/latest/qdir.html#entryInfoList) returns QFileInfoList, not QFileInfoList *

lsropia
12th March 2011, 20:32
Thanks for the reply.

As I mentioned this code worked long back & now it is complaining. What you mention that it should not compile may also be right. But if I change as per your observation it does not compile. "QDir::entryInfoList() returns QFileInfoList, not QFileInfoList * " Hence, it seems what is coded is compile proof.

Any more ideas? Actually I am out of touch for quite some time now, and I could sure use some help.

Thanks once more.

squidge
12th March 2011, 20:51
Please answer the question "what is your Qt version and compiler?"

lsropia
13th March 2011, 07:18
Qt Designer Version: 4.7.1

qmake -v
Qmake version: 1.07a (Qt 3.3.8b)
Qmake is free software from Trolltech ASA.

make -v
GNU Make 3.82
Built for i386-redhat-linux-gnu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

ChrisW67
13th March 2011, 08:27
You claim to be using Qt 4.7.1 but the qmake you tell us about is from Qt 3 and QFileInfoListIterator is a Qt3 item. Has the code been ported to Qt4 using the Qt 3 Support module or should you be using the Qt3 tool set? Have you tried to mix code previously built with Qt 3 with Qt 4 components?