gpt4 book ai didi

c - GTK Glade C - TreeView 选择奇怪的段错误

转载 作者:行者123 更新时间:2023-11-30 16:37:22 25 4
gpt4 key购买 nike

我目前正在学习 GTK 和 Glade。我正在一点一点地尝试制作一个小型会计程序。现在,我发生了一个奇怪的段错误,我不明白为什么。我只想使用 gtk_tree_selection_unselect_all 取消选择所有 GtkTreeSelection。请注意,如果尝试选择全部,它也会崩溃。

奇怪的是,如果我注释掉 on_treeview_selection1_changed 函数,则不会发生段错误。所以看来这个函数在某种程度上与另一个函数混淆了..有可能吗?

main.c:

#include <gtk/gtk.h>

typedef struct {
GtkTreeView *treeview;
GtkWidget *ent_date;
GtkWidget *ent_lib;
GtkWidget *ent_mont;
GtkWidget *but_unset;
} app_widgets;

enum
{
DATE_COLUMN,
LIBELLE_COLUMN,
MONTANT_COLUMN,
N_COLUMN
};

void on_window_main_destroy() {
gtk_main_quit();
}

void on_button_unset_clicked (GtkButton *button, app_widgets *app_wid) {

GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(app_wid->treeview));

gtk_tree_selection_unselect_all(selection);
//gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
//gtk_tree_selection_select_all(selection);

}

void on_treeview_selection1_changed (GtkTreeSelection *treeselection, app_widgets *app_wid) {

gchar *a,*b,*c;
GtkTreeIter iter;
GtkTreeModel *model;

if (gtk_tree_selection_get_selected(treeselection, &model, &iter))
{
gtk_tree_model_get(model, &iter, DATE_COLUMN, &a, LIBELLE_COLUMN, &b,MONTANT_COLUMN, &c, -1);

}

gtk_entry_set_text(GTK_ENTRY(app_wid->ent_date), a);
gtk_entry_set_text(GTK_ENTRY(app_wid->ent_lib), b);
gtk_entry_set_text(GTK_ENTRY(app_wid->ent_mont), c);

g_free(a);
g_free(b);
g_free(c);

}

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

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);

widgets->treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview1"));
widgets->ent_date = GTK_WIDGET(gtk_builder_get_object(builder, "entry_date"));
widgets->ent_lib = GTK_WIDGET(gtk_builder_get_object(builder, "entry_lib"));
widgets->ent_mont = GTK_WIDGET(gtk_builder_get_object(builder, "entry_mont"));
widgets->but_unset = GTK_WIDGET(gtk_builder_get_object(builder, "button_unset"));

gtk_builder_connect_signals(builder, widgets);

gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(builder, "window1")));

g_object_unref(builder);

gtk_main();

g_slice_free(app_widgets, widgets);
return 0;
}

和 test.glade 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name Date -->
<column type="gchararray"/>
<!-- column-name Libellé -->
<column type="gchararray"/>
<!-- column-name Montant -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">20.05.2017</col>
<col id="1" translatable="yes">Something - here</col>
<col id="2" translatable="yes">30.20</col>
</row>
<row>
<col id="0" translatable="yes">25.06.2017</col>
<col id="1" translatable="yes">Something else - overthere</col>
<col id="2" translatable="yes">24.90</col>
</row>
<row>
<col id="0" translatable="yes">11.08.2017</col>
<col id="1" translatable="yes">Third thing - lala</col>
<col id="2" translatable="yes">5.15</col>
</row>
</data>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="window_position">center</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore1</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1">
<signal name="changed" handler="on_treeview_selection1_changed" swapped="no"/>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="resizable">True</property>
<property name="fixed_width">70</property>
<property name="title" translatable="yes">Date</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn2">
<property name="resizable">True</property>
<property name="title" translatable="yes">What</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext2"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn3">
<property name="resizable">True</property>
<property name="title" translatable="yes">Amount</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext3"/>
<attributes>
<attribute name="text">2</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box_date">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Date</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry_date">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox" id="box_lib">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">What</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry_lib">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="box_mont">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Amount</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry_mont">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkFixed" id="fixed1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button_unset">
<property name="label" translatable="yes">Unselect</property>
<property name="width_request">100</property>
<property name="height_request">23</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="xalign">0.47999998927116394</property>
<property name="yalign">0.44999998807907104</property>
<signal name="clicked" handler="on_button_unset_clicked" swapped="no"/>
</object>
<packing>
<property name="x">370</property>
<property name="y">68</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

最佳答案

on_treeview_selection1_changed()即使gtk_tree_selection_get_selected(treeselection, &model, &iter),您也尝试设置输入字段返回 FALSE,因为当前没有选择。在这种情况下,这意味着变量 a、b 和 c 尚未由 gtk_tree_model_get(model, &iter, DATE_COLUMN, &a, LIBELLE_COLUMN, &b,MONTANT_COLUMN, &c, -1) 设置。 ,并调用三个 gtk_entry_set_text因此,指令将导致段错误(出于同样的原因,g_free指令也不起作用)。 A、b 和 c 必须指向字符串而不是某些随机值。

您可能只想使用 gtk_entry_set_text(GTK_ENTRY(app_wid->ent_date), "");等等,如果您想在没有选择的情况下删除输入字段的内容。

关于c - GTK Glade C - TreeView 选择奇怪的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47972163/

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