Search code examples
c++gtk

gtk_text_view not showing up in a dialog box


i was trying to make a window whic shows up whenever the user clicks a button. the window itself does show up but the text view inside of it does not! anyone knows a fix for this?

code:

GtkWidget *dialog = gtk_dialog_new();
GtkWidget *box              = gtk_box_new(GtkOrientation::GTK_ORIENTATION_HORIZONTAL,60);
GtkWidget *element;

gtk_window_set_title(GTK_WINDOW(dialog),"new task - Hb Tasks");
gtk_window_set_default_size(GTK_WINDOW(dialog),300,200);
gtk_window_set_child(GTK_WINDOW(dialog),box);
gtk_window_set_resizable(GTK_WINDOW(dialog), false);

element = gtk_text_view_new();
gtk_box_append(GTK_BOX(box),element);
char text[22] = "insert the task name.";
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(element));
gtk_text_buffer_set_text(buffer,text, strlen(text));

gtk_window_present(GTK_WINDOW(dialog));
loadTasks(dataBase);

i've read the documentation for almost 2 hours and i still did not find a way out.

this is how i see the window:

i also tried this example here: https://docs.gtk.org/gtk4/section-text-widget.html


Solution

  • I suspect that the problem is within the scope of the objects and variables. There are already some articles on this here, such as:

    How can I get user input from an entry? GTK 4 C /

    unable to add label to box by the click of a button gtk4 in C /

    Get form entered values on gtk C /

    Since I only work with the C programming language with GTK4, here is an example in this programming language of how to solve the problem.

    #include <gtk/gtk.h>
    
    static void show_window(GtkWidget *button)
    {
        // create child window with textview
        GtkWidget *window2 = gtk_window_new();
        gtk_window_set_title(GTK_WINDOW(window2), "Child Window");
        gtk_window_set_default_size(GTK_WINDOW(window2), 200, 150);
        GtkWidget *element = gtk_text_view_new();
        const char text[22] = "insert the task name.";
        GtkTextBuffer *buffer = Gtk_text_view_get_buffer(GTK_TEXT_VIEW(element));
        gtk_text_buffer_set_text(buffer,text, strlen(text));
        gtk_window_set_child(GTK_WINDOW(window2),GTK_WIDGET(element));
        gtk_window_set_modal(GTK_WINDOW(window2),TRUE);
        gtk_widget_set_visible(window2,TRUE);
    }
    
    static void activate(GtkApplication* app)
    {
        GtkWidget *button1;
        GtkWidget *window1;
    
        // create app window
        window1 = gtk_application_window_new(app);
        gtk_window_set_title(GTK_WINDOW(window1), "APP-Window");
        gtk_window_set_default_size(GTK_WINDOW(window1), 400, 300);
    
        button1 = gtk_button_new_with_label("click me");
    
        gtk_window_set_child(GTK_WINDOW(window1),button1);
        g_signal_connect(button1,"clicked",G_CALLBACK(show_window),NULL);
        gtk_application_add_window(app, GTK_WINDOW(window1));
    
        gtk_widget_set_visible(window1,TRUE);
    
    }
    
    int main(int argc, char* argv[])
    {
        GtkApplication* app;
        app = gtk_application_new("org.gtk.childwindow", G_APPLICATION_DEFAULT_FLAGS);
        g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
        g_application_run(G_APPLICATION(app), argc, argv);
        g_object_unref(app);
    
        return 0;
    }
    

    It shouldn't be hard to translate this into C++.