QFileSystemModel -- Detecting empty folder (with 'AllDirs' filter)?
I'm using Qt 4.6 within Ruby (via QtRuby) and am trying to make a generic Directory Selection Dialog which displays a small "loading" glyph whilst the file system is being queried and the Directory Tree (QTreeView) is being updated.
UPDATE: I must say that the animation doesn't work as expected, is there another way of detecting these events (loading, loaded) ? See "On another note" below.
I have managed to connect the "new directories loaded" event via the rowsInserted
Signal of the used QFileSystemModel, works quite fine. I'm also able to catch the "loading new directories" event via the rowsAboutToBeInserted
signal. Yet the animation I'm trying to play (a simple animated GIF to indicate progress, loaded within a QMovie) is getting played even if the directory which has been 'expanded' is empty. Here is the code I am using :
# FileSystemModel extension which shows a 'busy' animation
# in a given Qt::Label
class FileSystemModelEx < Qt::FileSystemModel
# Slot declarations
slots "handle_ready(QModelIndex, int, int)"
slots "handle_busy(QModelIndex, int, int)"
# Parametrised constructor, initializes fields
def initialize(p_parent, p_label, p_busy_icon, p_ready_icon)
# Call superclass constructor
super(p_parent)
# Set instance vars
@label = p_label
@busy_icon = p_busy_icon
@ready_icon = p_ready_icon
# Connect 'finished loaded' event
Qt::Object.connect(self,
SIGNAL('rowsAboutToBeInserted(QModelIndex, int, int)'),
self,
SLOT('handle_busy(QModelIndex, int, int)'))
# Connect 'loading' event
Qt::Object.connect(self,
SIGNAL('rowsInserted(QModelIndex, int, int)'),
self,
SLOT('handle_ready(QModelIndex, int, int)'))
end
# Loading finished event, changes icon state to ready
def handle_ready(p_index, p_start, p_end)
set_icon(false)
puts " done - loaded #{rowCount(p_index)} folders"
end
# Loading started event, changes icon state to busy
def handle_busy(p_index, p_start, p_end)
set_icon(true)
path = fileInfo(p_index).canonicalFilePath
puts "Loading . . . path = '#{path}'"
end
# Overriden start loading event
def fetchMore(p_index)
handle_busy(p_index, nil, nil)
开发者_开发问答 super(p_index)
end
# Utility method, switches icons, depending on a given state
def set_icon(p_busy)
movie = (p_busy ? @busy_icon : @ready_icon)
@label.setMovie(movie)
movie.start
end
end # class FileSystemModelEx
My question would be: How can I keep the animation from playing, if the loaded folder is empty? One cannot filter the empty directories beforehand, isn't that so?
On another note, is there another way of implementing such 'loading' / 'loaded' event handlers, other than that described above? I've looked around the signals, virtuals (fetchMore
and canFetchMore
, to no avail), scanned the source yet I cannot reach the call which sends the thread on its quest of retrieving more files. Overriding event
or timerEvent
doesn't help.
For completion's sake, here is the QFileSystemModel I'm using as well:
# Creates a FileSystemModel which display folders only
def create_model
@model = FileSystemModelEx.new(self,
@form.iconPlaceholderDir,
@loading_icon, @folder_icon)
@model.setReadOnly(true)
@model.setFilter(Qt::Dir::NoDotAndDotDot | Qt::Dir::AllDirs)
@model.setRootPath(Qt::Dir.rootPath)
@form.shellTreeView.setModel(@model)
end
Any help would be appreciated, thanks in advance! I can provide further details if the need arises, no problem.
You should try hooking up the model's directoryLoaded(const QString&)
slot. It signals when the directory has been fully processed.
Sample app with qt4-ruby (2.1.0) build againts Ruby 1.8.7 and Qt 4.7.3
#!/usr/bin/ruby -w
require 'Qt4'
class MyObject < Qt::Object
slots "mySlot(QString)"
def mySlot(path)
print "Done loading ", path, "\n"
end
end
a = Qt::Application.new(ARGV)
m = Qt::FileSystemModel.new
v = Qt::TreeView.new
m.setRootPath("/")
v.setModel(m)
o = MyObject::new
Qt::Object.connect(m, SIGNAL('directoryLoaded(QString)'), o, SLOT('mySlot(QString)'))
v.show
a.exec
(Be kind, this is my very first ruby "program"...)
精彩评论