February 19, 2012

saeed144 saeed144
Lab Rat
10 posts

QFile accessing samba shared file (Qt on linux )

 

Dear all,

I’m developing Qt 4.7.4 in Linux, trying to access a file in other linux samba server.
The code on windows works like a charm, but still not in Linux,
please let me know who can I work it out,

  1.     QString cAudio = "smb://192.168.0.2/audioshare/audiofile.wav";
  2.     QFile   cAudioFile(cAudio);
  3.     if (cAudioFile.exists())
  4.         QMessageBox::about(NULL, "cAudioFile", "file exists"+cAudio);
  5.     else
  6.         QMessageBox::about(NULL, "cAudioFile", "file missing"+cAudio);

The file is easily opened in VLC player, and the samba folder/file does not need any user/password.

  1. QString cAudio = "//192.168.0.2/audioshare/audiofile.wav";

did not work either.
But it works for local files like

  1. QString cAudio = "/home/user1/audio/audiofile.wav";

BTW, I have set current directory to

  1. QDir::setCurrent(QCoreApplication::applicationDirPath());

in the main program.

Thanks in advance,
Saeed144

5 replies

February 19, 2012

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

QFile does not support non-local files.

You can mount the resource locally and then access the data. There are also libraries like KIO Slaves from KDE and gnomeVFS from gnome to transparently access data not in the local file system.

February 19, 2012

saeed144 saeed144
Lab Rat
10 posts

Dear Tobias,
As I said, running the code in Windows, works OK. I have to change the QFile path so that Windows understands it.

  1.  QFile cAudioFile("\\\\192.168.0.2\\audioshare\\text.txt");
  2.  if (cAudioFile.exists())
  3.  QMessageBox::about(NULL, "1", "exists");
  4.  else
  5.  QMessageBox::about(NULL, "1", "not exists");

It’s tricky, because, in windows I can read the file, shared on Linux, but I can’t read it in Linux!

BTW, gnomeVFS is deprecated based on wikipedia, and I should use GIO,

Thanks for the reply,
Saeed144

February 19, 2012

Volker Volker
Ant Farmer
5428 posts

UNC file paths are supported by Windows natively. So it’s no surprise that those work.

The libc/stdlib functions on Linux do not support remote filesystems, as Tobias mentioned. So it does not work unless you have mounted that share to the local filesystem already.

Comparing Windows and Linux here is comparing apples and oranges. What works on one OS, does not necessarily work on the other.

February 20, 2012

saeed144 saeed144
Lab Rat
10 posts

Being a software developer, I have to develop for both operating systems, though I personally prefer linux.

It was interesting for me to see how OS can affect the behavior of a Cross-platform framework like Qt.
Didn’t mean to compare the operating systems in this matter,

Anyway, thanks for the help, here is my code,

  1. #ifdef Q_OS_LINUX
  2.     cAudioPath = "/home/user/audioshare/";
  3. #endif
  4. #ifdef Q_OS_WIN32
  5.     cAudioPath = "\\\\192.168.0.2\\audioshare\\";
  6. #endif

February 20, 2012

fluca1978 fluca1978
Lab Rat
529 posts

saeed144 wrote:
Being a software developer, I have to develop for both operating systems, though I personally prefer linux.

It was interesting for me to see how OS can affect the behavior of a Cross-platform framework like Qt.

Well, the framework cannot give you a totally OS-independent environment, at some point you have to deal with your deploy environment. For instance, Windows allows unit letters (C:) while *nix does not.
My opinion is that, in order to be as much portable as possible, your application should not use two different paths but a single one which correspond to either a mount point or a link and is configured at the time the application is deployed.

 
  ‹‹ Illegal call of non-static member function!      [solved]expected class-name before ’{’ token ››

You must log in to post a reply. Not a member yet? Register here!