Bug 197103 - [qyoto] Application crashes when expanding 2nd level nodes in a QTreeView
Summary: [qyoto] Application crashes when expanding 2nd level nodes in a QTreeView
Status: RESOLVED NOT A BUG
Alias: None
Product: bindings
Classification: Developer tools
Component: general (show other bugs)
Version: unspecified
Platform: Compiled Sources Linux
: NOR crash
Target Milestone: ---
Assignee: kde-bindings
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-06-19 08:14 UTC by ttmails2
Modified: 2009-06-20 17:06 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ttmails2 2009-06-19 08:14:24 UTC
Version:            (using Devel)
Compiler:          i686-pc-linux-gnu-4.3.3 -O2 -mtune=core2 -march=core2 -fomit-frame-pointer -pipe
OS:                Linux
Installed from:    Compiled sources

I am able to reproduce this _always_ using the following code:

(I tried to cut it down to the necessary parts, but needed a custom tree model)

using System;
using System.Collections.Generic;

using Qyoto;

public class Editor
{
	public static int Main(String[] args)
	{
		new QApplication(args);
		
		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];
	}
}
Comment 1 ttmails2 2009-06-20 17:06:38 UTC
I'm sorry. My fault. Wasn't really clear about how to handle the root node.