Change widgets in box

,

Hello all,
I would like to ask for some help in gtk4 app writen in C.

I need change (after button click) widgets in gtk_box.

I have prepared function to clean g_hBox (DeleteHBox). It should delete all widgets. And than I would like fill g_hBox with new widgets.

But it does not work. I thing that problem is in function DeleteHBox. This function is there in two variants. V2 crash and V1 do not crash but also does not work.

Please could any body give me any advice how to correct my code:

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

using namespace std;

GtkWidget *g_hBox;
int g_cnt = 10;

/*
void DeleteHBox ()
{
    cout << "DeleteHBox V2" << endl;

    int lCnt = 0;

    GtkWidget *child = gtk_widget_get_first_child (g_hBox);
    GtkWidget *widgetKSmazani;
    while (child != NULL)
    {

        widgetKSmazani = child;
        cout << "deleting " << to_string(lCnt) << endl;
        ++lCnt;
        gtk_box_remove(GTK_BOX(g_hBox), widgetKSmazani);

        child = gtk_widget_get_next_sibling (child);
    }
    if (child != NULL)
    {
        gtk_box_remove(GTK_BOX(g_hBox), child);
    }
}
*/

void DeleteHBox ()
{
    cout << "DeleteHBox V1" << endl;
    gtk_widget_unmap(g_hBox);
    g_hBox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL,10);
}

void FillHBox ()
{
     for (int i = 0; i < g_cnt; ++i)
    {
        GtkWidget *l = gtk_label_new(to_string(i).c_str());
        gtk_box_append (GTK_BOX (g_hBox), l);
    }
}

void ChangeWidgets ( )
{
    cout << "opening"<< endl;

    DeleteHBox();

    FillHBox();
}

void W1 (GtkWidget* wid, GtkWidget* data)
{
    cout << "W1" << endl;
    g_cnt = 5;
    ChangeWidgets();
}

void W2 (GtkWidget* wid, GtkWidget* data)
{
    cout << "W2" << endl;
    g_cnt = 10;
    ChangeWidgets();
}

static void appActivate (GApplication *app, gpointer user_data) {
    GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));
    gtk_window_set_title (GTK_WINDOW (win), "Title");
    gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);

    GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);

    gtk_window_set_child (GTK_WINDOW (win), vBox);

    GtkWidget *labText = gtk_label_new("label");
    gtk_box_append (GTK_BOX (vBox), labText);

    g_hBox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL,0);
    gtk_box_append (GTK_BOX (vBox), g_hBox);

    FillHBox();

    GtkWidget *labText2 = gtk_label_new("label2");
    gtk_box_append (GTK_BOX (vBox), labText2);

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

    GtkWidget *btn2 = gtk_button_new_with_label("2");
    gtk_box_append  (GTK_BOX (vBox), btn2);
    g_signal_connect_swapped(GTK_BUTTON(btn2), "clicked", G_CALLBACK(W2), 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 ("testing.app", 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;
}

thanks a lot all!

 gtk_box_remove(GTK_BOX(g_hBox), widgetKSmazani);

        child = gtk_widget_get_next_sibling (child);
  child = gtk_widget_get_next_sibling (child);

 gtk_box_remove(GTK_BOX(g_hBox), widgetKSmazani);

Have you done any debugging?

I have found solution. I hace written 3rd version of function DeleteHBox - I delete widgets there from the end and it is working:

void DeleteHBox ()
{
    cout << "DeleteHBox V3" << endl;

    int lCnt = 0;
    GtkWidget *widgetKSmazani;
    GtkWidget *child = gtk_widget_get_last_child (g_hBox);
    while (child != NULL)
    {
        child = gtk_widget_get_last_child (g_hBox);
        widgetKSmazani = child;
        cout << "deleting " << to_string(lCnt) << endl;
        ++lCnt;
        if (GTK_IS_WIDGET(widgetKSmazani))
        {
            gtk_box_remove(GTK_BOX(g_hBox), widgetKSmazani);
        }
    }
}

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