gtk_text_view not showing up in a dialog box.

, ,

hello, i was trying to make a window wich 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);

You need to call gtk_widget_set_hexpand().

Thank you, its working fine.
i hope you have a great day!

is there a way to resize it? i wanted the text_view to be a little smaller not to be as big as the screen.

Put the GtkTextView widget inside a GtkScrolledWindow and then put the scrolled window inside the box.

Mark the TextView to expand both horizontally and vertically. Then you can decide how big the contents of the scrolled window should be before it starts to scroll.

hi, i tried this to resize the scrolled window but it seems that the resizing of the height is not working at all!

this is what i did:


void createTask(qic::DataBase *dataBase) {
    GtkWidget *dialog = gtk_dialog_new();
    GtkWidget *box              = gtk_box_new(GtkOrientation::GTK_ORIENTATION_HORIZONTAL,60);
    GtkWidget *element;
    GtkWidget *scrolledWindow   = gtk_scrolled_window_new();

    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_widget_set_hexpand_set(element, true);
    gtk_widget_set_vexpand_set(element, true);
    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_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolledWindow),element);
    gtk_scrolled_window_set_max_content_height(GTK_SCROLLED_WINDOW(scrolledWindow),10);
    gtk_box_append(GTK_BOX(box),scrolledWindow);

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

this is what i see:
image

i would like to have just a small rectangle but it seems that im not able to resize it.

I’m not entirely clear on what your goal for the layout is (will there be something below the TextView?), but I believe what’s happening is that your UI only has a single ‘row’, so GTK is giving it all available space. The same thing happens when you put it directly in a Window, in both directions.

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.