首页 文章

QML中的图像 Map 集

提问于
浏览
0

我有一个像这样的图像:http://www.imagemagick.org/Usage/basics/result.gif(它包含两行个别数据:上排的三个方形图像,另一个方格下方和两个'empty'方格)

我想在转发器中使用它,这样我就可以得到四个图像按钮,每个按钮都有一个子图像 .

QML中是否有“纹理图集”模块?

我只找到http://doc.qt.io/qt-5/qml-qtquick-sprite.html,我希望有一些东西对我的用例更好 .

1 回答

  • 2

    正如我在评论中所说,我会使用 QQuickImageProvider 来做到这一点,因为这是最快和最少内存密集的方式 . 但QML非常灵活,您可以随心所欲地使用它 . 至于你的问题,你可以做如下:

    Tile.qml

    import QtQuick 2.9
    
    Item {
        id: container
        property alias source: img.source
        property int cellWidth: 1
        property int cellHeight: 1
        property int slideIndex: 0
        clip: true
        Image {
            id: img
            property int cols: img.paintedWidth / container.cellWidth
            property int rows: img.paintedHeight / container.cellHeight
            x: -container.cellWidth * Math.floor(container.slideIndex % cols)
            y: -container.cellHeight * Math.floor(container.slideIndex / cols)
        }
    }
    

    和用法:

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        id: window
        title: "Test"
        visible: true
        width: 600
        height: 400
    
        Row {
            spacing: 10
            anchors.centerIn: parent
            Repeater {
                model: 4
                Tile {
                    width: 32
                    height: 32
                    cellWidth: 32
                    cellHeight: 32
                    slideIndex: index
                    source: "http://www.imagemagick.org/Usage/basics/result.gif"
                }
            }
        }
    }
    

    我们的想法是将整个图像包装到一个容器中并适当地剪裁它 . 在这里,我使用基于索引的幻灯片访问,您可以根据需要进行操作 .

相关问题