Faced with an incomprehensible problem. I read the documentation :

const gchar * gtk_entry_get_text (GtkEntry *entry); 

I see that the function accepts a GtkEntry pointer. Next, I write the form in the glide:

  ... <child> <object class="GtkEntry" id="txtMatrixSize"> <!-- ясно видно что это именно GtkEntry --> <property name="visible">True</property> <property name="can_focus">True</property> <property name="valign">center</property> <property name="max_length">3</property> <property name="max_width_chars">3</property> <property name="text" translatable="yes">45</property> <property name="shadow_type">none</property> <property name="primary_icon_activatable">False</property> <property name="secondary_icon_activatable">False</property> <property name="primary_icon_sensitive">False</property> <property name="secondary_icon_sensitive">False</property> </object> <packing> <property name="expand">False</property> <property name="fill">False</property> <property name="position">1</property> </packing> </child> ... 

further, MainForm.h

 #pragma once #include <gtk/gtk.h> #include <time.h> #include <cairo.h> #define UI_FILE "Forms/mainForm.glade" // Струкутура контроллов на форме typedef struct CControls { GtkEntry *txtMatrixSize; // тип указателя GtkEntry } Controls; // Структура формы typedef struct CMainForm { GtkBuilder *builder; GtkWidget *mainWindow; Controls controls; } MainForm; extern void OpenForm(int,char**,MainForm); 

The code of the simplest program with the installation of the text:

 #include "MainForm.h" MainForm form; void initControls(MainForm mainForm) { mainForm.controls.txtMatrixSize = GTK_ENTRY(gtk_builder_get_object(mainForm.builder, "txtMatrixSize")); // получаю Entry из разметки, пытаюсь привести его к указателю на GtkEntry } void OpenForm(int argc, char **argv, MainForm mainForm){ GError *error = NULL; gtk_init(&argc,&argv); mainForm.builder = gtk_builder_new(); if(!gtk_builder_add_from_file(mainForm.builder,UI_FILE,&error)) { g_warning( "%s", error->message ); g_free( error ); return; } mainForm.mainWindow = GTK_WIDGET(gtk_builder_get_object(mainForm.builder, "mainForm")); initControls(mainForm); gtk_entry_set_text(mainForm.controls.txtMatrixSize,"123"); // пытаюсь установить текст gtk_builder_connect_signals(mainForm.builder,NULL); g_object_unref(G_OBJECT(mainForm.builder)); gtk_widget_show(mainForm.mainWindow); gtk_main(); } 

Actually, everything is going well, then I get the following error:

(pathfinder: 2735): Gtk-CRITICAL **: gtk_entry_set_text: assertion 'GTK_IS_ENTRY (entry)' failed

A similar behavior ("object is not GtkWidget") occurs, for example, if I try to set the size of the widget

It feels like I didn’t correctly GTK_ENTRY, but I can’t find other examples. Tell me what I did wrong?

    1 answer 1

    You are working with the structure in the stack so you do not save the address on GtkEntry, it is more correct to work through pointers

     #include "MainForm.h" MainForm form; void initControls(MainForm * mainForm) { mainForm->controls->txtMatrixSize = GTK_ENTRY(gtk_builder_get_object(mainForm.builder, "txtMatrixSize")); // получаю Entry из разметки, пытаюсь привести его к указателю на GtkEntry } void OpenForm(int argc, char **argv, MainForm *mainForm){ GError *error = NULL; gtk_init(&argc,&argv); mainForm->builder = gtk_builder_new(); if(!gtk_builder_add_from_file(mainForm->builder,UI_FILE,&error)) { g_warning( "%s", error->message ); g_free( error ); return; } mainForm->mainWindow = GTK_WIDGET(gtk_builder_get_object(mainForm->builder, "mainForm")); initControls(mainForm); gtk_entry_set_text(mainForm->controls->txtMatrixSize,"123"); // пытаюсь установить текст gtk_builder_connect_signals(mainForm->builder,NULL); g_object_unref(G_OBJECT(mainForm->builder)); gtk_widget_show(mainForm->mainWindow); gtk_main(); } 

    Immediately did not notice the function call.

    • >> You are working with the structure in the stack << for sure, it was here that you decided to simplify, now I will recheck ... - test123
    • Indeed it is. Thank you so much! - test123
    • @ test123 I didn’t like the glade. I don’t use it, it’s not convenient when the object’s settings are in different files. - Yaroslav
    • While I did not meet any alternatives, gtk started learning a week ago, so there is no experience. - test123
    • After 3-4 windows you get used to the location methods in GTK and already the primary mapping is not needed. - Yaroslav