Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

gtk - TreeView or ListView without column title bar

Is there a way to show data in TreeView or ListView without column title bar?

question from:https://stackoverflow.com/questions/66059313/treeview-or-listview-without-column-title-bar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you talk about column title bar without showing any code makes me think that you are asking about Header.

If so, then this is the function you need:

gtk_tree_view_set_headers_visible()

Here is with headers_visible set to TRUE (which is default):

enter image description here

And here is with headers_visible set to FALSE:

enter image description here

And here is an example Code:

#include <gtk/gtk.h>

GtkWidget    *create_window ( void );
GtkWidget    *create_scrolled_window ( void );
GtkWidget    *create_tree_view ( void );
GtkListStore *create_list_store ( void );

int main ( void )
{
    GtkWidget *window;
    GtkWidget *scrolled_window;
    GtkWidget *tree_view;

    /// ***
    gtk_init ( NULL, NULL );

    /// ***
    window = create_window();

    /// ***
    ///scrolled_window = gtk_scrolled_window_new ( NULL, NULL );
    scrolled_window = create_scrolled_window ( );
    gtk_scrolled_window_set_kinetic_scrolling ( GTK_SCROLLED_WINDOW ( scrolled_window ), TRUE );
    gtk_scrolled_window_set_capture_button_press ( GTK_SCROLLED_WINDOW ( scrolled_window ), TRUE );

    /// ***
    tree_view = create_tree_view ( );
    gtk_container_add ( GTK_CONTAINER ( scrolled_window ), tree_view );
    gtk_container_add ( GTK_CONTAINER ( window ), scrolled_window );

    /// ***
    gtk_widget_show_all ( window );

    // ***
    gtk_widget_show_all ( window );
    gtk_main ();
}

GtkWidget *create_window ( void )
{
    GtkWidget *window;

    /// ***
    window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
    gtk_window_set_default_size ( GTK_WINDOW ( window ), 150, 150 );
    gtk_container_set_border_width ( GTK_CONTAINER ( window ), 5 );

    /// ***
    g_signal_connect ( window, "delete_event", G_CALLBACK ( gtk_main_quit ), NULL );

    /// ***
    return window;
}

GtkWidget *create_scrolled_window ( void )
{
    GtkWidget *scrolled_window;

    /// ***
    scrolled_window = gtk_scrolled_window_new ( NULL, NULL );
    gtk_scrolled_window_set_kinetic_scrolling ( GTK_SCROLLED_WINDOW ( scrolled_window ), TRUE );
    gtk_scrolled_window_set_capture_button_press ( GTK_SCROLLED_WINDOW ( scrolled_window ), TRUE );

    /// ***
    return scrolled_window;
}

GtkListStore *create_list_store ( void )
{
    GtkListStore *list_store;
    list_store = gtk_list_store_new ( 1, G_TYPE_STRING );

    /// ***
    for ( gint i = 0; i < 10; i++ )
    {
        GtkTreeIter iter;
        gchar *label = g_strdup_printf ( "Row number %d", i );

        /// ***
        gtk_list_store_append ( list_store, &iter );
        gtk_list_store_set ( list_store, &iter, 0, label, -1 );

        /// ***
        g_free ( label );
    }

    return list_store;
}

GtkWidget *create_tree_view ( void )
{
    GtkWidget       *tree_view;
    GtkCellRenderer *renderer;
    GtkListStore    *store;

    /// ***
    tree_view = gtk_tree_view_new ();
    store     = create_list_store();

    /// ***
    renderer = gtk_cell_renderer_text_new ();
    g_object_set ( renderer, "editable", TRUE, NULL );

    /// **
    gtk_tree_view_insert_column_with_attributes ( GTK_TREE_VIEW ( tree_view ),
            0, "This is the Title",
            renderer,
            "text", 0,
            NULL );

    /// ***
    gtk_tree_view_set_model ( GTK_TREE_VIEW ( tree_view ), GTK_TREE_MODEL ( store ) );
    g_object_unref ( store );

    /// ***
    gtk_tree_view_set_headers_visible ( GTK_TREE_VIEW ( tree_view ), FALSE );

    /// ***
    return tree_view;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...