October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Selection, Modelled: Why GTK4 Puts Selection in the Model, Not the View

GTK4 list views don't own selection. A GtkSelectionModel does. Here is what that design buys you, which built-in model to pick, and how to handle its signals safely.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Tuning GtkSingleSelection

The GtkSingleSelection reference exposes two properties that capture the remaining policy choices:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • 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.

Reading and changing selection

Query narrowly

The interface offers several ways to read state. Use the smallest one that answers your question:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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:

  1. Receive the position and count.
  2. Re-query that range with is_selected() or get_selection_in_range().
  3. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.