模式窗口中的“确认”按钮未触发 GtkButton 信号回调的问题

问题描述 投票:0回答:1

我正在开发一个 GTK4 应用程序,其中有一个带有“添加项目”表单的模式窗口。模式窗口包括一个输入表单和一个“确认”按钮。由于某种原因,单击“确认”按钮时没有触发该按钮的信号回调。

  1. 我正在使用 Glade 进行 UI 设计。
  2. 主窗口其他按钮成功触发各自的回调。
  3. “确认”按钮放置在 GtkDialog(模态窗口)内。
  4. 当我点击“确认”按钮时,我希望看到确认消息,但什么也没有发生。

代码:

#include <gtk/gtk.h>
#include "tov.h"

// Forward declarations of signal handlers
void on_button_add_clicked(GtkButton *button, gpointer user_data);
void on_button_delete_clicked(GtkButton *button, gpointer user_data);
void on_button_show_content_clicked(GtkButton *button, gpointer user_data);
void on_button_modify_content_clicked(GtkButton *button, gpointer user_data);
void on_button_confirm_clicked(GtkButton *button, gpointer user_data);

// ...

// Activation callback
static void activate(GtkApplication *app, gpointer user_data)
{
    GtkBuilder *builder;
    GtkWidget *window;
    GtkWidget *modal_window;
    GtkWidget *button_add, *button_delete, *button_show_content, *button_modify_content, *confirm_button;

    // Initialize your TOV file (replace with actual initialization)
    FichierTOV *fichier = g_malloc(sizeof(FichierTOV));
    initialiserFichierTOV(fichier, MAX_ENREGISTREMENTS);

    // Create a new builder from the Glade file
    builder = gtk_builder_new_from_file("design.glade");

    // Get the main window and buttons from the builder
    window = GTK_WIDGET(gtk_builder_get_object(builder, "main_window"));
    button_add = GTK_WIDGET(gtk_builder_get_object(builder, "button_add"));
    button_delete = GTK_WIDGET(gtk_builder_get_object(builder, "button_delete"));
    button_show_content = GTK_WIDGET(gtk_builder_get_object(builder, "button_show_content"));
    button_modify_content = GTK_WIDGET(gtk_builder_get_object(builder, "button_modify_content"));
    confirm_button = GTK_WIDGET(gtk_builder_get_object(builder, "button_confirm"));

    if (!window || !button_add || !button_delete || !button_show_content || !button_modify_content || !confirm_button)
    {
        g_printerr("Failed to fetch widgets from the Glade file\n");
        g_object_unref(builder);
        return;
    }

    // Set the application for the window and connect signals
    gtk_window_set_application(GTK_WINDOW(window), app);

    // Connect signals
    g_signal_connect(button_add, "clicked", G_CALLBACK(on_button_add_clicked), window);
    g_signal_connect(button_delete, "clicked", G_CALLBACK(on_button_delete_clicked), fichier);
    g_signal_connect(button_show_content, "clicked", G_CALLBACK(on_button_show_content_clicked), fichier);
    g_signal_connect(button_modify_content, "clicked", G_CALLBACK(on_button_modify_content_clicked), fichier);

    // Create the modal window
    modal_window = GTK_WIDGET(gtk_builder_get_object(builder, "add_item_modal"));

    if (!modal_window)
    {
        g_printerr("Failed to fetch modal window from the Glade file\n");
        g_object_unref(builder);
        return;
    }

    // Set the modal window for the "Confirm" button
    g_signal_connect(confirm_button, "clicked", G_CALLBACK(on_button_confirm_clicked), modal_window);

    // Show the main window
    gtk_window_present(GTK_WINDOW(window));

    // Release the builder
    g_object_unref(builder);
}

// Signal handler for the "Add" button
void on_button_add_clicked(GtkButton *button, gpointer user_data)
{
    printf("Add button was clicked\n");
    GtkBuilder *builder = gtk_builder_new_from_file("design.glade");
    GtkWidget *modal_window = GTK_WIDGET(gtk_builder_get_object(builder, "add_item_modal"));

    // Set parent window for the modal
    GtkWindow *parent_window = GTK_WINDOW(user_data);
    gtk_window_set_transient_for(GTK_WINDOW(modal_window), parent_window);

    // Present the modal window using gtk_window_present
    gtk_window_present(GTK_WINDOW(modal_window));
    g_object_unref(builder);
}

void on_button_confirm_clicked(GtkButton *button, gpointer user_data)
{
    GtkWidget *modal_window = GTK_WIDGET(user_data);
    printf("Confirm button was clicked\n");
    // Additional logic...
}

void on_button_modify_content_clicked(GtkButton *button, gpointer user_data)
{
    printf("Modify Content button was clicked\n");
}

