無法使g_signal_connect正確傳遞參數。它可能是因爲我不明白「c_handler」或「data」參數。代碼在這裏:C和GTK g_signal_connect傳遞參數
#include <gtk/gtk.h>
// Function prototypes
void my_file_saveas();
// Function
void my_file_saveas(GtkTextBuffer *buf)
{
// Get the start and end bounds of the buffer
GtkTextIter start, end;
gtk_text_buffer_get_bounds (buf, &start, &end);
}
int main(int argc, char *argv[])
{
GtkWidget *menubar;
GtkWidget *fileMenu;
GtkWidget *fileMh;
GtkWidget *saveasMi;
GtkWidget *view;
GtkTextBuffer *buffer;
GtkWidget *scroll_window;
GtkWidget *vbox;
GtkWidget *window;
// Call this function first
gtk_init(&argc, &argv);
// Create widgets
menubar = gtk_menu_bar_new();
fileMenu = gtk_menu_new();
fileMh = gtk_menu_item_new_with_label("File");
saveasMi = gtk_menu_item_new_with_label("Save As");
view = gtk_text_view_new();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
// Set out the menubar
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), saveasMi);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMh), fileMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMh);
// Create a scroll window and add the view to it
scroll_window = gtk_scrolled_window_new (NULL, NULL);
gtk_container_add(GTK_CONTAINER(scroll_window), view);
// Create a vertical box and add the menubar and scroll_window
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), scroll_window, TRUE, TRUE, 0);
// Setup top level window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
// Put the box in the top level window
gtk_container_add(GTK_CONTAINER(window), vbox);
// Events
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(saveasMi), "activate", G_CALLBACK(my_file_saveas), buffer);
// Display the window
gtk_widget_show_all(window);
// Runs main
gtk_main();
return 0;
}
它編譯好。但是,當我運行它的輸出是在這裏:
[email protected] ~/c/test $ ./ed10
(ed10:2974): Gtk-CRITICAL **: gtk_text_buffer_get_bounds: assertion 'GTK_IS_TEXT_BUFFER (buffer)' failed