Search code examples
qmlqtquick2qtquickcontrols2

QML Image size is ignored


I have a ToolButton in QML with an image with size 48x48 pixels:

ToolButton {
    contentItem: Image {
        source: "Icons/idea48.png"
    }
}

if I set width and height nothing changes:

ToolButton {
    contentItem: Image {
        source: "Icons/idea48.png"
        width: 5
        height: 5
    }
}

on the screen it is still 48x48.

And even adding fill Mode does not help:

ToolButton {
    visible: scene.serviceMode
    contentItem: Image {
        source: "Icons/idea48.png"
        width: 10
        height: 10
        fillMode: Image.Stretch
        sourceSize: {
            width: 48
            height: 48
        }
    }
}

the sourceSize should be 48 to render image with high pixel density.

I also tried to put Image inside Item, but with no success:

ToolButton {
    contentItem: Item {
        width: 24
        height: 24
        Image {
            source: "Icons/idea48.png"
            fillMode: Image.Stretch
            sourceSize: {
                width: 48
                height: 48
            }
        }
    }
}

Solution

  • Qt Quick Controls 2.3 (Qt 5.10) adds built-in support for button icons. By default, different styles may request different icon sizes, according to their design guidelines, but you can easily override the icon size.

    ToolButton {
        icon.width: 24
        icon.height: 24
        icon.source: "Icons/idea48.png"
    }
    

    What comes to high-DPI support, consider providing @Nx versions like the Gallery example does: http://code.qt.io/cgit/qt/qtquickcontrols2.git/tree/examples/quickcontrols2/gallery/icons/gallery?h=5.10