如何在不锁定应用程序的情况下使用大型数据集填充 Gtk::TreeModelColumn

发布于 2024-09-26 10:48:03 字数 182 浏览 0 评论 0原文

我需要将一个大的(也许不是那么多 - 几千个条目)数据集填充到 Gtk::TreeModelColumn 中。如何在不锁定应用程序的情况下做到这一点?将处理放入单独的线程中安全吗?那么我必须用锁保护应用程序的哪些部分?它只是 Gtk::TreemodelColumn 类,还是它所在的 Gtk::TreeView 小部件,甚至可能是周围的框架或窗口?

I need to fill in a large (maybe not so much - several thousands of entries) dataset to a Gtk::TreeModelColumn. How do I do that without locking up the application. Is it safe to put the processing into separate thread? What parts of the application do I have to protect with a lock then? Is it only the Gtk::TreemodelColumn class, or Gtk::TreeView widget it is placed in, or maybe even surrounding frame or window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2024-10-03 10:48:03

您可以采取两种一般方法。 (免责声明:我试图提供示例代码,但我很少使用 gtkmm - 我对 C 语言中的 GTK 更熟悉。但是原理保持不变。)

一种是使用 空闲函数 - 当 GUI 中没有发生任何事情时运行。为了获得最佳结果,请在空闲函数中进行少量计算,例如将一项添加到树视图中。如果您从空闲函数返回true,那么每当有更多可用处理时间时就会​​再次调用它。如果返回false,则不会再次调用。空闲函数的好处是您不必锁定任何内容。因此,您可以像这样定义空闲函数:

bool fill_column(Gtk::TreeModelColumn* column)
{
    // add an item to column
    return !column_is_full();
}

然后像这样启动进程:

Glib::signal_idle().connect(sigc::bind(&fill_column, column));

另一种方法是使用线程。在 C API 中,这将涉及到 gdk_threads_enter() 等,但我认为在 gtkmm 中执行此操作的正确方法是使用 Glib::Dispatcher。我以前没有使用过它,但是这里是一个例子。但是,您仍然可以将 C API 与 gtkmm 结合使用,如 这里

There are two general approaches you could take. (Disclaimer: I've tried to provide example code, but I rarely use gtkmm - I'm much more familiar with GTK in C. The principles remain the same, however.)

One is to use an idle function - that runs whenever nothing's happening in your GUI. For best results, do a small amount of calculation in the idle function, like adding one item to your treeview. If you return true from the idle function, then it is called again whenever there is more processing time available. If you return false, then it is not called again. The good part about idle functions is that you don't have to lock anything. So you can define your idle function like this:

bool fill_column(Gtk::TreeModelColumn* column)
{
    // add an item to column
    return !column_is_full();
}

Then start the process like this:

Glib::signal_idle().connect(sigc::bind(&fill_column, column));

The other approach is to use threads. In the C API, this would involve gdk_threads_enter() and friends, but I gather that the proper way to do that in gtkmm, is to use Glib::Dispatcher. I haven't used it before, but here is an example of it. However, you can also still use the C API with gtkmm, as pointed out here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文