void on_button_show_content_clicked(GtkButton *button, gpointer user_data)
{
    printf("Show Content button was clicked\n");
}

void on_button_delete_clicked(GtkButton *button, gpointer user_data)
{
    printf("Delete button was clicked\n");
}

// Add your other functions here...

// Your file I/O functions and other logic...

int main(int argc, char *argv[])
{
    GtkApplication *app;
    int status;

    // Create a new application
    app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);

    // Connect the activation signal to the activation callback
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);

    // Start the application
    status = g_application_run(G_APPLICATION(app), argc, argv);

    g_object_unref(app);
    return status;
}

Glade UI 说明:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="4.0"/>
  <object class="GtkWindow" id="main_window">
    <property name="title">File Manager</property>
    <property name="default-width">600</property>
    <property name="default-height">400</property>
    <child>
      <object class="GtkBox" id="vbox_main">
        <property name="orientation">vertical</property>
        <property name="spacing">10</property>
        <!-- Vertical Box for displaying content and buttons -->
        <child>
          <!-- Label for displaying content -->
          <object class="GtkLabel" id="label_content">
            <property name="width-request">580</property>
            <property name="height-request">600</property>
            <property name="visible">True</property>
            <property name="label">[Content will be displayed here]</property>
            <property name="wrap">true</property>
          </object>
        </child>
        <child>
          <!-- Horizontal Box for buttons -->
          <object class="GtkBox" id="hbox_buttons">
            <property name="homogeneous">true</property>
            <child>
              <object class="GtkButton" id="button_add">
                <property name="label">Add</property>
                <property name="width-request">150</property> <!-- Increase width -->
                <property name="height-request">100</property> <!-- Increase height -->
              </object>
            </child>
            <child>
              <object class="GtkButton" id="button_delete">
                <property name="label">Delete</property>
                <property name="width-request">150</property> <!-- Increase width -->
                <property name="height-request">100</property> <!-- Increase height -->
              </object>
            </child>
            <child>
              <object class="GtkButton" id="button_show_content">
                <property name="label">Show Content</property>
                <property name="width-request">150</property> <!-- Increase width -->
                <property name="height-request">100</property> <!-- Increase height -->
              </object>
            </child>
            <child>
              <object class="GtkButton" id="button_modify_content">
                <property name="label">Modify Content</property>
                <property name="width-request">150</property> <!-- Increase width -->
                <property name="height-request">100</property> <!-- Increase height -->
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>

  <object class="GtkDialog" id="add_item_modal">
    <property name="title">Add Item</property>
    <property name="modal">True</property>
    <child>
        <placeholder/>
    </child>
    <child>
        <object class="GtkBox" id="modal_box">
            <property name="orientation">vertical</property>
            <property name="margin-start">10</property>
            <property name="margin-end">10</property>
            <property name="margin-top">10</property>
            <property name="margin-bottom">10</property>
            <child>
                <object class="GtkLabel" id="first_name_label">
                    <property name="label">Enter first name:</property>
                </object>
            </child>
            <child>
                <object class="GtkEntry" id="first_name_entry"/>
            </child>
            <child>
                <object class="GtkLabel" id="second_name_label">
                    <property name="label">Enter second name:</property>
                    <property name="margin-top">10</property>
                </object>
            </child>
            <child>
                <object class="GtkEntry" id="second_name_entry"/>
            </child>
            <child>
                <object class="GtkLabel" id="id_label">
                    <property name="label">Enter ID (numbers only):</property>
                    <property name="margin-top">10</property>
                </object>
            </child>
            <child>
                <object class="GtkEntry" id="id_entry"/>
            </child>
            <child>
                <object class="GtkButton" id="button_confirm">
                    <property name="label">Confirm</property>
                    <property name="margin-top">20</property>
                </object>
            </child>
        </object>
    </child>
</object>
</interface>

如何解决这个问题

c user-interface gtk gtk3 gtk4
1个回答
0
投票

问题与变量/对象的范围有关。 以下内容应包含在 on_button_add_clicket 函数中。

GtkWidget *confirm_button; 
confirm_button = GTK_WIDGET(gtk_builder_get_object(builder, "button_confirm"));
// Create the modal window
    modal_window = GTK_WIDGET(gtk_builder_get_object(builder, "add_item_modal"));

    if (!modal_window)
    {
        g_printerr("Failed to fetch modal window from the Glade file\n");
        g_object_unref(builder);
        return;
    }

    // Set the modal window for the "Confirm" button
    g_signal_connect(confirm_button, "clicked", G_CALLBACK(on_button_confirm_clicked), modal_window);

当然要在原来的位置删除。

© www.soinside.com 2019 - 2024. All rights reserved.