solitare/qml/CardModel.qml
ItsDrike d21e9d1336
Fix SVG scaling in QML
For some reason, the default QML Image element doesn't scale SVGs like
it should. Essentially, it just reads the source size of the SVG and
scales it like it would a regular image (say PNG).

I'm fixing this by manually setting the source size of the image to the
current size. That said, this is in my opinion a hacky fix and it's
really weird that QML doesn't special-case SVGs and scale them as it
should.

Source:
https://stackoverflow.com/questions/69641034/qml-image-does-not-stretch-fit-svg
2024-12-09 20:57:51 +01:00

34 lines
994 B
QML

import QtQuick
import Solitare
Item {
id: cardModel
required property PlayingCard card
required property bool isFaceDown
property string backStyle: "red"
signal clicked
width: 80
height: width * 1.4 // Maintian the aspect ratio of a playing card
Image {
id: cardImage
anchors.fill: parent
source: cardModel.isFaceDown ? "qrc:/img/playing_cards/backs/" + cardModel.backStyle + ".svg" : cardModel.card ? "qrc:/img/playing_cards/fronts/" + cardModel.card.suitString + "_" + cardModel.card.valueString + ".svg" : "qrc:/img/playing_cards/backs/blue.svg"
// This makes sure the SVG scales properly, otherwise it would
// attempt to scale the image from the original source size, which
// can end up being blurry.
sourceSize: Qt.size(width, height)
fillMode: Image.PreserveAspectFit
MouseArea {
anchors.fill: parent
onClicked: cardModel.clicked()
}
}
}