What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
In GTK4, “which rows are selected” is not state the list view owns. It lives in a GtkSelectionModel, an interface that extends GListModel and adds selection queries, requested changes and a notification signal. The view displays and interacts with whatever model you hand it. Selection policy (none, one or many) is therefore something you choose by composing a model, not a mode you switch on a widget.
The model, in one picture
A plain list model answers two questions: which items exist, and in what order. A selection model wraps that and adds a third: for each position, is the item selected? Matthias Clasen’s GTK Development Blog post of September 8, 2020 puts it this way: for each item in the underlying model, a GtkSelectionModel maintains whether it is selected or not.
| # | Preview | Product | Price | |
|---|---|---|---|---|
| 1 |
|
Gtk+ Programming in C | $30.94 | Buy on Amazon |
| 2 |
|
Foundations of GTK+ Development | $22.87 | Buy on Amazon |
| 3 |
|
An Introduction to C & GUI Programming | $17.99 | Buy on Amazon |
| 4 |
|
Competitive Programming 4 - Book 2: The Lower Bound of Programming Contests in the 2020s | $24.00 | Buy on Amazon |
| 5 |
|
Programming Python with GTK and SQLite | $25.70 | Buy on Amazon |
As an Amazon Associate I earn from qualifying purchases.
The list view sits on top. According to the GTK4 list widget overview, a list view uses a model implementing GtkSelectionModel. That model decides which items can be selected and how selecting one affects the others. Selection behavior and sharing are properties of the selection model, and selection is exposed to list items through their selected property.
Free tools Windows power users keep installed
One-click scans. No signup required.
This is a model/view separation. It does not mean every GTK view uses selection, and it does not mean you must write selection code yourself.
#1 Best Overall
What you gain by putting selection in the model
- Policy is swappable. Changing from single to multiple selection means wrapping your data in a different model. The view code stays the same.
- Selection can be shared. Because state belongs to a model object, more than one consumer can use the same selection model. The list widget overview names sharing as a property of the selection model.
- Selection stays with the data. It is indexed by position in the list model, so it can track item changes in the model itself rather than in widget state that may be recycled.
- Custom behavior has a seam. If the built-in modes are not enough, you implement the interface rather than subclassing a view.
Choosing a selection model
GTK states: “GTK provides default implementations of the most common selection modes such as GtkSingleSelection, so you will only need to implement this interface if you want detailed control about how selections should be handled.” Most applications just pick one.
| Model | Selectable items | Use when |
|---|---|---|
GtkNoSelection |
Zero | The list is display-only or rows are activated rather than selected. |
GtkSingleSelection |
One | One row is the current item, such as a master list driving a detail pane. |
GtkMultiSelection |
Many | Users act on several rows at once. |
The cardinalities come from the GTK development blog. A custom implementation is worth considering only when none of these policies fits.
Rank #2
Tuning GtkSingleSelection
The GtkSingleSelection reference exposes two properties that capture the remaining policy choices:
autoselect: whether the model keeps something selected automatically.can-unselect: whether the user may clear the current selection.
It also keeps a selected item selected when that item is removed and re-added within the same items-changed emission. A sort-model reorder is the typical case, so sorting doesn’t drop the user’s selection.
Rank #3
Reading and changing selection
Query narrowly
The interface offers several ways to read state. Use the smallest one that answers your question:
is_selected()for a single position.get_selection_in_range()for a small range, such as the positions named in a change notification.get_selection()only when you truly need everything. It returns all selected positions as aGtkBitsetand may be slow.
Requesting changes
Changes are requests, not guarantees. Per the interface reference, return values of the selection functions mainly indicate complete failure, and selection may happen asynchronously. Don’t treat a return value as proof that state changed. Wait for the signal, then query.
When you can, prefer the simpler operations. set_selection() is the fine-grained update path, but GTK notes that implementations are more likely to support the simpler operations.
Handling selection-changed correctly
The selection-changed signal reports the first position and the number of items whose state may have changed. It does not report the new values. The pattern is:
Best Value
- Receive the position and count.
- Re-query that range with
is_selected()orget_selection_in_range(). - Update your own state from the answer.
Avoid modifying the model inside the handler. GTK warns this can cause reentrancy problems and suggests deferring the change when unsure, for example to an idle callback.
The gap: items that arrive already selected
Items added through GListModel::items-changed may already be selected, and no selection-changed is emitted for them. If you mirror the full selection, for example to drive a “3 selected” label, listen to both signals.
When to write your own
Implement GtkSelectionModel only for behavior the built-ins can’t express. You then take on the whole contract: item and range queries, the selection operations, and accurate selection-changed notifications. The interface reference is the place to start.
Version note
The linked API references are labeled API version 4.0, and some generated pages report library versions in the 4.23 range. The blog discussion dates from 2020. Check the reference for the GTK minor version you target before relying on fine details.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




