Gnocl Cookbook‎ > ‎

Avoiding List Row/Column Madness

Adding rows of data to a gnocl::list is pretty straightforward. Assuming that there is a list object whose widget-id is $lst the inclusion of a row can be achieved with this:

$lst add [list <b>apple</b> 1 3 0.7 0 1 65 1 1 ] -singleRow 1 

Pretty straight forward, a single row is added to the end of the gnocl::list treemodel.

Setting individual cell values is relatively simple too, but now we must supply row and column indices.

$lst cellConfigure 0 1 -value 0

But, as humans we think with names rather than numbers, so how can I treat a cell as though it had a name?

The solution to this comes through the use of the -titles option. This is not mandatory but is really useful if we want to access cells by name. Unlike the -types option for setting the internal tree model cell render, -titles can re-configured.

In the above example, we can see that widget lst has a total of 9 columns. Although not shown above, the object was created using the following 2 commands:

set titles [list markup boolean integer float radio progress spinner combo accelerator]

set lst [gnocl::list \
    -types [list markup boolean integer float radio progress spinner combo accelerator] \
    -titles $titles ]

Although the string "titles" is used to set the column titles, it can be reused as a list in order to address specific columns. In effect, extracting a title index from this list will return the column number. This is can be done using the Tcl lsearch command.

So, to change the value of the progressbar in row 0, all that needs to be called is:

$lst cellConfigure 0 [lsearch $titles progress] -value 30

Now, the question arises can something similar be done for rows? Well, yes.

Whenever a row is added to the model, the interpretor will return the path to the row, in other words its row number. For example:

set myRow [$lst add [list <b>apple</b> 1 3 0.7 0 1 65 1 1 ] -singleRow 1] 

So, each cell can be accessed through referencing.

$lst cellConfigure $myRow [lsearch $titles progress] -value 30

And, of course, if the original $titles string is outside the scope of any particular Tcl procedure, the list can retrieved using cget because we know the widget-id.

$lst cellConfigure 0 [lsearch [$lst cget -titles] progress] -value 30




Comments