我正在用C ++和GTK 3创建一个小程序(只是学习它),对此我遇到了麻烦。因此,我隔离了有问题的部分。

可以通过单击按钮来获取并打印该条目。

这是代码:

#include <iostream>
#include <gtk/gtk.h>

using namespace std;

GtkWidget *wventana;
GtkWidget *wgrid;

//FUNCS

void ventana(string titulo, int margen)
{
    const char * tituloc = titulo.c_str();

    wventana = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position (GTK_WINDOW (wventana), GTK_WIN_POS_CENTER);
    gtk_window_set_title (GTK_WINDOW (wventana), tituloc);
    gtk_container_set_border_width(GTK_CONTAINER(wventana), margen);
}

void grid()
{
    wgrid = gtk_grid_new();
    gtk_container_add(GTK_CONTAINER(wventana), wgrid);
    gtk_grid_set_row_spacing (GTK_GRID (wgrid), 10);
    gtk_grid_set_column_spacing (GTK_GRID (wgrid), 25);
}

void boton_clic (GtkEntry *wentrada, gpointer user_data)
{
    const char *nombre;
    nombre = gtk_entry_get_text(wentrada);
    cout << nombre << endl;
}

void entrada(int x, int y, int lx, int ly)
{
    GtkWidget *wentrada;
    wentrada = gtk_entry_new();
    gtk_grid_attach (GTK_GRID (wgrid), wentrada, x, y, lx, ly);
}

void boton(string texto, int x, int y, int lx, int ly)
{
    const char * wtexto = texto.c_str();

    GtkWidget *wboton;
    wboton = gtk_button_new_with_label (wtexto);
    gtk_grid_attach (GTK_GRID (wgrid), wboton, x, y, lx, ly);

    g_signal_connect (GTK_BUTTON (wboton), "clicked", G_CALLBACK (boton_clic), G_OBJECT     (wventana));
}

//MAIN

int main(int argc, char *argv[])
{
    gtk_init (&argc, &argv);

    ventana("ventana",10);
    grid();
    entrada(1, 1, 1, 1);
    boton("Aprietame", 1, 2, 1, 1);

    gtk_widget_show_all (wventana);
    gtk_main ();

    return 0;
}


它没有得到该条目,并显示以下错误消息:

(boton:6669): Gtk-CRITICAL **: gtk_entry_get_text: assertion 'GTK_IS_ENTRY (entry)' failed


谁能说出问题所在吗?而且,我该如何使其工作?
或者在任何情况下,有什么好的替代方法? (必须具有单独的功能)

提前致谢。



感谢您的答复@ptomato,我尝试用您建议的更改代替新代码:

#include <iostream>
#include <gtk/gtk.h>

using namespace std;

GtkWidget *wventana;
GtkWidget *wgrid;

//FUNCS

void ventana(string titulo, int margen)
{
    const char * tituloc = titulo.c_str();

    wventana = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position (GTK_WINDOW (wventana), GTK_WIN_POS_CENTER);
    gtk_window_set_title (GTK_WINDOW (wventana), tituloc);
    gtk_container_set_border_width(GTK_CONTAINER(wventana), margen);
}

void grid()
{
    wgrid = gtk_grid_new();
    gtk_container_add(GTK_CONTAINER(wventana), wgrid);
    gtk_grid_set_row_spacing (GTK_GRID (wgrid), 10);
    gtk_grid_set_column_spacing (GTK_GRID (wgrid), 25);
}

void boton_clic (GtkButton *wboton, GtkEntry *wentrada)
{
    const char *nombre;
    nombre = gtk_entry_get_text (wentrada);
    cout << nombre << endl;
}

void entrada(int x, int y, int lx, int ly)
{
    GtkWidget *wentrada;
    wentrada = gtk_entry_new();
    gtk_grid_attach (GTK_GRID (wgrid), wentrada, x, y, lx, ly);
}

void boton(string texto, int x, int y, int lx, int ly)
{
    const char * wtexto = texto.c_str();

    GtkWidget *wboton;
    wboton = gtk_button_new_with_label (wtexto);
    gtk_grid_attach (GTK_GRID (wgrid), wboton, x, y, lx, ly);

    g_signal_connect (wboton, "clicked", G_CALLBACK (boton_clic), wentrada);
}

//MAIN

int main(int argc, char *argv[])
{
    gtk_init (&argc, &argv);

    ventana("ventana",10);
    grid();
    entrada(1, 1, 1, 1);
    boton("Aprietame", 1, 2, 1, 1);

    gtk_widget_show_all (wventana);
    gtk_main ();

    return 0;
}


但是当我尝试编译它时,它显示:

In file included from /usr/include/glib-2.0/gobject/gobject.h:28:0,
                 from /usr/include/glib-2.0/gobject/gbinding.h:29,
                 from /usr/include/glib-2.0/glib-object.h:23,
                 from /usr/include/glib-2.0/gio/gioenums.h:28,
                 from /usr/include/glib-2.0/gio/giotypes.h:28,
                 from /usr/include/glib-2.0/gio/gio.h:26,
                 from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
                 from /usr/include/gtk-3.0/gdk/gdk.h:32,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from boton.cpp:2:
boton.cpp: In function ‘void boton(std::string, int, int, int, int)’:
boton.cpp:51:64: error: ‘wentrada’ was not declared in this scope
  g_signal_connect (wboton, "clicked", G_CALLBACK (boton_clic), wentrada);
                                                                ^
/usr/include/glib-2.0/gobject/gsignal.h:472:73: note: in definition of macro     ‘g_signal_connect’
     g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL,     (GConnectFlags) 0)
                                                                             ^
 Compilation failed.


怎么了?我该怎么办???

您能再解释一下第二个论点吗? (英语不是我的母语,这部分对我来说有点困惑:P)

再次感谢。

最佳答案

boton_clic信号处理程序具有两个参数:GtkWidget *和指向您指定的某些用户数据的指针。第一个参数始终是发送信号的对象,因此为wboton;但是您标记了参数wentrada并将其视为GtkEntry *,因此出现了错误消息。

第二个参数是您在连接信号时指定的用户数据。在您的g_signal_connect()呼叫中,您在此处传递wventana。但是,如果要访问信号处理程序中的条目,则应改为传递wentrada

因此,它应如下所示:

void boton_clic (GtkButton *wboton, GtkEntry *wentrada)
{
    const char *nombre;
    nombre = gtk_entry_get_text (wentrada);
    cout << nombre << endl;
}

...

g_signal_connect (wboton, "clicked", G_CALLBACK (boton_clic), wentrada);

09-26 09:39