无法在树视图中切换复选框

时间:2014-12-08 07:58:12

标签: c gtk3 glade gtktreeview

我正在使用一个包含2列的表格的简单对话框,其中一列是复选框。我添加了一个GtkCellRendererToggle,但那个不可点击。我添加了一个永远不会调用的onToggle函数。 单元格渲染器是可激活的,我已经添加了一个调用来在C代码中第二次激活它以保证安全,但没有任何帮助。我究竟做错了什么? 这是代码:

void
on_button_delete_files_click (GtkTreeSelection * widget, gpointer user_data)
{
    GtkDialog    *  dialog = NULL;
    GtkWidget    *  file_choser;
    char         *  msg = NULL;
    GtkListStore *  liststore  = NULL;
    GtkTreeIter     iter;
    GDir         *  dir = NULL;
    char         *  filename = NULL;

    if (!builder_delete_files_dialog) {
        GError * error = NULL;
        builder_delete_files_dialog = gtk_builder_new ();
        if (!gtk_builder_add_from_file (builder_delete_files_dialog, "deletedlg.glade", &error)) {
            g_warning ("%s", error->message);
            g_free (error);
            exit (1);
        }
    }

    //
    // Get list store
    //
    liststore = GTK_LIST_STORE (gtk_builder_get_object (builder_delete_files_dialog,  "podcast_file_liststore"));
    //gtk_list_store_clear (liststore);

    //
    // Add all files
    //
    dir = g_dir_open (download_directory, 0, NULL);
    if (NULL == dir) {

    }

    while (NULL != (filename = g_dir_read_name (dir))) {
        if (NULL == g_strrstr (filename, ".mp3"))
            continue;

        gtk_list_store_append(liststore, &iter);
        gtk_list_store_set (liststore, &iter,
                              TO_BE_DELETED, FALSE,
                              FILENAME, filename,
                              -1);
    }
    //
    // nice try since stuff is not activatable
    //

    GtkCellRendererToggle * toggle = GTK_CELL_RENDERER_TOGGLE(gtk_builder_get_object (builder_delete_files_dialog, "cellrenderertoggle1"));
    gtk_cell_renderer_toggle_set_activatable (toggle, TRUE);

    dialog = GTK_WIDGET(gtk_builder_get_object (builder_delete_files_dialog,  "dialog_delete_files"));

    int rc = gtk_dialog_run (GTK_DIALOG(dialog));
    switch (rc) {
    case GTK_RESPONSE_OK:
    case GTK_RESPONSE_CANCEL:
    default:
        break;
    }

    gtk_widget_hide (dialog);

__error:
    FREE_POINTER(g_dir_close, dir);
}

void on_toggle_renderer_click (GtkCellRendererToggle * cell, char * tree_path, gpointer data)
{
    g_print ("=====> Dingens clicked !!! <=====\n");
    GtkTreeModel *model = (GtkTreeModel *)data;
    GtkTreeIter  iter;
    GtkTreePath *path = gtk_tree_path_new_from_string (tree_path);
    gboolean fixed;

    /* get toggled iter */
    gtk_tree_model_get_iter (model, &iter, path);
    gtk_tree_model_get (model, &iter, TO_BE_DELETED, &fixed, -1);

    /* do something with the value */
    fixed ^= 1;

    /* set new value */
    gtk_list_store_set (GTK_LIST_STORE (model), &iter, TO_BE_DELETED, fixed, -1);

    /* clean up */
    gtk_tree_path_free (path);
}

这是XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
  <requires lib="gtk+" version="3.10"/>
  <object class="GtkImage" id="imageApply">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="stock">gtk-apply</property>
  </object>
  <object class="GtkImage" id="imageDelete">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="stock">gtk-delete</property>
  </object>
  <object class="GtkListStore" id="podcast_file_liststore">
    <columns>
      <!-- column-name TO_BE_DELETED -->
      <column type="gboolean"/>
      <!-- column-name FILENAME -->
      <column type="gchararray"/>
      <!-- column-name < -->
      <column type="&lt; Neue Spalte definieren &gt;"/>
    </columns>
  </object>
  <object class="GtkDialog" id="dialog_delete_files">
    <property name="width_request">320</property>
    <property name="height_request">300</property>
    <property name="can_focus">False</property>
    <property name="resizable">False</property>
    <property name="type_hint">dialog</property>
    <child internal-child="vbox">
      <object class="GtkBox" id="dialog-vbox1">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox" id="dialog-action_area1">
            <property name="can_focus">False</property>
            <property name="layout_style">end</property>
            <child>
              <object class="GtkButton" id="btnDelete">
                <property name="label" translatable="yes">Delete Selected</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="image">imageDelete</property>
                <property name="always_show_image">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="btnClose">
                <property name="label" translatable="yes">Close</property>
                <property name="name">btnClose</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="image">imageApply</property>
                <property name="always_show_image">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="pack_type">end</property>
            <property name="position">0</property>
          </packing>
        </child>
        <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="file_treeview">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="vexpand">True</property>
                <property name="model">podcast_file_liststore</property>
                <child internal-child="selection">
                  <object class="GtkTreeSelection" id="treeview-selection2"/>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="treeviewcolumn1">
                    <property name="title" translatable="yes">Delete</property>
                    <child>
                      <object class="GtkCellRendererToggle" id="cellrenderertoggle1">
                        <signal name="toggled" handler="on_toggle_renderer_click" object="podcast_file_liststore" swapped="no"/>
                      </object>
                      <attributes>
                        <attribute name="active">0</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="treeviewcolumn2">
                    <property name="title" translatable="yes">File name</property>
                    <child>
                      <object class="GtkCellRendererText" id="cellrenderertext1"/>
                      <attributes>
                        <attribute name="text">1</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
    <action-widgets>
      <action-widget response="-5">btnDelete</action-widget>
      <action-widget response="-6">btnClose</action-widget>
    </action-widgets>
  </object>
</interface>

再次:单击复选框时未调用onToggle-Event处理程序。这让我觉得单元格在某种程度上不活跃。

非常感谢每一个提示。 电贺 启

2 个答案:

答案 0 :(得分:0)

我通过使GtkCellRendererToggle'可激活'来实现它 - 即添加值为True的'activatable'属性。 - alexmurray

答案 1 :(得分:0)

我在搜索如何切换GtkTreeView中的复选框时发现了这个问题。 对于同一条船上的用户,在渲染的树视图中单击复选框不会自动切换该框,但会发出“已切换”信号。 将此信号连接到回调函数,并在该函数中执行切换行时应执行的所有功能。

.h1::before {
    content: "";
    position: absolute;
    border-radius: 50%;
    border: 10px solid black;
    left: 293px;
    top: 24px;
}