[Kde-bindings] QTreeView with Qyoto and Kimono
Tim Taubert
ttmails at gmx.de
Sun Jun 14 21:56:38 UTC 2009
Arno Rehn wrote:
> On Sunday 14 June 2009 20:35:24 Tim Taubert wrote:
>> Hey,
>>
>> I'm currently trying to develop a text editor with Mono/Qyoto/Kimono. I
>> have a problem with the QTreeView component that uses a custom Model
>> (same error occurs using QDirModel). Every time I try to expand a 2nd
>> level node the application crashes with the following error message:
>> [snip]
>> Any ideas? Need some more information? :)
> The source or, even better, a small test case of you app would help us very
> much to identify the problem :).
It took me some time to reduce the code to a cleaner subset still
triggering the error. This is the smallest example I was able to
produce. It is supposed to be some kind of a project manager with only
projects as the root nodes. Every project has a root directory and lists
the files and sub-directories under it. For the sake of simplicity every
node has the same label in this example.
(Thanks in advance!)
Classes:
--------
Editor => application class with a tree view main widget
ItemModel => the model used by the tree view
Node => the node representing a file/folder with its information
Description:
------------
This example raises a glibc error every time I expand a 2nd or 3rd level
node.
Code:
-----
using System;
using System.Collections.Generic;
using Qyoto;
public class Editor
{
public static int Main(String[] args)
{
new QApplication(args);
// project manager
QTreeView tv = new QTreeView();
ItemModel model = new ItemModel();
tv.SetModel(model);
tv.Show();
return QApplication.Exec();
}
}
public class ItemModel : QAbstractItemModel
{
public ItemModel () : base()
{
}
public override int RowCount (Qyoto.QModelIndex parent)
{
if (!parent.IsValid())
{
// this is the number of projects
return 1;
}
return ((Node)parent.InternalPointer()).RowCount();
}
public override QVariant Data (Qyoto.QModelIndex index, int role)
{
if (role == (int)ItemDataRole.DisplayRole)
{
return "node";
}
return new QVariant();
}
public override QModelIndex Parent (Qyoto.QModelIndex child)
{
Node node = (Node)child.InternalPointer();
node = node.Parent();
if (null == node)
{
// project nodes have no parent
return new QModelIndex();
}
return this.CreateIndex(node.Row(), 0, node);
}
public override QModelIndex Index (int row, int column,
Qyoto.QModelIndex parent)
{
Node node;
if (parent.IsValid())
{
node = ((Node)parent.InternalPointer()).ChildAt(row);
}
else
{
// create a new project node
node = new Node("/", row);
}
return this.CreateIndex(row, column, node);
}
public override int ColumnCount (Qyoto.QModelIndex parent)
{
return 1;
}
}
public class Node
{
private string path;
private int row;
private Node parent;
private List<Node> children;
public Node(string path, int row)
{
this.path = path;
this.row = row;
}
public Node(string path, int row, Node parent) : this(path, row)
{
this.parent = parent;
}
public Node Parent()
{
return parent;
}
public string Path()
{
return path;
}
public int Row()
{
return row;
}
public int RowCount()
{
return Children().Count;
}
// fetch all sub-directories and files
protected List<Node> Children()
{
if (null == children)
{
QDir dir = new QDir(Path());
dir.SetFilter((uint)(QDir.Filter.NoDotAndDotDot |
QDir.Filter.AllEntries));
QDirIterator it = new QDirIterator(dir);
children = new List<Node>();
while (it.HasNext())
{
children.Add(new Node(it.Next(), children.Count, this));
}
}
return children;
}
public Node ChildAt(int index)
{
return Children()[index];
}
}
More information about the Kde-bindings
mailing list