gpt4 book ai didi

gtk - 如何在 GtkTreeview 中创建带有字符串和 pixbuf 的列?

转载 作者:行者123 更新时间:2023-12-04 20:12:22 31 4
gpt4 key购买 nike

我正在使用 Gtk+2 的应用程序,我需要实现一个文件树 View 。

实际代码是:

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();

}

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");
}
}

它显示了两列(第一个是文件夹/文件图标,第二个是文件夹/文件的名称)

有办法在一列中做到这一点吗??

编辑:在名称的一侧设置图标可能是一些技巧,实际代码显示图标和字符串但是当我展开一列时,字符串向右移动一点,并且图标之间有一个空格和字符串。

最佳答案

使用 TreeViewColumn 的方法,pack_start() ,我只是将任何单元格渲染器附加到列中。

(在 C 中,这就像 http://developer.gnome.org/gtk/unstable/gtk-question-index.html(见 5.3))

所以,刚刚修改:

// 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);

和:

// 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);

它就在那里:)

关于gtk - 如何在 GtkTreeview 中创建带有字符串和 pixbuf 的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13648323/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com