In QML for Qt5 I could use ColorOverlay to apply a color to an image.
But in Qt6, it no longer exists.
What is the best way to color an image in QML for Qt6?
In QML for Qt5 I could use ColorOverlay to apply a color to an image.
But in Qt6, it no longer exists.
What is the best way to color an image in QML for Qt6?
Based on the comments to your question and my tinkering with it, I managed to found 2 ways of colouring an image/icon in Qt 6 QML:
Achieved by using icon
property of the Button class, for instance:
Button {
// ...
icon.source: "<IMAGE_SOURCE>"
icon.color: "<IMAGE_COLOUR>"
icon.width: <IMAGE_WIDTH>
icon.height: <IMAGE_HEIGHT>
// ...
}
Unfortunately, the image is centered both vertically and horizontally and it is unchangeable.
IconImage
class - completely layout-wise customizableThe class provides color
property out of the box, allowing you to easily change an image colour, while also enabling you to position it however you want because it is a separate class.
The following example demonstrates how it can be used to create more complex buttons, having e.g. both a text and an image, thanks to contentItem
property:
Button {
// ...
contentItem: Item {
id: content
IconImage {
id: <IMAGE_ID>
source: "<IMAGE_SOURCE>"
width: <IMAGE_WIDTH>
height: <IMAGE_HEIGHT>
color: "<IMAGE_COLOUR>"
}
// ...
// other elements classes like Text, etc.
// ...
}
// ...
}
As usual, there is no "the best" solution to the problem. It is up to you to decide which one you very need. The first one is the simplest to setup while the second one is the most customizable.
© 2022 - 2024 — McMap. All rights reserved.
Qt5Compat.GraphicalEffects
module. Alternatively, look into shader effects. – Aileen