IconButton, how to use own icons with highlight [not a question]

asked 2014-06-01 00:34:40 +0300

kimmoli gravatar image

updated 2018-12-16 19:08:59 +0300

Tanghus gravatar image

IconButton supports stock theme-icons with highlight feature, but unfortunately not your own icons.

For this purpose you need to implement your own QQuickImageProvider, which mimics operation of theme -imageprovider from Sailfish Silica.

Assumes your own icons are stored in /usr/share/harbour-appname/qml/icons/

e.g. IconProvider.h


#include <sailfishapp.h>
#include <QQuickImageProvider>
#include <QPainter>
#include <QColor>

class IconProvider : public QQuickImageProvider
    IconProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap)

    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
        QStringList parts = id.split('?');

        QPixmap sourcePixmap(SailfishApp::pathTo("qml/icons/" + parts.at(0) + ".png").toString(QUrl::RemoveScheme));

        if (size)
            *size  = sourcePixmap.size();

        if (parts.length() > 1)
            if (QColor::isValidColor(parts.at(1)))
                QPainter painter(&sourcePixmap);
                painter.fillRect(sourcePixmap.rect(), parts.at(1));

        if (requestedSize.width() > 0 && requestedSize.height() > 0)
            return sourcePixmap.scaled(requestedSize.width(), requestedSize.height(), Qt::IgnoreAspectRatio);
            return sourcePixmap;


And then in main.cpp after createView()

#include "IconProvider.h"
QQmlEngine *engine = view->engine();
engine->addImageProvider(QLatin1String("myIcons"), new IconProvider);

This can then be used in QML

    icon.source: "image://myIcons/icon-toss"
    anchors.bottom: parent.bottom
    highlighted: tossMode

    onClicked: tossMode = true

edit retag flag offensive reopen delete

The question has been closed for the following reason "not a real question" by kimmoli
close date 2014-10-11 21:46:13.983440


Thank you for sharing :) you could also share it on developer help blog: http://sailfishdev.tumblr.com/

Mariusmssj ( 2014-06-01 00:58:57 +0300 )edit

thanks, added to http://sailfishdev.tumblr.com/ blog

coderus ( 2014-06-01 08:53:15 +0300 )edit

Thank you very much for this. :)

Buschmann ( 2014-06-01 14:29:38 +0300 )edit

Hi. Can I use this IconProvider with GPL code? Can you declare license of this code? Thanks. Lukas

Karry ( 2018-11-14 09:46:27 +0300 )edit

I would say it is WTFPL, but you can choose LGPL3 also.

kimmoli ( 2018-11-14 10:24:56 +0300 )edit