Gtk update treeview




















A tree view column needs to contain at least one cell renderer, but can contain multiple cell renderers. Packing renderers into a tree view column is similar to packing widgets into a GtkBox. It is important to realise what GtkTreeModel is and what it is not. It also provides functions to retrieve data from the data store, and tell the tree view what type of data is stored in the model.

As the names imply, GtkListStore is used for simple lists of data items where items have no hierarchical parent-child relationships, and GtkTreeStore is used for tree-like data structures, where items can have parent-child relationships. A list of files in a directory would be an example of a simple list structure, whereas a directory tree is an example for a tree structure. A list is basically just a special case of a tree with none of the items having any children, so one could use a tree store to maintain a simple list of items as well.

The only reason GtkListStore exists is in order to provide an easier interface that does not need to cater for child-parent relationships, and because a simple list model can be optimised for the special case where no children exist, which makes it faster and more efficient.

If you plan to store a lot of data, or have a large number of rows, you should consider implementing your own custom model that stores and manipulates data your own way and implements the GtkTreeModel interface. This will not only be more efficient, but probably also lead to saner code in the long run, and give you more control over your data.

See below for more details on how to implement custom models. Tree model implementations like GtkListStore and GtkTreeStore will take care of the view side for you once you have configured the GtkTreeView to display what you want. If you change data in the store, the model will notify the tree view and your data display will be updated. If you add or remove rows, the model will also notify the store, and your row will appear in or disappear from the view as well.

A model data store has model columns and rows. A model column represents a certain data field of an item that has a fixed data type. You need to know what kind of data you want to store when you create a list store or a tree store, as you can not add new fields later on. For example, we might want to display a list of files. We would create a list store with two fields: a field that stores the filename i.

The filename would be stored in column 0 of the model, and the file size would be stored in column 1 of the model. The GLib type system GType is used to indicate what type of data is stored in a model column.

These are the most commonly used types:. You do not need to understand the type system, it will usually suffice to know the above types, so you can tell a list store or tree store what kind of data you want to store. Advanced users can derive their own types from the fundamental GLib types. For simple structures you could register a new boxed type for example, but that is usually not necessary.

This creates a new list store with two columns. Column 0 stores a string and column 1 stores an unsigned integer for each row. At this point the model has no rows yet of course.

There are different ways to refer to a specific row. A GtkTreePath is a comparatively straight-forward way to describe the logical position of a row in the model. As a GtkTreeView always displays all rows in a model, a tree path always describes the same row in both model and view.

The picture shows the tree path in string form next to the label. Basically, it just counts the children from the imaginary root of the tree view. An empty tree path string would specify that imaginary invisible root. So you just count your way down from the root to the row in question, and you get your tree path.

Now go to the 9th child of that row. Proceed to the 4th child of the previous row. Then continue to the 1st child of that. Now you are at the row this tree path describes. This is not what it means for GTK though. While humans start counting at 1, computers usually start counting at 0.

Then go to the 10th child of that row. Pick the 5th child of that row. Then proceed to the 2nd child of the previous row. This is important to keep in mind. See the section on GtkTreeRowReference s below for a tree path that keeps updating itself to make sure it always refers to the same row when the model changes.

Usually you will rarely have to handle the string notation, it is described here merely to demonstrate the concept of tree paths. Instead of the string notation, GtkTreePath uses an integer array internally. You can get the depth i. A depth of 0 is the imaginary invisible root node of the tree view and model.

A depth of 1 means that the tree path describes a top-level row. As lists are just trees without child nodes, all rows in a list always have tree paths of depth 1. You will rarely need to operate with those either.

Note that this way you can construct and operate on tree paths that refer to rows that do not exist in model or view! The only way to check whether a path is valid for a specific model i.

GtkTreePath is an opaque structure, with its details hidden from the compiler. Another way to refer to a row in a list or tree is GtkTreeIter. A tree iter is just a structure that contains a couple of pointers that mean something to the model you are using.

