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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GTK Glade C - Treeview选择奇怪的段错误相关的知识,希望对你有一定的参考价值。

我正在学习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), "");等。

以上是关于GTK Glade C - Treeview选择奇怪的段错误的主要内容,如果未能解决你的问题,请参考以下文章

Glade开发GTK+ 界面

为啥 Gdk::Pixbufs 在 Gtk::TreeView 中不可见?

C 和 GTK3 - 使用 Glade 时不显示应用程序菜单

使用 Glade 时在 GTK-Notebook 中添加选项卡

GTK得到多个范围值分割错误

Gtk+/Glade编程--简介