Skip to content

Commit afb284c

Browse files
committed
ref #35, QML QuickImageProvider Support.
1 parent bf84536 commit afb284c

File tree

13 files changed

+274
-2
lines changed

13 files changed

+274
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changes
22

3+
- (2025-11-21) #35, QML QuickImageProvider Support. (contribution by @Ivorforce)
34
- (2025-08-28) #67, Update to Font Awesome 7, added Pro+ icon support
45
- (2025-07-15) #66, Style name parsing (stringToStyleEnum) not working properly (contribution by @samapico)
56
- (2025-05-08) #65, Support for Qt 6.9. Fix the CMakelist example

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ project(QtAwesome VERSION ${QTAWESOME_VERSION} DESCRIPTION "Add Font Awesome ico
4141

4242
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
4343
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Widgets)
44+
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Quick Qml)
4445

4546
set(CMAKE_AUTOUIC ON)
4647
set(CMAKE_AUTOMOC ON)
@@ -62,6 +63,14 @@ add_library(QtAwesome
6263
${QtAwesome_HEADERS}
6364
)
6465

66+
if(Qt6Quick_FOUND)
67+
list(APPEND QtAwesome_HEADERS QtAwesome/QtAwesomeQuickImageProvider.h)
68+
target_link_libraries(QtAwesome PUBLIC
69+
Qt${QT_VERSION_MAJOR}::Quick
70+
Qt${QT_VERSION_MAJOR}::Qml
71+
)
72+
endif()
73+
6574
include(GNUInstallDirs)
6675

6776
target_include_directories(QtAwesome

QtAwesome/QtAwesome.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <QFontDatabase>
1818
#include <QFontMetrics>
1919
#include <QString>
20+
#include <QUrlQuery>
2021

2122

2223
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
@@ -113,7 +114,7 @@ class QtAwesomeCharIconPainter: public QtAwesomeIconPainter
113114
painter->setRenderHint(QPainter::HighQualityAntialiasing);
114115
#endif
115116

116-
QVariant var =options.value("anim");
117+
QVariant var = options.value("anim");
117118
QtAwesomeAnimation* anim = var.value<QtAwesomeAnimation*>();
118119
if (anim) {
119120
anim->setup(*painter, rect);
@@ -587,6 +588,59 @@ QString QtAwesome::fontName(int style) const
587588
return _fontDetails[style].fontFamily();
588589
}
589590

591+
/// \brief Requests a pixmap which can drictly be used by QQuickImageProvider
592+
///
593+
/// \param An identifier in the format "regular/name?option1=x&option2=y
594+
QPixmap QtAwesome::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
595+
{
596+
QString baseId = id;
597+
QVariantMap options;
598+
599+
int queryStart = id.indexOf('?');
600+
if (queryStart != -1) {
601+
baseId = id.left(queryStart);
602+
QString queryString = id.mid(queryStart + 1);
603+
604+
QUrlQuery query(queryString);
605+
for (const auto &item : query.queryItems()) {
606+
options.insert(item.first, item.second);
607+
}
608+
transformStringVariantOptions(options);
609+
}
610+
611+
int nameStart = baseId.indexOf("/");
612+
if (nameStart != -1) {
613+
baseId = "fa-" + baseId.left(nameStart) + " fa-" + baseId.mid(nameStart + 1);
614+
} else {
615+
baseId = "fa-solid fa-" + baseId;
616+
}
617+
618+
QIcon icn = icon(baseId, options);
619+
QSize actualSize = requestedSize.isValid() ? requestedSize : QSize(128, 128);
620+
if (size) {
621+
*size = actualSize;
622+
}
623+
624+
return icn.pixmap(actualSize);
625+
}
626+
627+
628+
/// \Brief Transforms a String based hash map to the correct objec types.
629+
/// All color types which aren't colors are converted to QColor objects and Interpreted
630+
/// as HEX codes (# is optional)
631+
void QtAwesome::transformStringVariantOptions(QVariantMap& options)
632+
{
633+
for (auto i = options.cbegin(), end = options.cend(); i != end; ++i) {
634+
QString key = i.key();
635+
if (key.contains("color") && i.value().userType() == QMetaType::QString) {
636+
QString value = i.value().value<QString>();
637+
QColor colorValue = QColor(value[0] == '#' ? QColor(value) : QColor(QString("#%1").arg(value)));
638+
options[key] = colorValue;
639+
}
640+
}
641+
}
642+
643+
590644
int QtAwesome::stringToStyleEnum(const QString style) const
591645
{
592646
if (style == "fa-solid") return fa::fa_solid;

QtAwesome/QtAwesome.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ Q_OBJECT
216216
/// Returns the font-name that is used as icon-map
217217
QString fontName(int style) const;
218218

219+
// Requests a pixmap which can be used by QQuickImageProvider
220+
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
221+
void transformStringVariantOptions(QVariantMap& options);
222+
219223
protected:
220224
int stringToStyleEnum(const QString style) const;
221225
const QString styleEnumToString(int style) const;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <QQuickImageProvider>
4+
#include "QtAwesome.h"
5+
6+
class QtAwesomeQuickImageProvider : public QQuickImageProvider
7+
{
8+
public:
9+
QtAwesomeQuickImageProvider(fa::QtAwesome* awesome)
10+
: QQuickImageProvider(QQuickImageProvider::ImageType::Pixmap)
11+
, _awesomeRef(awesome)
12+
{}
13+
14+
virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) {
15+
return _awesomeRef->requestPixmap(id, size, requestedSize);
16+
}
17+
18+
private:
19+
fa::QtAwesome* _awesomeRef;
20+
};

QtAwesomeSample/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ add_subdirectory(../ QtAwesome)
1313
add_executable(QtAwesomeSample
1414
mainwindow.cpp
1515
main.cpp
16-
)
16+
)
1717

