Jun 02 2007
ruby Qt::TreeWidget example
I am involved in some projects were we are using Qt library for GUI development with ruby. In the following example I will show how to use the Qt::TreeWidget object.
The TreeWidget can be used not only to display information hierarchically but also to add multiple columns to the data model.
We are going to use a simple example in which we will populate our TreeWidget with filesystem information.
Below is a screen capture of what we want to accomplish:-

Here is the code:-
=begin
**
** treedemo.rb
** 02/JUN/2007
** Daniel Martin Gomez
**
** Desc:
** This script shows how to use the Qt::TreeWidget object. It populates a tree
** using filesystem directory information. Please note that this should only
** be used as an usage example of the widget. See Qt::DirModel for
** implementing directory tree behaviour.
**
** Version:
** v1.0 [02/Jun/2007]: first released
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
=end
#require Qt4 bindings for ruby
require 'Qt4'
$DEFAULT_PATH = '/home/etd'
#populate the tree
def populate(tree, path, parent=nil)
Dir["#{path}/*"].each do |file|
#fill item information
item = Qt::TreeWidgetItem.new()
item.setText(0, File.basename(file))
item.setText(1, File.mtime(file).strftime("%Y-%m-%d %I:%M:%S %p"))
item.setText(2, (File.size(file).to_s + ' bytes'))
item.setText(3, File.dirname(file) + '/')
item.setIcon(0, Qt::Icon.new(File.ftype(file)) )
#add item to the tree
if (parent == nil)
tree.addTopLevelItem(item)
else
parent.insertChild(parent.childCount, item)
end
if (File.ftype(file) == 'directory')
populate(tree, file, item)
end
end
end
if $0 == __FILE__
#parse command line
if (ARGV.size > 0)
path = ARGV[0]
else
path = $DEFAULT_PATH
end
app = Qt::Application.new(ARGV)
tree = Qt::TreeWidget.new
tree.windowTitle = "Simple Tree Example"
#set view headers
tree.setHeaderLabels(['File Name', 'Last Modified', 'Size', 'Path'])
populate(tree,path)
tree.show
app.exec
endInteresting bits and pieces of the code above include the import of the Qt4 library for ruby:-
require 'Qt4'
And the main Qt application execution:-
app = Qt::Application.new(ARGV) tree = Qt::TreeWidget.new tree.windowTitle = "Simple Tree Example" #set view headers tree.setHeaderLabels(['File Name', 'Last Modified', 'Size', 'Path']) populate(tree,path) tree.show app.exec
Every Qt Application needs to create a Qt::Application object. In our simple example only one widget is used (the TreeWidget). Because we are calling to the widget’s default constructor it will be drawn in it’s own window. If we want to draw the widget inside a parent window, we should pass that parent window in the widget’s constructor.
We then set the headers of the table and populate the tree (see below).
By calling to tree.show we let Qt know that from now on the widget must be drawn. The last line transfers the execution control to the Qt engine.
To populate the tree we are using a recursive function. For every row we want to add to the view we create a Qt::TreeWidgetItem, populate its properties (mainly the text to be displayed in each column) and add it to the TreeWidget object. The code that handles the hierarchy is shown here:-
#add item to the tree if (parent == nil) tree.addTopLevelItem(item) else parent.insertChild(parent.childCount, item) end if (File.ftype(file) == 'directory') populate(tree, file, item) end
Where we are using TreeWidget.addTopLevelItem or TreeWidgetItem.insertChild depending on whether we want to add a top level element or a child to a given item.
One last notice, the call:-
item.setIcon(0, Qt::Icon.new(File.ftype(file)))
Sets the icon for the item’s first column. In this version the pictures are loaded from the filesystem, so you will need to have directory.png and file.png in the execution directory.
References
Popularity: 20% [?]