Tree iters are used internally by models, and they often contain a direct pointer to the internal data of the row in question. You should never look at the content of a tree iter and you must not modify it directly either. Some of these functions are:. There are more functions that operate on iters. This is unlikely to be implemented for a variety of reasons. It should be fairly simple to write a helper function that provides this functionality though once you have read this section.

Tree iters are used to retrieve data from the store, and to put data into the store. Tree iters are often only valid for a short time, and might become invalid if the store changes with some models. It is therefore usually a bad idea to store tree iters, unless you really know what you are doing. There is a better way to keep track of a row over time: GtkTreeRowReference.

A GtkTreeRowReference is basically an object that takes a tree path, and watches a model for changes. If anything changes, like rows getting inserted or removed, or rows getting re-ordered, the tree row reference object will keep the given tree path up to date, so that it always points to the same row as before.

In case the given row is removed, the tree row reference will become invalid. After that, the tree row reference will keep updating the path whenever the model changes. If the row has been deleted, NULL will be returned instead of of a tree path. Note that using tree row references entails a small overhead. This is hardly significant for If you have read the tutorial only up to here so far, it is hard to explain really what tree row references are good for.

An example where tree row references come in handy can be found further below in the section on removing multiple rows in one go. In practice, a programmer can either use tree row references to keep track of rows over time, or store tree iters directly if, and only if, the model has persistent iters.

However, using tree row references is definitively the Right Way tm to do things, even though it comes with some overhead that might impact performance in case of trees that have a very large number of rows in that case it might be preferable to write a custom model anyway though. Especially beginners might find it easier to handle and store tree row references than iters, because tree row references are handled by pointer value, which you can easily add to a GList or pointer array, while it is easy to store tree iters in a wrong way.

We need the iter here to retrieve data from the store:. As tree iters are only valid for a short time, they are usually allocated on the stack, as in the following example keep in mind that GtkTreeIter is just a structure that contains data fields you do not need to know anything about :.

The code above asks the model to fill the iter structure to make it point to the first row in the list store. This will insert a new empty row at the end of the list. This in itself is not very useful yet of course.

We will add data to the rows in the next section. If you supply NULL instead of providing the tree iter of another row, a new top-level row will be inserted.

If you do provide a parent tree iter, the new empty row will be inserted after any already existing children of the parent. Again, there are other ways to insert a row into the tree store and they are documented in the GtkTreeStore API reference manual.

Another short example:. A common scenario is that a model needs to be filled with a lot of rows at some point, either at start-up, or when some file is opened.

An equally common scenario is that this takes an awfully long time even on powerful machines once the model contains more than a couple of thousand rows, with an exponentially decreasing rate of insertion. As already pointed out above, writing a custom model might be the best thing to do in this case.

Nevertheless, there are some things you can do to work around this problem and speed things up a bit even with the stock GTK models:. Firstly, you should detach your list store or tree store from the tree view before doing your mass insertions, then do your insertions, and only connect your store to the tree view again when you are done with your insertions.

Like this:. Secondly, you should make sure that sorting is disabled while you are doing your mass insertions, otherwise your store might be resorted after each and every single row insertion, which is going to be everything but fast. Thirdly, you should not keep around a lot of tree row references if you have so many rows, because with each insertion or removal every single tree row reference will check whether its path needs to be updated or not.

The first two arguments are a pointer to the model, and the iter pointing to the row whose data we want to change. They are followed by a variable number of column, data argument pairs, terminated by a The column refers to the model column number and is usually an enum value to make the code more readable and to make changes easier. The data should be of the same data type as the model column.

Here is an example where we create a store that stores two strings and one integer for each row:. You do not need to worry about allocating and freeing memory for the data to store. If you store a string, for example, the model will make a copy of the string and store that. If you then set the field to a new string later on, the model will automatically free the old string and again make a copy of the new string and store the copy.

The model does not know anything about the size or content of the data your pointer refers to, so it could not even make a copy if it wanted to, so you need to allocate and free the memory yourself in this case. See the GObject reference manual for details. Again, a custom model might be the better alternative, depending on the overall amount of data to be stored and retrieved. Storing data is not very useful if it cannot be retrieved again.

