首页 > 解决方案 > 为什么 SwipeView 组件不尊重 Item 模型属性?

问题描述

在我的应用程序中,我有一个带有图像和尺寸的项目

LanguageImage.qml

Item {
...
        Image
        {
            width: 100
            height:  100            
            id:imageFlag
            anchors.fill: parent
            fillMode: Image.PreserveAspectCrop

        }
...
}

当我想在 SwipeView 中使用时,我把它们当作组件

main.qml

...
   SwipeView
    {
        id:sliderImmagine
        anchors.fill: parent
        currentIndex: 0
        clip:true

        LanguageImage
        {

            id:italian
            Image
            {
                width:100
                height: 100
                source: "/image/italy.jpg"
            }
        }

        LanguageImage
        {
            id:spanish
            Image
            {
                source: "/image/spain.jpg"
            }
        }
        LanguageImage
        {
            id:french
            Image
            {
                source: "/image/france.jpg"
            }
        }

    }

但是这些项目不尊重我在项目类型中设置的尺寸。为什么这个?如果我在 SwipeView 组件中强制尺寸,那没关系。有解决方案还是我错了?

标签: qtpropertiesqmlcomponentsswipeview

解决方案


如果您想提供对组件图像的访问,您必须执行以下操作:

Item {
    property alias source: imageFlag.source
     Image {
         width: 100
         height:  100            
         id:imageFlag
         anchors.fill: parent
         fillMode: Image.PreserveAspectCrop
     }
}

现在您可以按如下方式使用它:

LanguageImage {
    source: "/image/italy.jpg"
}

推荐阅读