List box - first row is always selected


Hello all,
I have problem in my testing gtk4 app written in C.

There is list box. When app start first row is laways selected, but I want that no row is selected.

I can unselect it with buttons, but it is not solution.

here is code of my ap:

#include <gtk/gtk.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <ctime>
#include <vector>

using namespace std;
GtkWidget* lBox;

int indexToSelect = 3;

void Activate (GtkWidget* wid, GtkWidget* data) {
    cout << "activate" <<endl;
    GtkListBoxRow* row = gtk_list_box_get_row_at_index(GTK_LIST_BOX(lBox),indexToSelect);

void Deactivate (GtkWidget* wid, GtkWidget* data) {
    cout << "deactivate" <<endl;
    GtkListBoxRow* row = gtk_list_box_get_row_at_index(GTK_LIST_BOX(lBox),indexToSelect);

static void appActivate (GApplication *app, gpointer user_data) {
    GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));
    gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
    gtk_widget_set_name(win,"hlavni okno apky");

    GtkWidget  *hBox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL,10);
    gtk_window_set_child (GTK_WINDOW (win), hBox);

    GtkWidget *skrollBox = gtk_scrolled_window_new();
    gtk_box_append (GTK_BOX (hBox), skrollBox);

    lBox = gtk_list_box_new();
    gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW (skrollBox), lBox);

    gtk_list_box_set_selection_mode (GTK_LIST_BOX(lBox), GTK_SELECTION_MULTIPLE);

    //gtk_list_box_unselect_all(GTK_LIST_BOX(lBox)); // does not work and first is alvays selected

    //unselect 1st row - answers: gtk_list_box_unselect_row: assertion 'GTK_IS_LIST_BOX_ROW (row)' failed
    //GtkListBoxRow* row = gtk_list_box_get_row_at_index(GTK_LIST_BOX(lBox),0);

    for (int i = 0; i < 40; i++) {
        string  jmeno = "text " + to_string(i);
        GtkWidget* lab = gtk_label_new(jmeno.c_str());
        gtk_list_box_append(GTK_LIST_BOX(lBox),  lab);

    GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);
    gtk_box_append (GTK_BOX (hBox), vBox);

    GtkWidget *btn1 = gtk_button_new_with_label("activate");
    gtk_box_append  (GTK_BOX (vBox), btn1);
    g_signal_connect_swapped(GTK_BUTTON(btn1), "clicked", G_CALLBACK(Activate), NULL);

    GtkWidget *btn2 = gtk_button_new_with_label("deactivate");
    gtk_box_append  (GTK_BOX (vBox), btn2);
    g_signal_connect_swapped(GTK_BUTTON(btn2), "clicked", G_CALLBACK(Deactivate), NULL);

    GtkWidget *btnBack = gtk_button_new_with_label("Close");
    gtk_box_append  (GTK_BOX (vBox), btnBack);
    g_signal_connect_swapped(GTK_BUTTON(btnBack), "clicked", G_CALLBACK(gtk_window_destroy), win);

    gtk_widget_show (win);

int main(int argc, char **argv) {
    GtkApplication *app;
    app = gtk_application_new ("", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (appActivate), NULL);
    return g_application_run (G_APPLICATION (app), NULL, NULL);
    g_object_unref (app);

    return 0;

In code is also two commented tests how to unselect it programically but they are not working.

please could anybody give me any advice how to solve it?
Thanks a lot!

Yes, you cannot unselect rows in an empty list.

Your issue is related to keyboard focus.

If no explicit focus widget was set, then GtkWindow moves the keyboard focus to the first focusable widget. In your app that happens to be the first list row, and as a side effect of receiving key focus, the row is selected.

That means you have two options for addressing the issue:

  1. explicitly set the keyboard focus before showing the window; for example

    gtk_widget_grab_focus (btnBack);
    gtk_widget_show (win);
  2. keep the focus on the row, but undo the selection

    gtk_widget_show (win);

Great! both soulutions work!
Tahnk You

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.