2012-05-31 22 views
0

我創建了這個類,爲了提高效率,我想將縮略圖加載到iconview中作爲另一個線程,因爲如果我在同一個線程中執行,gui的加載速度非常慢。但是,當我創建線程時,它不起作用,它會繪製一些縮略圖,然後消失。當我使用連接時,它工作。這是我的代碼:如何在gtk中加載一個小部件作爲不同的線程? (vala)

public class FotoThumbnailPane : Gtk.ScrolledWindow{ 

private FotoThumbnailPane_i pane; 
private string namet; 

public FotoThumbnailPane(string name){ 
    this.namet = name; 
} 

public void set_imagelist(fileutils.ImageList image_list){ 
    pane = new FotoThumbnailPane_i(image_list); 
    this.add (pane); 
    this.set_min_content_width(140); 
    this.show_all(); 
} 

    //This is my threaded function 
    public void* load_thumbs(){ 

    pane.set_visible(false); 
    pane.newmodel = new Gtk.ListStore (2, typeof (Gdk.Pixbuf), typeof (string)); 
    pane.set_selection_mode (Gtk.SelectionMode.SINGLE); 
    pane.set_pixbuf_column (0); 
    pane.set_model(pane.newmodel); 

    string icon_style = """ 
      .thumbnail-view { 
       background-color: #FFFFFF; 
      } 
      .thumbnail-view:selected { 
       background-color: #9D9D9D; 
       border-color: shade (mix (rgb (34, 255, 120), #fff, 0.5), 0.9); 
      } 
     """; 

    var icon_view_style = new Gtk.CssProvider(); 

     try { 
      icon_view_style.load_from_data (icon_style, -1); 
     } catch (Error e) { 
      warning (e.message); 
     } 
     pane.get_style_context().add_class ("thumbnail-view"); 
    pane.get_style_context().add_provider (icon_view_style, Gtk.STYLE_PROVIDER_PRIORITY_THEME); 

    //Add thumbnails to the iconview 
    string buff; 
    for(int i=1; i<pane.image_list.size; i++){ 
    buff = pane.image_list.get_full_filename(i); 
    stdout.printf("Added %s to thumbnail\n", buff); 
      var image = new Gdk.Pixbuf.from_file_at_scale(buff, 110, 80, false); 
      // Add the wallpaper name and thumbnail to the IconView 
      Gtk.TreeIter root; 
      pane.newmodel.append(out root); 
      pane.newmodel.set(root, 0, image, -1); 
      pane.newmodel.set(root, 1, pane.image_list.get_filename(i), -1); 

      // Select the thumbnail if it is the first in list 
      if (i==0) { 
       pane.select_path (pane.newmodel.get_path (root)); 
      }  
      pane.iters.append (root); 
    } 
    pane.set_sensitive(true); 
    this.queue_draw(); 
return null; 
} 

}

+0

這將是更容易幫助,如果你發佈一個最小的,自包含的測試用例(即,什麼人可以編譯和執行)。 – nemequ

+0

@nemequ謝謝,我試圖做到這一點,但無法找到方法...我一般都很喜歡異步的東西... 這是我的痛苦嘗試代碼 http:// pastebin.com/06Wz6Yfq – angrymadcat

+0

@nemequ哇!非常感謝你,我認爲這將是非常有用的! – angrymadcat

回答

3

你實際上並不需要處理程序中的線程 - 你可以只使用一個asynchronous method加載內容。具體而言,Gdk.Pixbuf.new_from_stream_at_scale_async

下面是一個例子:

public async void load (Gtk.Image img, string filename) { 
    GLib.File file = GLib.File.new_for_commandline_arg (filename); 
    try { 
    GLib.InputStream stream = yield file.read_async(); 
    Gdk.Pixbuf pixbuf = yield Gdk.Pixbuf.new_from_stream_at_scale_async (stream, 320, -1, true); 
    img.set_from_pixbuf (pixbuf); 
    } catch (GLib.Error e) { 
    GLib.error (e.message); 
    } 
} 

private static int main (string[] args) { 
    GLib.return_val_if_fail (args.length > 1, -1); 

    Gtk.init (ref args); 

    Gtk.Window win = new Gtk.Window(); 
    win.destroy.connect (() => { 
     Gtk.main_quit(); 
    }); 

    Gtk.Image image = new Gtk.Image(); 
    win.add (image); 

    load.begin (image, args[1], (obj, async_res) => { 
     GLib.debug ("Finished loading."); 
    }); 

    win.show_all(); 

    Gtk.main(); 

    return 0; 
} 
相關問題