-1

I have a strange problem.

In QT my Application is working fine, but when i run the .exe file the Method QDir::entryList is not working... and I don't know why. The path and so is set.

QDir pdir(cs.protocolDir);
QString log;

log.append(QString("[LPR] Trying to fetch protcol files\n"));

QStringList filter("*.txt");
Message locMSG;

QStringList flist = pdir.entryList(QStringList(filter),
                                   QDir::Files | QDir::NoSymLinks);


foreach (QString str, flist) {
    QString filename = QString("%1%2").arg(cs.protocolDir).arg(str);
    log.append(QString("[LPR]Filename: %1\n").arg(filename));
    QFile file(filename);

    //and so on...

I hope someone can help me with this problem. THX

mreithuber
  • 3
  • 1
  • 5

3 Answers3

1

If the path a QDir is constructed with is not found QDir::entryList() will silently fail and return an empty list. Try inserting:

if (!pdir.exists())
{
    log.append(QString("[LPR] Path %1 does not exist - PWD is %2").arg(cs.protocolDir).arg(QDir::currentPath()));
}

...just after your first log statement, to see if the directory is found or not.

Is the path in cs.protocolDir a relative path, by any chance? If by "in Qt" you mean running it directly from Qt Creator then, Qt Creator will be setting a "working directory" which is by default the location of the binary. Perhaps it works in that case because the path is calculated relative to the binary.

When you run the executable, the working directory will be whatever directory you are in when you run the binary if you run it from the command line; or some directory chosen by your operating system otherwise.

If this is the problem then you'll need to use an absolute path instead.

Sez
  • 1,275
  • 11
  • 26
0

It's all about timing/syncing!

The referred DIR is not ready at the moment entryList is executed, I'll explain why works in debug mode:

While in debug mode, there's some (in)significant delay from collectors, watchers and many other structures to allow effective debugging, which costs performance.

So, if the directory is a dynamic one (network, flash drives, virtual, etc...) it may be necessary to wait sometime until the directory is completely built by the Operating System and ready to be accessed!

You can easily try by putting a sleep BEFORE initializing QDir with the path. Change the amount of delay in sleep function in order to understand the necessary time of your FS/SO;

meetnick
  • 1,196
  • 2
  • 12
  • 28
-1

It might because the executing paths are different while you run the application in debug mode from Qt creator or visual studio, and while you run the exe file by double clicking. Try logging the absolute path QDir::absolutePath(), and try using absolute path in cs.protocolDir.

Martian Puss
  • 710
  • 2
  • 7
  • 16