我已经与vala做了一个应用,在某些时候我必须处理大量文件。我创建了一个窗口来选择文件夹,然后获取文件的路径并对它们进行一些处理。Vala强制刷新进度条
我添加了一个进度条到这个窗口来显示有多少个文件被处理,但由于某种原因,它始终保持为空。 代码约窗口:
this.files_window = new Gtk.Window();
this.files_window.window_position = Gtk.WindowPosition.CENTER;
this.files_window.destroy.connect (Gtk.main_quit);
// VBox:
Gtk.Box vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 5);
this.files_window.add (vbox);
// Buttons to open and close
Gtk.Button cancel = new Gtk.Button.with_label ("Cancel");
Gtk.Button select = new Gtk.Button.with_label ("Select");
vbox.add (select);
vbox.add (cancel);
// proogress bar
this.progress_bar = new Gtk.ProgressBar();
vbox.add(this.progress_bar);
// conect select to method do_stuff
select.clicked.connect (do_stuff);
this.files_window.show_all();
正如你所看到的,我连接按钮“选择”方法“do_stuff”从哪里获得所选文件的路径,并提出一些过程。
我更新correctlly的进步党栏的分数,因为我已经添加了一些版画知道,如果值是正确的,它是。只是这些窗户并不令人耳目一新,可能是因为它在处理文件的过程中所做的所有工作。这里是关于do_stuff()方法的代码:
// some proces to get paths of files in the list sfiles
double fraction = 0.0;
this.progress_bar.set_fraction (fraction);
int processed_files = 0;
foreach (string sfile in sfiles) {
do_some_proces_to_file(sfile);
processed_files += 1;
fraction = (double)processed_files/(double)sfiles.length;
this.progress_bar.set_fraction (fraction);
stdout.printf("Real fraction: %f\n", this.progress_bar.get_fraction());
}
的printf的表明PROGRES栏的值被更新,但在窗口杆总是空的。
我错过了什么吗?这是进行进度栏的正确方法吗?我应该做另一个线程来做这些东西吗?
谢谢!您的解决方案非常完美。正如你所说,我宁愿这样做,而不愿意使用线程。 – bcedu