IconButton, how to use own icons with highlight [not a question]
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
#ifndef ICONPROVIDER_H
#define ICONPROVIDER_H
#include <sailfishapp.h>
#include <QQuickImageProvider>
#include <QPainter>
#include <QColor>
class IconProvider : public QQuickImageProvider
{
public:
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.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.fillRect(sourcePixmap.rect(), parts.at(1));
painter.end();
}
if (requestedSize.width() > 0 && requestedSize.height() > 0)
return sourcePixmap.scaled(requestedSize.width(), requestedSize.height(), Qt::IgnoreAspectRatio);
else
return sourcePixmap;
}
};
#endif // ICONPROVIDER_H
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
IconButton
{
icon.source: "image://myIcons/icon-toss"
anchors.bottom: parent.bottom
highlighted: tossMode
onClicked: tossMode = true
}
Thank you for sharing :) you could also share it on developer help blog: http://sailfishdev.tumblr.com/
Mariusmssj ( 2014-06-01 00:58:57 +0200 )editthanks, added to http://sailfishdev.tumblr.com/ blog
coderus ( 2014-06-01 08:53:15 +0200 )editThank you very much for this. :)
Buschmann ( 2014-06-01 14:29:38 +0200 )editHi. Can I use this IconProvider with GPL code? Can you declare license of this code? Thanks. Lukas
Karry ( 2018-11-14 09:46:27 +0200 )editI would say it is WTFPL, but you can choose LGPL3 also.
kimmoli ( 2018-11-14 10:24:56 +0200 )edit