Gtk 微调器未出现

发布于 2024-09-10 23:38:13 字数 279 浏览 8 评论 0原文

我试图在计算正在进行时显示 Gtk::Spinner 对象,但似乎没有发生任何事情。代码片段看起来像......

{
    Gtk::Spinner spinner;
    spinner.start (); 
    // do some work...
    spinner.stop ();
}

我认为旋转器需要知道它出现在哪个对话上,但我看不到任何将其输入到对象中的方法。有谁知道我在哪里可以找到有效的示例?我可以在很多地方找到 Gtk 文档,但这没有多大帮助。

I'm trying to get a Gtk::Spinner object to display while calculations are in progress but nothing appears to be happening. The snippet of code looks like...

{
    Gtk::Spinner spinner;
    spinner.start (); 
    // do some work...
    spinner.stop ();
}

I'd have thought the spinner needed to know which dialogue it appears over but I can't see any way of feeding that into the object. Does anyone know where I could find a worked example? I can find the Gtk documentation in many places, but that isn't helping much.

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

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

发布评论

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

评论(3

作死小能手 2024-09-17 23:38:13

简而言之,如果屏幕上没有出现 Gtk::Spinner,这里有一个快速检查表:

  1. 确保安装了 librsvg
  2. 检查当前活动的 Gtk 主题是否有一个基本名称为“process-working-symbolic”的图像文件(例如 /path/to/your/theme/icons/scalable-up-to- 32/status/process-working-symbolic.svg)。
  3. 在您的应用程序代码中:确保将微调器对象添加到某些父小部件/容器中: vbox.pack_start(spinner, Gtk::PACK_SHRINK);
  4. 在您的应用程序代码中:确保所有小部件实际上都可见: show_all_children();
  5. 在您的应用代码中:最后,您需要调用 spinner.start() 以使小部件实际显示并在屏幕上旋转。

详细信息:

实际的 Gtk spinner 小部件代码(请参阅函数 gtk_spinner_class_init( ) in gtkspinner.c) 本身不进行任何绘图。事实上它的代码很少。它所做的只是向 gtk 小部件添加 CSS 代码:

  • 它将 CSS 类spinner”分配给小部件
  • ,并自动添加和删除 CSS 伪类每当小部件的布尔gtk属性活动”更改时,类检查”。

因此实际的外观(图标)和动画是由当前主题的 CSS 文件定义的。例如,在 Gtk 的默认主题的 gtk-contained.css 文件中,您会发现以下内容:

spinner {
  background: none;
  opacity: 0;
  -gtk-icon-source: -gtk-icontheme("process-working-symbolic");
};

这意味着它将自动搜索基本名称为“process-working-symbolic”的适当图像文件”在主题的“图标”目录树中。实际的旋转动画是在同一个 CSS 文件中定义的:

spinner:checked {
  opacity: 1;
  animation: spin 1s linear infinite;
};

在大多数其他主题中,旋转器小部件的这些 CSS 代码部分几乎是相同的。

现在这里有一个常见问题:在大多数主题中,旋转器图像是一个 SVG 文件(例如通常为“process-working-symbolic.svg”),但库存 Gdk pixbuf 加载程序不支持完全是SVG格式。它们支持各种其他图像文件格式,但要真正允许 Gdk/Gtk 加载 .svg 文件,您需要安装能够执行此操作的第三方 pixbuf 加载程序,例如 librsvg

替代方案:

如果您不能或不想安装支持 SVG 的 Gdk pixbuf 加载程序(如 librsvg),那么您也可以简单地添加具有相同基本名称的图像文件,但在另一种文件格式,例如“process-working-symbolic.png”到主题的图标目录树中。因此,要么绘制或下载一些旋转图片,然后缩放它,并将它们多次放置到主题的“图标”目录中列出的分辨率,例如:

/THEMEDIR/icons/8x8/status/process-working-symbolic.png
/THEMEDIR/icons/16x16/status/process-working-symbolic.png
/THEMEDIR/icons/22x22/status/process-working-symbolic.png
/THEMEDIR/icons/24x24/status/process-working-symbolic.png
/THEMEDIR/icons/32x32/status/process-working-symbolic.png
/THEMEDIR/icons/64x64/status/process-working-symbolic.png
/THEMEDIR/icons/256x256/status/process-working-symbolic.png
/THEMEDIR/icons/512x512/status/process-working-symbolic.png

您还应该知道:每当您执行spinner.start()旋转器图标立即出现在屏幕上,但即使在一台不错的机器上,旋转器通常也需要几乎一秒钟的时间才能开始其动画。

In short, here is a quick checklist if Gtk::Spinner is not appearing on screen:

  1. Make sure librsvg is installed.
  2. Check if your currently active Gtk theme has an image file with base name "process-working-symbolic" (e.g. /path/to/your/theme/icons/scalable-up-to-32/status/process-working-symbolic.svg).
  3. In your app code: Make sure you added the spinner object to some parent widget / container: vbox.pack_start(spinner, Gtk::PACK_SHRINK);
  4. In your app code: Ensure all widgets are actually made visible: show_all_children();
  5. In your app code: Finally you need to call spinner.start() for the widget to actually appear and spin on screen.

In detail:

The actual Gtk spinner widget code (see function gtk_spinner_class_init() in gtkspinner.c) is not doing any drawing on its own. In fact its code is very little. All it does is adding CSS code to the gtk widget:

  • it assigns the CSS class "spinner" to the widget
  • and it automatically adds and removes the CSS pseudo-class "checked" whenever the widget's boolean gtk property "active" changes.

So the actual look (icon) and the animation is defined by your current theme's CSS file. E.g. in Gtk's default theme's gtk-contained.css file you find the following:

spinner {
  background: none;
  opacity: 0;
  -gtk-icon-source: -gtk-icontheme("process-working-symbolic");
};

Which means it will automatically search for an appropriate image file with base name "process-working-symbolic" in the theme's "icons" directory tree. And the actual spinning animation is defined in the same CSS file by this:

spinner:checked {
  opacity: 1;
  animation: spin 1s linear infinite;
};

In most other themes these CSS code portions for the spinner widget are pretty much identical.

Now here is a common problem with this: in most themes the spinner image is an SVG file (e.g. typically "process-working-symbolic.svg"), but the stock Gdk pixbuf loaders do not support the SVG format at all. They support a variety of other image file formats, but for actually allowing Gdk/Gtk to load .svg files you need to install a third-party pixbuf loader capable to do so like librsvg.

Alternative:

In case you cannot or don't want to bother to install an SVG capable Gdk pixbuf loader like librsvg, then you can also simply add image file(s) with the same base name, but in another file format like "process-working-symbolic.png" to your theme's icons directory tree. So either draw or download some spinner picture then scale it and place them several times to the resolutions listed in your theme's "icons" directory, e.g.:

/THEMEDIR/icons/8x8/status/process-working-symbolic.png
/THEMEDIR/icons/16x16/status/process-working-symbolic.png
/THEMEDIR/icons/22x22/status/process-working-symbolic.png
/THEMEDIR/icons/24x24/status/process-working-symbolic.png
/THEMEDIR/icons/32x32/status/process-working-symbolic.png
/THEMEDIR/icons/64x64/status/process-working-symbolic.png
/THEMEDIR/icons/256x256/status/process-working-symbolic.png
/THEMEDIR/icons/512x512/status/process-working-symbolic.png

Also you should know: Whenever you do spinner.start() the spinner icon immediately appears on screen, but even on a decent machine it typically takes almost a second before the spinner starts its animation.

客…行舟 2024-09-17 23:38:13

您是否调用

spinner.show ();

并将其添加到某个窗口?

而且,你的计算可能会阻塞UI,因此不会更新。 打电话一次。

while (Gtk::Main::events_pending ())
    Gtk::Main::iteration ();

偶尔

Did you call

spinner.show ();

and add it to some window?

Moreover, your calculations may block the UI, so it is not updated. Call

while (Gtk::Main::events_pending ())
    Gtk::Main::iteration ();

once in a while.

苦笑流年记忆 2024-09-17 23:38:13

要将鼠标光标更改为“忙”,您可以执行以下操作:

Glib::RefPtr<Gdk::Window> window = dialog.get_window();
if(window) {
    window->set_cursor(Gdk::Cursor(Gdk::WATCH));
    Gdk::flush();
}

要将其更改回来,请执行

window->set_cursor();

以下操作。

免责声明:我通常使用 C 语言的 GTK 进行工作,并即时翻译了它......

To change the mouse cursor to "busy" you can do the following:

Glib::RefPtr<Gdk::Window> window = dialog.get_window();
if(window) {
    window->set_cursor(Gdk::Cursor(Gdk::WATCH));
    Gdk::flush();
}

To change it back, do

window->set_cursor();

instead.

Disclaimer: I usually work with GTK in C, and have translated this on the fly...

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