Glade-GTK + C语言:如何使用FileChooserButton获取文件名?

时间:2013-01-03 15:43:31

标签: c user-interface gtk glade

我是这个林间空地的新手(也是这个论坛的新手),想知道你们中的任何人是否可以帮助我:

我必须获取用户从chooserbutton中选择的文件名,并将其作为字符串发送给函数。不幸的是,我一直在收到这个恼人的错误:

  

Gtk-CRITICAL **:IA__gtk_file_chooser_get_uri:断言`GTK_IS_FILE_CHOOSER(选择器)'失败

我认为这是由于小部件的错误使用造成的。有人可以帮我解决这个问题吗?源代码如下所示,正如您所见,它来自https://live.gnome.org/Glade/Tutorials处的Glade-GTK教程。

该程序是一个简单的2按钮窗口:第一个按钮是filechooser按钮,第二个按钮是单击时调用“clica”功能的标准按钮。它应该显示用户通过文件选择器按钮选择的文件名,但这是错误发生的时间。

测试源(只是为了弄清楚如何使用小部件):

#include <gtk/gtk.h>

int
main( int    argc,
      char **argv )
{
    GtkBuilder *builder;
    GtkWidget  *window;
    GError     *error = NULL;

    /* Init GTK+ */
    gtk_init( &argc, &argv );

    /* Create new GtkBuilder object */
    builder = gtk_builder_new();
    /* Load UI from file. If error occurs, report it and quit application.
     * Replace "tut.glade" with your saved project. */
    if( ! gtk_builder_add_from_file( builder, "tut.glade", &error ) )
    {
        g_warning( "%s", error->message );
        g_free( error );
        return( 1 );
    }

    /* Get main window pointer from UI */
    window = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) );

    /* Connect signals */
    gtk_builder_connect_signals( builder, NULL );

    /* Destroy builder, since we don't need it anymore */
    g_object_unref( G_OBJECT( builder ) );

    /* Show window. All other widgets are automatically shown by GtkBuilder */
    gtk_widget_show( window );

    /* Start main loop */
    gtk_main();

    return( 0 );
}

void clica(GtkFileChooser *filechooserbutton1){//this button was inserted through Glade
   char cNome[250];
      *cNome = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(filechooserbutton1));

      printf("\n%s", cNome);

}

Glade文件:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.24"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkHBox" id="hbox1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkFileChooserButton" id="filechooserbutton1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_action_appearance">False</property>
            <signal name="clicked" handler="clica" swapped="no"/>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

1 个答案:

答案 0 :(得分:1)

04/01/2013更新(现在我们有林间空地文件)

我们现在可以看到您使用了错误对象的信号。你连接了你的&#34; clica&#34;回调&#34;点击&#34; GtkButton&#34; button1&#34;的信号,而不是将其连接到GtkFileChooserButton&#34; filechooserbutton1&#34;。

原始回答: 没有林间空地文件,我们无法看到您连接的信号,而不是它们连接的信号。但是你有可能不遵循信号的原型。如果断言失败,那意味着在你的回调中,你认为第一个参数是GtkFileChooser是另一回事。