在 Qt4 中显示标准警告图标
如果 QLineEdit 包含无效数据,我会尝试在其旁边显示“警告”图标。
我试图使用QStyle::standardIcon(QStyle::SP_MessageBoxWarning)
来获取标准像素图并将其放置在QLabel
中,在某些情况下这似乎去工作。运行 Gnome 时,图标显示正确,但在 KDE 下运行时,不显示图标。我假设警告图标根本不包含在 KDE 下使用的样式中。
在 Qt 中显示“标准”警告图标的首选方式是什么?是否存在一些列表显示每种样式中包含哪些图标?如何从我知道包含警告图标的样式中获取图标?
I'm trying to display a "warning" icon next to a QLineEdit
if it contains invalid data.
I was trying to use QStyle::standardIcon(QStyle::SP_MessageBoxWarning)
to get a standard pixmap and place it inside a QLabel
, and in some cases this seems to work. When running Gnome the icon is displayed correctly, but when running under KDE no icon is shown. I assume that the warning icon is simply not included in the style used under KDE.
What is the preferred way to display a "standard" warning icon in Qt? Does there exist some list which shows which icons are included in every style? How can I get an icon from a style that I know includes the warning icon?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上次我遇到类似的问题时,我发现 这个 Qt 实验室讨论很有用。它告诉我 QIcon 现在(我相信从 4.6 开始)有一个
QIcon::fromTheme
函数允许您加载基于 Freedesktop.org 图标命名规范,此外还提供一个备用图标,以便在当前主题没有相关图标时使用。然后我所做的是包含一些非常基本的图标作为后备,并且通常仅通过其 Freedesktop 名称来指定图标。这几乎总是提供主题一致的外观,并且该程序在人们丢失图标的情况下仍然有效。
至于警告图标,我猜测/希望每个主题都必须有一个名为“dialog-warning”的图标,这就是您正在寻找的图标。
我希望这有帮助。
编辑:哦,如果您不知道,查看例如 Tango 图标集 来粗略了解 Freedesktop 名称对应的内容(尽管它当然取决于主题)。
The last time I had a similar problem, I found this Qt labs discussion useful. It informed me that QIcon now (since 4.6 I believe) has a
QIcon::fromTheme
function that allows you to load an icon based on the Freedesktop.org Icon Naming Specification, and in addition provide a fallback icon to be used if the current theme does not have the icon in question.What I did was then to include some very basic icons for use as fallback, and in general specify icons only by their Freedesktop names. This gave a theme-consistent look almost always, and the program still worked in cases where people were missing icons.
As for the warning icon, I'm guessing/hoping that every theme must have the one named "dialog-warning", and that it's what you're looking for.
I hope this helps.
Edit: Oh and, in case you don't know, it can be useful to look at for example the Tango icon set to get a rough idea of what the Freedesktop names correspond to (although it is of course theme-dependent).
Qt 确实捆绑了许多图像,这些图像是您可以在其中使用的资源你自己的代码。这些图像是通过
standardIcon()
提供的图像的超集。您可能需要验证特定图像是否包含在您所定位的 Qt 版本中。最终结果可能如下所示:
Qt does bundle a number of images that are resources that you can use in your own code. These images are a superset of those available via
standardIcon()
You may want to verify that the particular image is included in the versions of Qt you're targeting.The end result could look like the following:
对于任何想知道如何在 Windows 环境中执行此操作的人,您可以:
在自定义类中创建 qLabel,然后在该类的构造函数中创建具有所需样式的 QIcon,将其转换为像素图并使用QLabel::setPixmap() 函数将其应用于您创建的:
For anyone who wants to know how to do this in a Windows environment you can:
Create a qLabel in your custom class, and then in the constructor of that class create a QIcon with the style you want, convert it into a pixmap and use the QLabel::setPixmap() function to apply it to the one you created: