admin管理员组

文章数量:1306681

since Gtk 4.10 it's a little difficult to implement an gtk dialog

the only source found is reddit with just an little bug

void gtk_alert_dialog_set_buttons ( GtkAlertDialog* self, const char* const* labels ) labels Type: An array of char*

The new button labels.

The array must be NULL-terminated.

below a little code is running for the moment with gtk 4.14

#include <gtk/gtk.h>
/*
 * gcc -std=c11 -Wall -fmax-errors=10 -Wextra test_dialogue_01_gtk4.c -o test_dialogue_01_gtk4 `pkg-config --cflags --libs gtk4`

*/
void on_choose (GObject *source_object, GAsyncResult *res, gpointer user_data) 
{
  GtkAlertDialog *dialog = GTK_ALERT_DIALOG (source_object);
  GError *err = NULL;

  int button = gtk_alert_dialog_choose_finish (dialog, res, &err);

  if (err) {
    g_print("An error occured!\n");
    g_print("Error Message: %s\n", err->message);
    return;
  }

    if (button == 0)
    {
        g_print("Cancelled!\n");
        g_application_quit (G_APPLICATION (user_data));
    }
    else if (button == 1)
        g_print("Proceed with whatever!\n");
    else
        g_assert_not_reached();
    (void)  user_data;
}

void on_activate (GtkApplication *app) 
{
    GtkWidget *win = gtk_application_window_new (app);
    gtk_window_set_default_size (GTK_WINDOW (win), 400, 400);
    GtkAlertDialog *dialog = gtk_alert_dialog_new ("Heading");
    const char* buttons[] = {"Cancel", "Proceed",NULL};
    gtk_alert_dialog_set_detail (dialog, "Contents of the message");
    gtk_alert_dialog_set_buttons (dialog, buttons);
    gtk_alert_dialog_set_cancel_button (dialog, 0);   // Cancel is at index 0
    gtk_alert_dialog_set_default_button (dialog, 1);  // If the user presses enter key, "Proceed" button
                                                      // should be pressed. Proceed is at index 1

    gtk_window_present (GTK_WINDOW (win));
    gtk_alert_dialog_choose (dialog, GTK_WINDOW (win), NULL, on_choose, app);
}

int main (int argc, char** argv) 
{
  GtkApplication *app = gtk_application_new (".example.alert-dialog", G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
  return g_application_run (G_APPLICATION (app), argc, argv);
}

since Gtk 4.10 it's a little difficult to implement an gtk dialog

the only source found is reddit with just an little bug

void gtk_alert_dialog_set_buttons ( GtkAlertDialog* self, const char* const* labels ) labels Type: An array of char*

The new button labels.

The array must be NULL-terminated.

below a little code is running for the moment with gtk 4.14

#include <gtk/gtk.h>
/*
 * gcc -std=c11 -Wall -fmax-errors=10 -Wextra test_dialogue_01_gtk4.c -o test_dialogue_01_gtk4 `pkg-config --cflags --libs gtk4`

*/
void on_choose (GObject *source_object, GAsyncResult *res, gpointer user_data) 
{
  GtkAlertDialog *dialog = GTK_ALERT_DIALOG (source_object);
  GError *err = NULL;

  int button = gtk_alert_dialog_choose_finish (dialog, res, &err);

  if (err) {
    g_print("An error occured!\n");
    g_print("Error Message: %s\n", err->message);
    return;
  }

    if (button == 0)
    {
        g_print("Cancelled!\n");
        g_application_quit (G_APPLICATION (user_data));
    }
    else if (button == 1)
        g_print("Proceed with whatever!\n");
    else
        g_assert_not_reached();
    (void)  user_data;
}

void on_activate (GtkApplication *app) 
{
    GtkWidget *win = gtk_application_window_new (app);
    gtk_window_set_default_size (GTK_WINDOW (win), 400, 400);
    GtkAlertDialog *dialog = gtk_alert_dialog_new ("Heading");
    const char* buttons[] = {"Cancel", "Proceed",NULL};
    gtk_alert_dialog_set_detail (dialog, "Contents of the message");
    gtk_alert_dialog_set_buttons (dialog, buttons);
    gtk_alert_dialog_set_cancel_button (dialog, 0);   // Cancel is at index 0
    gtk_alert_dialog_set_default_button (dialog, 1);  // If the user presses enter key, "Proceed" button
                                                      // should be pressed. Proceed is at index 1

    gtk_window_present (GTK_WINDOW (win));
    gtk_alert_dialog_choose (dialog, GTK_WINDOW (win), NULL, on_choose, app);
}

int main (int argc, char** argv) 
{
  GtkApplication *app = gtk_application_new (".example.alert-dialog", G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
  return g_application_run (G_APPLICATION (app), argc, argv);
}
Share Improve this question edited Feb 3 at 23:51 genpfault 52.2k12 gold badges91 silver badges148 bronze badges asked Feb 3 at 23:45 ManmaxManmax 3011 silver badge15 bronze badges 1
  • What exactly is the question? – Holger Commented Feb 4 at 9:10
Add a comment  | 

1 Answer 1

Reset to default 0

In fact not question. I giving just a solution to replace gtk_dialog_run. It's difficult to it

本文标签: chow to migrate gtk3 to gtk4 gtkdialogrunStack Overflow