PDA

View Full Version : File Dialog / Network Protocols in QT4



themolecule
8th September 2007, 18:24
In QT3 there was a very nice way to create new protocols and register them. This was very useful, for example, to change the way that a FileDialog displays its dir listings.

I had created a new protocol, called "sequence" that allowed me to pass different current directories to a file dialog:

file://path/to/file.0001.jpg
vs.
sequence://path/to/file.#.jpg

I have directories with thousands of video frames in them, all numbered:

file.0001.jpg
file.0002.jpg
...
file.9998.jpg
file.9999.jpg
image.0001.png
...
image.1234.png

there may be multiple frame sequences in a dir, so I had an algorithm to collapse the sequences into a shorter notation:

file.[1-9999]#.jpg
image.[1-1234]#.jpg

this is much easier to read and cleaner when doing video work.

The beauty of it was that all I had to do was pass a different protocol:// to the dialog to switch between the notations!

Now, in QT4, the network protocol has been deprecated to Q3NetworkProtocol, and I don't want to use it if it's going to be removed in the future. This is a real bummer, because now I may be forced to build a new QFileDialog object just to list files in this way.

An additional thing that the protocol would have accomodated would have been a machine name at the beginning of the path... I have a file server (which I wrote) running on certain machines, and clients can request dir listings from each server (via a main dispatching caching server).

Bottom line is, I wanted to be able to pass a starting protocol/path to a file dialog and be able to navigate around the directory tree, but the source of this information is not local, is not http or ftp, and NetworkProtocol does not seem to have a QT4 equivalent.

Is there a class with a similar "operationListChildren" that I can subclass to make this work?

wysota
8th September 2007, 20:30
Now, in QT4, the network protocol has been deprecated to Q3NetworkProtocol, and I don't want to use it if it's going to be removed in the future. This is a real bummer, because now I may be forced to build a new QFileDialog object just to list files in this way.

An additional thing that the protocol would have accomodated would have been a machine name at the beginning of the path... I have a file server (which I wrote) running on certain machines, and clients can request dir listings from each server (via a main dispatching caching server).

Bottom line is, I wanted to be able to pass a starting protocol/path to a file dialog and be able to navigate around the directory tree, but the source of this information is not local, is not http or ftp, and NetworkProtocol does not seem to have a QT4 equivalent.

Is there a class with a similar "operationListChildren" that I can subclass to make this work?

Take a look at QAbstractFileEngine.

themolecule
9th September 2007, 04:29
Thanks, this is exactly what I was looking for...!

After reading and re-reading the documentation and examples, I am confused about a couple points:

It seems that I can just subclass QFSFileEngine... let's call it "RemoteFileEngine". So I can infer that I will need at least three classes:

RemoteFileEngine
RemoteFileEngineHandler
RemoteFileEngineIterator

where the iterator is the entry point to the virtual file system listings. (ie, iterator returns the beginning of a dirlisting for beginEntryList, which QFileDialog uses to populate itself). The Handler creates engine items and the Engine does all the other work.

But after attempting to subclass I find that QFSFileEngine has methods scattered all over that reference the local file system, and it's kind of difficult to separate, or rather, override all the functions that talk to the local file system (the whole listing should be virtual, served by the server).

I am forced to conclude that I have to subclass QAbstractFileEngine instead, but there appears to be about 20 virtual functions, and certain private variables that I'll need to create. The source is kind of obfuscated for QFSFileEngine, so it's difficult to use this as a starting place. Is there an example somewhere, or a list of the minimum methods needed to successfully subclass?

wysota
9th September 2007, 05:31
If your files are real files, you can subclass QFSFileEngine and reimplement those methods defined in QAbstractFileEngine that you need changed. In doubt see the sources of the mentioned classes - maybe you only need to alter one or two methods.

themolecule
9th September 2007, 17:29
Unfortunately, it seems like QFSFileEngine is especially split between OS implementations, and is very connected to the local file system. I don't think subclassing is possible, or rather, it's pointless because so many methods would need to be overridden.

The files I want to list are not local to the file system, and therefore the FileDialog should never need to access the local file system (except to load icons).

I have started to write a QAbstractFileEngine subclass, but things don't seem to work right.

The example in the documentation on QAbstractFileEngineIterator has bugs in it. It puts the iterator on the first item, which means that the first item is never retrieved. I changed the index(0) to index(-1) which helps (after fixing currentFileName to check for -1). Also, filterNames and filters seems reversed in the documentation (which is easy to fix, but still seems like a typo).

But when the dialog is created it seems to search for history items, which seem to default to the currentDirectory (of the application). Then I set history to an empty list, and the sidebar urls to an empty list, but entries never show up in the dialog.

After searching, I find in the code that QDirIterator is a friend to QAbstractFileEngine, which is frustrating because this disallows me to set the path() of the iterator, and instead it inherits the dir from when the FileDialog was created. Perhaps my iterator has to subclass QDirIterator? But what do I do with QDirIteratorPrivate?

Is it really bad to use Q3NetworkProtocol, I mean, are they really gonna get rid of it?

themolecule
9th September 2007, 19:11
this is interesting...

http://lists.trolltech.com/qt-interest/2007-03/msg00693.html

I think it's a wash... I should build my own FileDialog.

wysota
10th September 2007, 08:40
Unfortunately, it seems like QFSFileEngine is especially split between OS implementations, and is very connected to the local file system. I don't think subclassing is possible, or rather, it's pointless because so many methods would need to be overridden.

In my opinion there shouldn't be much work with it. In general you'd need to translate file paths and call default implementations.