隐藏图像在 Gtk::Button 中的方法

huangapple go评论68阅读模式
英文:

How to hide an image in a Gtk::Button

问题

以下是翻译好的内容:

我继承了一段执行以下操作的代码:

if (ok){
    m_Test_Button.set_image(m_Image_OK);
    m_Test_Button.set_image_position(Gtk::POS_RIGHT);
    m_Test_Button.set_always_show_image(true);
}else{
    m_Test_Button.set_image(m_Image_not_OK);
    m_Test_Button.set_image_position(Gtk::POS_RIGHT);
    m_Test_Button.set_always_show_image(true);
}

在某些时候,之前编写该代码的程序员尝试使用 m_Test_Button.set_always_show_image(false); 来隐藏图像。然而,这并不能真正隐藏图像;当应该停止显示图像时,图像仍然可见,这可能会导致用户在更改参数时感到困惑,因为先前测试的状态在更改之前仍然可见。

我猜我可以通过在其位置使用一个空白图像来隐藏它,但也有不这样做的理由(比如对齐考虑以及它看起来像是一个权宜之计而不是一个解决方案)。

Gtk::Button帮助文档 甚至没有提到 set_always_show_image() 或与图像相关的几乎任何内容(除了图标路径)。我在网上找到的唯一文档似乎表明,正如名称所示,此函数可能不会隐藏图像。

所以我对如何正确隐藏图像一无所知。我之前没有使用 Gtkmm 的经验。

更新:根本问题在于,部分是因为我对 Gtkmm 不熟悉,我查看的文档与我正在使用的 Gtkmm 版本不同。感谢评论者提醒我注意这一点。

英文:

I've inherited a code that does this:

if (ok){
    m_Test_Button.set_image(m_Image_OK);
    m_Test_Button.set_image_position(Gtk::POS_RIGHT);
    m_Test_Button.set_always_show_image(true);
}else{
    m_Test_Button.set_image(m_Image_not_OK);
    m_Test_Button.set_image_position(Gtk::POS_RIGHT);
    m_Test_Button.set_always_show_image(true);
}

At certain points, the previous programmer who wrote that code attempted to use m_Test_Button.set_always_show_image(false); to hide the image. However, that does not really work to hide the image; it remains visible when it should stop being visible, which is a potential cause of confusion for users when changing parameters because the previous test's status prior to the change still remains visible.

I guess I can hide it just by using a blank image in its place, but there are reasons not to do it (like alignment considerations, and the fact that it looks like a kludge rather than a solution).

The Gtk::Button help does not even mention set_always_show_image() or almost anything at all related to images (other than an icon path). The only documentation I've found online seems to indicate that as the name implies, this function might not hide the image.

So I'm in the blue as for what to do to properly hide the image. I've no previous experience with Gtkmm.

Update: The underlying problem was that, partly due to my inexperience with Gtkmm, I was checking the docs for a different version of Gtkmm to the one I was using. Thanks to the commenters for making me aware of this.

答案1

得分: 1

set_always_show_image() 似乎会强制按钮显示图像,即使默认情况下,Gtkmm 会将其隐藏(因为这可能看起来很奇怪,例如)。根据文档

> 通过将 nullptr 分配给 property_image() 可以取消图像设置。

就文档而言,我以前也遇到过与您相同的问题。您可以查看此页面,其中包含所有版本的API文档。您还可以查看此答案,在其中我提供了多种查找 Gtkmm 信息的方法,这些方法是我多年来找到的。

英文:

The set_always_show_image() seems to force the button to show the image, even if, by default, Gtkmm would have hidden it (because it would look weird, for example). According to the docs:

> The image can be unset by assigning nullptr to property_image().

As far as documentations goes, I have had the same problem with versions as you in the past. You can look at this page, which will contain the API documentation for all versions. You can also look at this answer, in which I provide a couple different ways to find information on Gtkmm I have found over the years.

huangapple
  • 本文由 发表于 2023年6月26日 19:40:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定