Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly check network paths with Qt 5.2 (C++)

I have what I hope is a rather simple question.

In a Qt application, I am frequently accessing folders on a Windows PC in my workgroup network. The network path is of the form "\\NETWORKDEVICE\folder" or along those lines. To check if the path exists, I have been using a QDir, like so:

pathString = "somepathhere";
if(QDir(pathString).exists())
    workHere(pathString);

In other cases, I use QUrl to open a specific file or folder with Windows Explorer, like so:

pathString = "somepathhere";
QUrl url = pathString;
QDesktopServices::openUrl(url);

And I may mix the two, like so, to ensure the path exists beforehand.

pathString = "somepathhere";
if(QDir(pathString).exists())
{
  QUrl url = pathString;
  QDesktopServices::openUrl(url);
{

The problem is that whenever I use QDir::exists(), if the path is currently offline or the connection is poor to that PC from where my program is running, Windows will hang the application there for as long as 40 seconds before returning an answer to QDir::exists(). (You see this same reaction if you try to open a down network path in explorer; the address bar just fills slowly with green while it searches, until it gives up.)

This is an awfully long time for my program to be waiting for that response, and so I was wondering if anyone has any better ideas for how to handle this situation. I may be overlooking some setting or alternative function, but as of right now I am out of ideas. I hope to use this code to ensure that my alternative files and folders are accessed if the specified ones are offline.

Thanks so much for any help, and I apologize if I have missed any rules or etiquette here on the site.

like image 804
Kettamon Avatar asked Dec 14 '25 09:12

Kettamon


1 Answers

I believe, that if OS call(And after all QT wrappers, there IS an OS call) hangs for some time, then you can't get any useful result immediately. If so, the best thing you can do is put this work into another thread and signal to the main thread when it is finished. In the main thread you can print some status somewhere or draw beautiful picture with "Waiting..." sign on it.

like image 168
Геннадий Казачёк Avatar answered Dec 16 '25 03:12

Геннадий Казачёк



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!