1818
target_link_libraries(QtAwesomeSample
1919
PUBLIC QtAwesome

QtAwesomeSampleQml/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(QtAwesomeSampleQml)
3+
4+
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
5+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Gui Qml Quick)
6+
7+
set(CMAKE_CXX_STANDARD 17)
8+
set(CMAKE_CXX_FLAGS "")
9+
10+
set(CMAKE_AUTOUIC ON)
11+
set(CMAKE_AUTOMOC ON)
12+
set(CMAKE_AUTORCC ON)
13+
14+
add_subdirectory(../ QtAwesome)
15+
16+
#qt6_add_resources(QT_RESOURCES qml.qrc)
17+
add_library(
18+
main.qrc
19+
)
20+
21+
add_executable(QtAwesomeSampleQml
22+
main.cpp
23+
main.qrc
24+
)
25+
26+
target_link_libraries(QtAwesomeSampleQml
27+
PUBLIC QtAwesome
28+
PRIVATE Qt::Qml Qt::Quick
29+
)
30+

QtAwesomeSampleQml/LICENSE.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
MIT License
2+
===========
3+
4+
Copyright 2013-2025 [Rick Blommers](mailto:rick@blommersit.nl). All Rights Reserved.
5+
Author [Rick Blommers](mailto:rick@blommersit.nl)
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
10+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15+
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
16+
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
17+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
19+
Font Awesome License
20+
====================
21+
22+
[https://github.com/FortAwesome/Font-Awesome](https://github.com/FortAwesome/Font-Awesome)
23+
24+
Font Awesome Free is free, open source, and GPL friendly. You can use it for commercial projects,
25+
open source projects, or really almost whatever you want.
26+
27+
- Icons — CC BY 4.0 License
28+
In the Font Awesome Free download, the CC BY 4.0 license applies to all icons packaged as .svg and .js files types.
29+
- Fonts — SIL OFL 1.1 License
30+
In the Font Awesome Free download, the SIL OLF license applies to all icons packaged as web and desktop font files.
31+
- Code — MIT License
32+
In the Font Awesome Free download, the MIT license applies to all non-font and non-icon files.
33+
Attribution is required by MIT, SIL OLF, and CC BY licenses. Downloaded Font Awesome Free files already contain embedded comments with sufficient attribution, so you shouldn't need to do anything additional when using these files normally.
34+
35+
We've kept attribution comments terse, so we ask that you do not actively work to remove them from files,
36+
especially code. They're a great way for folks to learn about Font Awesome.

QtAwesomeSampleQml/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# QML Sample
2+
3+
A simple qml sample for using QtAwesome with QML.
4+
I've never used qml before, so please let me know if things can be improved.
5+
6+
Sample building with cmake:
7+
8+
```sh
9+
cd QtAwesomeSampleQml
10+
mkdir build
11+
cd build
12+
13+
cmake .. -DCMAKE_PREFIX_PATH="~/Qt/6.10.1/macos/lib/cmake/"
14+
cmake --build .
15+
```

QtAwesomeSampleQml/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* MIT Licensed
3+
*
4+
* Copyright 2011-2025 - Ribit Software by Blommers IT. All Rights Reserved.
5+
* Author Rick Blommers
6+
*/
7+
8+
#include "QtAwesome.h"
9+
10+
#include <QApplication>
11+
#include <QQmlApplicationEngine>
12+
13+
#include "QtAwesome.h"
14+
#include "QtAwesomeQuickImageProvider.h"
15+
16+
int main(int argc, char** argv)
17+
{
18+
QApplication app(argc, argv);
19+
20+
fa::QtAwesome* awesome = new fa::QtAwesome(qApp);
21+
awesome->initFontAwesome();
22+
23+
QQmlApplicationEngine engine;
24+
engine.addImageProvider("fa", new QtAwesomeQuickImageProvider(awesome));
25+
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
26+
27+
int result = app.exec();
28+
delete awesome;
29+
return result;
30+
}
31+

0 commit comments

Comments
 (0)