Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do "theme" icons work in Qt Creator Designer?

Tags:

When I create the "Command link button" (QCommandLinkButton) it has relatively nice green arrow icon.

image description

I would like to see what other nice icons can I choose. When I try to change the icon, [Theme] appears instead of path or some GUI selection dialog:

image description

I also noticed the context menu:

image description

When I click Set icon from theme, again expecting some GUI selection list, I get just a text field:

image description

What I was imagining:

image description

Where's the list of icons from which the green arrow was taken?

like image 488
Tomáš Zato - Reinstate Monica Avatar asked Dec 22 '15 14:12

Tomáš Zato - Reinstate Monica


People also ask

How do I change the theme on my Qt Designer?

Activating theme config/QtProject/qtcreator/styles . Go to Qt Creator -> Preferences... , click in the Text Editor tab, and select Dracula in the Color Scheme .

Does Qt have built in icons?

Qt ships with a small set of standard icons you can use in any of your applications for common actions. The icons are all accessible through the current active application style using .

How do I change my QT icon?

This can be done using Microsoft Visual Studio: Select File >> New, and choose the Icon File. Note: You need not load the application into the Visual Studio IDE as you are using the icon editor only.


2 Answers

QIcon::fromTheme works under specific conditions.

If it can find it in the QIcon::themeSearchPaths() for the QIcon::themeName()

If the desired icon isn't there, Qt Designer won't be able to do any of the from theme, named icons.

But... if you check your target system for the theme search paths and set the theme name, you are more likely to have success.

Example

On linux, I wanted to get a plus and a minus icon.

I found list-add.png and list-remove.png fit the bill.

https://github.com/GNOME/adwaita-icon-theme/tree/master/Adwaita/16x16/actions

http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html

I did a locate on my system and found these:

/usr/share/icons/gnome/16x16/actions/list-add.png ... /usr/share/icons/gnome/32x32/actions/list-add.png /usr/share/icons/gnome/scalable/actions/list-add.svg /usr/share/icons/oxygen/16x16/actions/list-add.png ... 

Forcing with fallback icon in QIcon::fromTheme

Find the icon on the filesystem:

ui->toolButton->setIcon(QIcon::fromTheme("list-add",     QIcon("/usr/share/icons/gnome/16x16/actions/list-add.png"))); 

Find the icon in the qt resource system... Add the icon in a qrc file in your build, then reference it's path.

ui->toolButton->setIcon(QIcon::fromTheme("list-add",     QIcon(":/list-add.png"))); 

Overriding the current icon theme

qDebug() << "themeSearchPaths:" << QIcon::themeSearchPaths() << QIcon::themeName();  // themeSearchPaths: ("/usr/local/share/icons", "/usr/share/icons", ":/icons") "hicolor" 

The default theme for the system, and for the target deployment machine, likely didn't have the icons in it I wanted... but the gnome or oxygen icon desktop theme installed would almost always have it...

QIcon::setThemeName("oxygen"); 

Note that you won't see the preview in Qt Designer necessarily because it doesn't set the theme until runtime of your code.

The gnome icon library has 1100+ icons in it. Here is one list:

https://gist.github.com/peteristhegreat/c0ca6e1a57e5d4b9cd0bb1d7b3be1d6a

This works as long as you know what themes are available on the target system.

The list from freedesktop.org has 286 icons listed.

Use icons included in Qt

Just like @peppe pointed out, Qt includes 70 standard icons, too.

widget->setIcon(widget->style()->standardIcon(QStyle::SP_BrowserReload)); 

http://doc.qt.io/qt-5/qstyle.html#StandardPixmap-enum

Conclusion

Using a stock library on your target system is probably the fastest. Using the Qt built-ins is fast to figure out and use, but is fairly limited. Using a resource file is probably the most robust method, and gives unlimited options on what icon to use.

Be sure to pick a standard icon pack, and think about licensing and attributions, and some other things like that.

And there is no shortage of icons available online:

https://www.quora.com/What-is-the-best-icon-library

https://www.google.com/search?q=open+source+icon+library

Hope that helps.

like image 190
phyatt Avatar answered Sep 19 '22 17:09

phyatt


I don't think that's the function you want to use. The "theme" name there corresponds to the QIcon::fromTheme functionality, which uses icons named according to the FDO specification

  • http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
  • http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html

And they're not really supported on non-FDO platforms (Windows, Mac, ...) unless you deploy your own theme files.


Now some stock icons are shipped with Qt itself; I don't know how to set them from Designer, but from code you can use QStyle::standardIcon:

 widget->setIcon(widget->style()->standardIcon(QStyle::SP_BrowserReload)); 

If the icon you need is not provided by Qt, you'll need to ship it. In that case the Resource System is a convenient way to bundle it alongside your executable.


Last, but not least, from a UX point of view you should consider using QToolButtons unless you're really building a Vista-like wizard.

like image 25
peppe Avatar answered Sep 22 '22 17:09

peppe