How to make a column with a string and pixbuf in GtkTreeview?

让人想犯罪 __ 提交于 2021-01-27 07:44:57

问题


I'm working in a app with Gtk+2 and i need to implement a File treeview.

the actual code it's:

public FileTree() {

    store = new TreeStore(2,typeof(string),typeof(string));

    this.change_dir( "/dir/path" );

    set_model( store );

    // File icon
    var pixbuf = new Gtk.CellRendererPixbuf();
    var column = new Gtk.TreeViewColumn();
    column.set_title("");
    column.pack_start(pixbuf, false);
    column.add_attribute(pixbuf,"stock-id",0);
    column.set_alignment(1.0f);
    append_column (column);

    // File name
    Gtk.CellRenderer cell = new Gtk.CellRendererText();
    insert_column_with_attributes(-1,"", cell, "text", 1);

    // Do some visual configs
    this.config();

}

and change_dir():

public void change_dir( string path ) {
        File repo_dir = File.new_for_path( path );

        try {
            generate_list( repo_dir, null, new Cancellable());
        } catch ( Error e ) {
            stderr.printf("Error: %s\n", e.message);
        }
    }

public void generate_list ( 
        File file, 
        TreeIter? parent = null, 
        Cancellable? cancellable = null 
    ) throws Error {

        // Enumerator
        FileEnumerator enumerator = file.enumerate_children (
            "standard::*",
            FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
            cancellable
        );
        FileInfo info = null;
        TreeIter iter;

        while(cancellable.is_cancelled() == false && ((info = enumerator.next_file(cancellable)) != null )) 
        {
            // Check if not it's in the omited files.
            if( ! (info.get_name() in IGNORED ) ) {

                // Check if is a dir or a file
                if( info.get_file_type() == FileType.DIRECTORY ) {

                    this.store.append( out iter, parent);
                    this.store.set(iter, 0, STOCK_DIRECTORY, 1, info.get_name());

                    File subdir = file.resolve_relative_path(info.get_name());

                    this.generate_list(subdir, iter, cancellable );
                } else {
                    // It's a file

                    this.store.append( out iter, parent);
                    this.store.set(iter, 0, STOCK_FILE, 1, info.get_name());
                }

            }
        }

        if ( cancellable.is_cancelled()) {
            throw new IOError.CANCELLED ("Operation was cancelled");
        }
    }

This it's showing two columns in ( first with a folder/file icon and the second one the name of the folder/file)

it's some way to do this in one single column??

EDIT: it could be some hack to set the icon at the side of the name, the actual code shows the icon and the string but when i expand a column, the strings moves a little to the right and there's a blank space between the icon and string.


回答1:


With the method of the TreeViewColumn, pack_start(), I Just append any cell renderer to the column.

(in C this is like http://developer.gnome.org/gtk/unstable/gtk-question-index.html (see 5.3))

so, just modified:

// File icon
var pixbuf = new Gtk.CellRendererPixbuf();
var column = new Gtk.TreeViewColumn();
column.set_title("");
column.pack_start(pixbuf, false);
column.add_attribute(pixbuf,"stock-id",0);
column.set_alignment(1.0f);
append_column (column);

// File name
Gtk.CellRenderer cell = new Gtk.CellRendererText();
insert_column_with_attributes(-1,"", cell, "text", 1);

with:

// File icon

var pixbuf = new Gtk.CellRendererPixbuf();
column.set_title("");
column.pack_start(pixbuf, false);
column.add_attribute(pixbuf,"stock-id",0);

// The name of the file.
var cell = new Gtk.CellRendererText();
column.pack_start(cell, false);
column.add_attribute(cell,"text",1);

append_column (column);

And there it is :)



来源:https://stackoverflow.com/questions/13648323/how-to-make-a-column-with-a-string-and-pixbuf-in-gtktreeview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!