在 Qt4 中显示标准警告图标

发布于 2024-10-07 23:43:45 字数 304 浏览 0 评论 0原文

如果 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 技术交流群。

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

发布评论

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

评论(3

花辞树 2024-10-14 23:43:45

上次我遇到类似的问题时,我发现 这个 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).

四叶草在未来唯美盛开 2024-10-14 23:43:45

Qt 确实捆绑了许多图像,这些图像是您可以在其中使用的资源你自己的代码。这些图像是通过 standardIcon() 提供的图像的超集。您可能需要验证特定图像是否包含在您所定位的 Qt 版本中。

最终结果可能如下所示:

QPixmap pixmap(":/trolltech/styles/commonstyle/images/up-128.png");
// use pixmap as needed

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:

QPixmap pixmap(":/trolltech/styles/commonstyle/images/up-128.png");
// use pixmap as needed
不语却知心 2024-10-14 23:43:45

对于任何想知道如何在 Windows 环境中执行此操作的人,您可以:

在自定义类中创建 qLabel,然后在该类的构造函数中创建具有所需样式的 QIcon,将其转换为像素图并使用QLabel::setPixmap() 函数将其应用于您创建的:

QIcon icon = style()->standardIcon(QStyle::SP_MessageBoxWarning); //or 
//whatever icon you choose
QPixmap pixmap = icon.pixmap(QSize(60, 60));
ui->iconLabel->setPixmap(pixmap);

ui->iconLabel->setScaledContents(true); //you can set this to fill the 
//dimensions of your qLabel if you wish.

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:

QIcon icon = style()->standardIcon(QStyle::SP_MessageBoxWarning); //or 
//whatever icon you choose
QPixmap pixmap = icon.pixmap(QSize(60, 60));
ui->iconLabel->setPixmap(pixmap);

ui->iconLabel->setScaledContents(true); //you can set this to fill the 
//dimensions of your qLabel if you wish.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文