[Interest] QIcon::fromTheme on OS X

Diego Iastrubni diegoiast at gmail.com
Thu Dec 24 14:24:46 CET 2015


yes, this works out of the box only in Linux. On Windows/Mac, you need to
do a little more work. I will give parts of the solution in this email, if
this is not completely clear do tell, and I will elaborate.

I did this in mainwindow.cpp (note that on linux I assume the icons are
available):
#ifndef Q_OS_LINUX
        QStringList l;
        l << ":flat-icon-theme";
        QIcon::setThemeSearchPaths(l);
        QIcon::setThemeName("flat-icon-theme");
#endif

And then create a corresponding resource directory. In my app I only added
512x512 icons, let the Qt menus scale down. Most PCs have enough memory, so
this should not be a problem:

diego ~/source/diego/ats $ ls src/flat-icon-theme/
512x512 build-flat-icon-theme.sh index.theme readme.txt
diego ~/source/diego/ats $ ls src/flat-icon-theme/512x512/
accessories-calculator.png dialog-warning.png document-save.png
edit-paste.png format-text-italic.png list-add.png view-fullscreen.png
zoom-fit-best.png
applications-games.png document-new.png edit-copy.png format-indent-less.png
format-text-underline.png mail-unread.png view-refresh.png zoom-in.png
audio-x-generic.png document-open.png edit-cut.png format-indent-more.png
go-home.png tools-check-spelling.png view-restore.png
dialog-information.png document-print.png edit-delete.png
format-justify-fill.png go-next.png toom-out.png x-office-calendar.png
dialog-question.png document-properties.png edit-find.png
format-text-bold.png go-previous.png video-x-generic.png
x-office-document.png

The last missing "voodoo" is that you *must* define the theme index:

diego ~/source/diego/ats $ cat src/flat-icon-theme/index.theme
[Icon Theme]
Name=Flat
Comment=Flat icons
Inherits=default
Directories=512x512

[512x512]
Size=16


Hope this helps

On Wed, Dec 23, 2015 at 6:07 PM, René J.V. <rjvbertin at gmail.com> wrote:

> Hello,
>
> I am sparring with QIcon::fromTheme() on OS X, and have the feeling I'm
> missing something from the documentation.
>
> From
>
> "Note: By default, only X11 will support themed icons. In order to use
> themed icons on Mac and Windows, you will have to bundle a compliant theme
> in one of your themeSearchPaths() and set the appropriate themeName()."
>
> I understand that even with the native OS X platform theme, all I would
> have to do is ensure that QIcon::themeSearchPaths() returns a list with
> XDG-compliant icon themes, and QIcon::themeName() a ditto icon theme name.
>
> I think I am replying to both conditions, adding
>
>         qDebug() << Q_FUNC_INFO << "themeSearchPaths=" <<
> themeSearchPaths() << "themeName=" << themeName();
>
> to the actual lookup-part of QIcon::fromTheme() I get the output
>
> static QIcon QIcon::fromTheme(const QString &, const QIcon &)
> themeSearchPaths= ("/opt/local/share/icons", "/opt/local/share/pixmaps",
> ":/icons") themeName= "hicolor"
>
> Evidently I have the hicolor theme in /opt/local/share/icons, and indeed
> when I launch applications using the XCB QPA plugin or using the KDE
> platform theme, I'm getting the expected icons, e.g.
>
> hasUserTheme= false engine= 0x7fe58bf583a0 name "dialog-ok" ->
> QIcon("dialog-ok",availableSizes[normal,Off]=(QSize(16, 16), QSize(22, 22),
> QSize(32, 32), QSize(48, 48), QSize(64, 64), QSize(128, 128), QSize(256,
> 256)),cacheKey=0x100000000)
>
> However, using just the native OS X theme I'm seeing this:
>
> hasUserTheme= false engine= 0x7fb5b9e3c880 name "dialog-ok" ->
> QIcon(availableSizes[normal,Off]=(),cacheKey=0x100000000)
> hasUserTheme= false engine= 0x7fb5b9f05b40 name "dialog-cancel" ->
> QIcon(availableSizes[normal,Off]=(),cacheKey=0x200000000)
> hasUserTheme= false engine= 0x7fb5b9c71c20 name "user-trash" ->
> QIcon(availableSizes[normal,Off]=(),cacheKey=0x700000000)
>
> It looks like a bit of required functionality isn't loaded or is cut out
> of the loop with the OS X platform theme, but that's not what the
> documentation suggests.
>
> Alternatively: the documentation also mentions a "guaranteed fallback for
> platforms that do not support theme icons", e.g. "edit-undo" ==
> ":/undo.png". Is there a lookup function somewhere that maps all
> known/supported XDG icon names to that fallback naming scheme?
>
> Thanks,
> René
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20151224/5b81b4ab/attachment.html>


More information about the Interest mailing list