The pointer must point to a variable that is of the same type as the data stored in that particular model column. Here is the previous example extended to traverse the list store and print out the data stored. Those are valid contents for the model, and if you are not sure that row contents have been set to something, you need to be prepared to handle NULL pointers and the like in your code.

Run the above program with an additional empty row and look at the output to see this in effect. Removing a single row is fairly straight forward: you need to get the iter that identifies the row you want to remove, and then use one of the above functions. Here is a simple example that removes a row when you double-click on it bad from a user interface point of view, but then it is just an example :.

Of course you could also start with the iter of the first top-level row, and then step-by-step move it to the row you want, although that seems a rather awkward way of doing it. Viewed times. When bigger data rows is used and it gives error immediately: Backend terminated or disconnected. What is the solution to update the data efficiently? Code example: import gi gi. Builder builder. Dictionary data converted to list data.

ListStore str, str, str, int, float treeview1. Add a comment. Active Oldest Votes. You can find an example of how to use GLib. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Retrieves whether the user can reorder the tree via drag-and-drop.

In case the built-in entry is being used, NULL will be returned. This function is supposed to be used in a ::query-tooltip signal handler for GtkTreeView. Note that there may be invisible paths in between.

Tree coordinates start at 0,0 for row 0 of the tree, and cover the entire scrollable area of the tree. If position is -1, then the column is inserted at the end. If position is -1, then the newly created column is inserted at the end. The column is initialized with the attributes given. Convenience function that inserts a new column into the GtkTreeView with the given cell renderer and a GtkTreeCellDataFunc to set cell renderer attributes normally using data from the model.

If so, the location can be considered as the background. You might wish to take special action on clicks on the background, such as clearing a current selection, having a custom context menu or starting rubber banding. If column is NULL , then no horizontal scrolling occurs. Likewise, if path is NULL no vertical scrolling occurs. At a minimum, one of column or path need to be non- NULL. Both are expected to be between 0.

Cause the GtkTreeView ::row-activated signal to be emitted on a single click instead of a double click. Sets a user function for determining where a column may be dropped when dragged. This function is called on every column pair in turn at the beginning of a column drag to determine where a drop can take place. Sets the current keyboard focus to be at path , and selects it.

Please note that editing can only happen when the widget is realized. Sets the row that is highlighted for feedback. If path is NULL , an existing highlight is removed. This does not have any visible effects for lists. Sets the column to draw the expander arrow at. If column is NULL , then the expander arrow is always at the first visible column.

Fixed height mode speeds up GtkTreeView by assuming that all rows have the same height. Hover expansion makes rows expand or collapse if the pointer moves over them. Hover selection makes the selected row follow the pointer. The value should be specified in pixels, a value of 0 disables this feature and in this case only the default indentation will be used.

Sets the model for a GtkTreeView. If model is NULL , then it will unset the old model. If reorderable is TRUE , then the user can reorder the model by dragging and dropping rows. The reordering is implemented by setting up the tree view as a drag source and destination. Therefore, drag and drop can not be used in a reorderable view for any other purpose.

Sets the row separator function, which is used to determine whether a row should be drawn as a separator. If the row separator function is NULL , no separators are drawn.

This is the default value. Sets column as the column where the interactive search code should search in for the current model. This is useful when you want to provide a search entry in our interface at all time at a fixed position. Passing NULL for entry will make the interactive search code use the built-in popup entry again. When disabled there will be no expanders visible in trees and there will be no way to expand and collapse rows by default.

Also note that hiding the expanders will disable the default indentation. Sets the tip area of tooltip to the area path , column and cell have in common. For example if path is NULL and column is set, the tip area will be set to the full area covered by column. If you only plan to have simple text-only tooltips on full rows, you can use this function to have GtkTreeView handle these automatically for you.

Sets the tip area of tooltip to be the area covered by the row at path. Please see GtkWidget for a full list of methods. Please see GObject for a full list of methods. Updates a list of accessible states. See the GtkAccessibleState documentation for the value types of accessible states. Only enable this option if all rows are the same height. The GtkLayoutManager instance to use to compute the preferred size of the widget, and allocate its children.



0コメント

  • 1000 / 1000