gpt4 book ai didi

c - 在 C 中将参数从 ComboBox 传递到 GTKTreeView

转载 作者:行者123 更新时间:2023-11-30 16:08:33 24 4
gpt4 key购买 nike

在C语言中,如何将ComboBox中的值传递给GtkTreeView?我们已经尝试过这段代码,但我们不知道在哪里写入 ComboBox 的 id。 ComboBox 中应使用哪些信号?

   void on_btn_car_order_clicked(GtkButton *button, gpointer *user_data){
GtkTreeIter iter;

GtkTreeView *treeview_payment1 = GTK_TREE_VIEW(user_data);
GtkListStore *liststore1 = GTK_LIST_STORE(gtk_tree_view_get_model(treeview_payment1));
gtk_list_store_append(liststore1, &iter);
gtk_list_store_set(liststore1, &iter, 0, "asdas" , 1, 1.0,2, 10.0, -1);
gtk_widget_hide (GTK_WIDGET(detail_window));
gtk_widget_show(listree_window);
}

GtkTreeView GtkWindow with ComboBox

谢谢你,此致,沙克

最佳答案

#include <gtk/gtk.h>

typedef struct
{
GtkWidget *w_car_choose;
GtkWidget *w_model_choose;
GtkWidget *w_submit_button;
GtkWidget *w_treeview;
} app_widgets;

GtkBuilder *builder;
GtkWidget *main_window;
GtkWidget *listtree_window;

void on_btn_submit_clicked(GtkButton *button, gpointer *user_data);

int main(int argc, char *argv[])
{

app_widgets *widgets = g_slice_new(app_widgets);

gtk_init(&argc, &argv);

builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "test.glade", NULL);

main_window = GTK_WIDGET(gtk_builder_get_object(builder, "main_window"));
listtree_window = GTK_WIDGET(gtk_builder_get_object(builder, "listtree_window"));


// get pointers to widgets
widgets->w_treeview = GTK_WIDGET(gtk_builder_get_object(builder, "treeview"));
widgets->w_car_choose = GTK_WIDGET(gtk_builder_get_object(builder, "car_choose"));
widgets->w_model_choose = GTK_WIDGET(gtk_builder_get_object(builder, "model_choose"));
widgets->w_submit_button = GTK_WIDGET(gtk_builder_get_object(builder, "btn_submit"));
gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->w_car_choose), 1);
gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->w_model_choose), 1);

gtk_builder_connect_signals(builder, widgets);

g_object_unref(builder);

gtk_widget_show(main_window);

g_signal_connect(GTK_WIDGET(widgets->w_submit_button), "clicked", G_CALLBACK(on_btn_submit_clicked), widgets);

gtk_main();
g_slice_free(app_widgets, widgets);

return 0;
}

void on_btn_submit_clicked(GtkButton *button, gpointer *user_data)
{
app_widgets *widgets = user_data;

GtkTreeIter iter;
GtkListStore *liststore = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(widgets->w_treeview)));
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter, 0, gtk_combo_box_text_get_active_text(GTK_COMBO_BOX(widgets->w_model_choose)), 1, gtk_combo_box_text_get_active_text(GTK_COMBO_BOX(widgets->w_car_choose)), -1);

gtk_widget_hide(GTK_WIDGET(main_window));
gtk_widget_show(listtree_window);
}

// called when window is closed
void on_window_main_destroy()
{
gtk_main_quit();
}

如果您想在 onClick 信号期间传递自定义数据,请使用

连接
g_signal_connect

https://developer.gnome.org/gobject/stable/gobject-Signals.html 。一般来说,对于连接信号,您可以使用 g_connect 手动连接它,因为大多数情况下在程序期间您可能想要修改将存储在结构中的数据,因此您可以手动连接,而不是使用空地。

#suggestions(如果你的实际程序是这样的)。您可以通过向“listtree_window”窗口添加“添加”按钮并使“main_window”成为对话框来在单个主窗口中完成上述程序。

关于c - 在 C 中将参数从 ComboBox 传递到 GTKTreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59307156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com