2

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

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

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

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

该程序是一个简单的 2 按钮窗口:第一个按钮是文件选择器按钮,第二个是标准按钮,单击时调用“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);

}

格莱德文件:

<?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>
4

1 回答 1

1

2013 年 4 月 1 日更新(现在我们有了林间空地文件)

我们现在可以看到您使用了错误对象的信号。您将“clica”回调连接到 GtkButton“button1”的“clicked”信号,而不是将其连接到 GtkFileChooserButton“filechooserbutton1”。

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

于 2013-01-03T17:41:19.913 回答