Retrieving Column Data from a ListStore?

Ron Tarrant rontarrant at gmail.com
Wed Apr 24 14:38:05 UTC 2019


Another thing I've been struggling with is fetching data from a 
TreeIter. Getting the iter itself is no problem, but when it 
comes to getting the contents of a specific column within the 
iter, none of the methods I've tried have worked. In fact, it 
almost seems like none of them are implemented.

I've written a callback function named onChanged() in the 
MyComboBox class that gets the TreeIter, but that's all I can get 
working. If anyone knows how to grab data from column 2 of the 
iter (see the MyListStore class at the bottom for the structure 
of the store) I'd be ever so grateful if you could fill in 
whatever else would be needed in that callback.

Here's the code:

```
import std.stdio;
import std.conv;

import gtk.MainWindow;
import gtk.Main;
import gtk.Box;
import gtk.Widget;
import gtk.ComboBox;
import gtk.CellRendererText;
import gtk.ListStore;
import gtk.TreeIter;

void main(string[] args)
{
	Main.init(args);

	TestRigWindow myTestRig = new TestRigWindow("Test Rig");
	
	Main.run();
	
} // main()


class TestRigWindow : MainWindow
{
	AppBox appBox;
	
	this(string title)
	{
		super(title);
		addOnDestroy(&quitApp);
		
		appBox = new AppBox();
		add(appBox);
		
		showAll();

	} // this() CONSTRUCTOR
	
		
	void quitApp(Widget widget)
	{
		writeln("Bye.");
		Main.quit();
		
	} // quitApp()

} // class myAppWindow


class AppBox : Box
{
	MyComboBox myComboBox;
	
	this()
	{
		super(Orientation.VERTICAL, 10);
		
		myComboBox = new MyComboBox();
		packStart(myComboBox, false, false, 0);

	} // this()

} // class AppBox


class MyComboBox : ComboBox
{
	MyListStore myListStore;
	CellRendererText renderer;
	
	this()
	{
		super(false);
		myListStore = new MyListStore();
		setModel(myListStore);
		
		renderer = new CellRendererText();
		packStart(renderer, true);
		addAttribute(renderer, "text", 1);
		setActive(0);
		
		addOnChanged(&onChanged);

	} // this()
	
	
	void onChanged(ComboBox cb)
	{
		TreeIter treeIter;
		string data;
		
		writeln("active: ", getActive());
		getActiveIter(treeIter);
		
		
	} // onChanged()
	
} // class MyComboBox


class MyListStore : ListStore
{
	TreeIter treeIter;
	string[] items = ["bikes", "bumps", "deer", "falling rocks", 
"road crews", "cattle"];
	
	this()
	{
		super([GType.INT, GType.STRING, GType.STRING]);
		
		for(int i = 0; i < items.length; i++)
		{
			treeIter = createIter();
			
			setValue(treeIter, 0, i + 10);
			setValue(treeIter, 1, items[i]);
			setValue(treeIter, 2, items[i] ~ " Extra data " ~ 
to!string(i));
		}

	} // this()
	
} // class MyListStore

```


More information about the Digitalmars-d-learn mailing list