Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Qt standard icons from within Qt Creator

Tags:

I would like to use the Qt Standard icons (as here). I have found many examples how to set the icons programmatically (runtime in code).

However, how could I set the "standard icon name" (e.g. application-exit) in Qt Creator? This blog here says, I do simply set the theme to the respective name. But I do not have such a theme property, I'd get a popup with a resource (screenshot below).

Where is the right place to apply the "standard name" (e.g. application-exit)?

Update from 2.4.x to Qt Creator 2.5.2 gives me the "theme" property from the above blog, but still no visible icons yet. What else is required to make the icons visible?

Theme now available with Creator 2.5.2

Remark Qt 4.8.1, now updated to 2.5.2, Windows 7

like image 361
Horst Walter Avatar asked Sep 06 '12 12:09

Horst Walter


People also ask

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 make Qt icons default?

To use these icons within Qt Designer you would select the drop-down and choose "Set Icon From Theme..." You then enter the name of the icon you want to use, e.g. document-new (the full list of valid names).


2 Answers

Theme icons are only supported on Linux/X11. On Windows and OS X, you have to provide your own icons.

The solution to this problem is to set the theme name in QtCreator (as in your example). Then you have to provide icons of the same Alias under Prefix ":/icons" in your resources. You can leverage icons from sets available in the public domain, e.g. the popular Tango icon set.

If you only target the Windows platform, the theme setting will be of no use for you. If you target both Linux/X11 and other platforms, with this solution you get the native icons on one system and the icons you provide yourself on the others.

You can find a very good explanation on how to do it here: http://mithatkonar.com/wiki/doku.php/qt/icons

like image 153
ypnos Avatar answered Nov 10 '22 00:11

ypnos


@ypnos suggested a great link with multiple approaches to solve the problem. I favorite the last one - Creating a custom icon theme. The author states three Pros (Available to all platforms, Excellent resizing, Covers all needed icons), and two Cons (Not consistent with system icon theme, Pain in the ass to implement). Here I suggest improvements to get rid of the Cons.

I've cloned the Tango iconset from github. Repository https://github.com/ppinard/qtango already features the index.theme file. But mainly, it brings a Python script generate_rcc.py, which can generate the *.qrc file automatically. I just had to change the arguments of subprocess.check_call() from '--binary' and '--compress' to '-binary' and '-compress'. The generated file contained absolute paths, but that's easy to Find&Replace. One can use this script to any iconset - first laborious step is solved.

Now, using the "Theme" property, you can define the icons in the Qt Designer, as already shown in the question. For those developing under Linux, system icons will show right in the Designer (suppose the iconset uses standard icon names). That's the native look (icons will be configurable in your system settings). That frees you from the ui->action_Open->setIcon(...) coding.

And the final tweak is to set the theme before ui setup.

MainWindow::MainWindow(QWidget *parent) :   QMainWindow(parent) { #ifdef _WIN32   QIcon::setThemeName("tango"); #endif    ui = new Ui::MainWindow;   ui->setupUi(this);    ...   // NO NEED FOR ui->action_Open->setIcon(...) } 

The #ifdef can of course be adjusted to target all needed platforms, or omitted to force the same icons on all platforms including Linux.

As a result, this approach avoids all laborious coding and the result is consistent with the system icons at least on Linux.

like image 37
yman Avatar answered Nov 09 '22 23:11

yman