2017-06-15 83 views
0

我现在开始使用qt/qml编程,它听起来有点不同于java,现在我的区域,所以我需要在一个文件夹中填充一组.png文件的数组,但是我不知道我该怎么做。 我有这样的:qml中的图像列表

// ... 
Image { 
    anchors.top: parent.center 
    anchors.left: parent.center 
    anchors.margins: 0 
    width: 22 
    height: width 
    source: "qrc:/images/img000.png" 
} 
// ... 

[编辑]此代码是日历例如” ../Qt-5.9/quickcontrols/controls/calendar的一部分,我得到了它的开始,因为这个例子是像我需要,但我需要在日历网格中显示此挂载的所有日期中放置图像,所以我将这些图像放在一个文件夹中,并在我的recources.qrc文件中的相应文件夹中引用这些图像,所以我需要循环这个文件夹来获取所有的图像,因为他们被顺序命名为“img000.png”到“img029.png”,所以我想填充这些图像的数组,然后把这些图像在适当的代表性日期逐一适合这个在日历网格中显示月份 我不知道我是否使用Lis吨...或者像@ eyllanesc迭代文件夹文件,我相信哪些操作列表是非常不同的形式的Java,这是在我的项目为Android的Java中。

我不知道QT,但我需要它,因为我需要迁移此项目的IO?

在此先感谢!

+0

“QML图片LIS t模型“将是一个搜索上下文。这个问题可能没有资格在这里发布。 – AlexanderVX

+0

嗨@AlexanderVX,很抱歉,如果我importunate你的问题,这是因为像我说的,我开始在QT,现在我的Java程序员,所以我完全混淆了什么是在QT/QML,但感谢您的警告。 –

+0

NP。你的问题比我最初阅读的要复杂一点。 – AlexanderVX

回答

2

由于图像的来源是类型为0xx.png的元素,可以用Repeater的索引进行迭代,为了适应它们,您可以使用GridLayout。

GridLayout { 
     columns: 5 
     Repeater { 
      model: 30 
      Rectangle{ 
       width: 100 
       height: 100 
       Image{ 
        anchors.fill: parent 
        source: { 
         var str = "%1".arg(index); 
         var format = "000"; 
         return "qrc:/images/img%1.png".arg(format.substring(0,format.length- str.length) + str)} 
       } 
      } 
     } 
    } 

我不想通过一个循环迭代,因为它实现GUI时是不优雅,不如采取特定的元素,例如你的情况天的月份,并采取“指数”。每天添加图片后,您只需要进行以下更改日历例如:

注:我认为图像img001.png,img002.png,...,img031.png

source code

Image { 
    //visible: eventModel.eventsForDate(styleData.date).length > 0 
    anchors.top: parent.top 
    anchors.left: parent.left 
    anchors.margins: -1 
    width: 12 
    height: width 
    //anchors.fill: parent 
    source: { 
     var str = "%1".arg(styleData.date.getDate()); 
     var format = "000"; 
     return "qrc:/images/img%1.png".arg(format.substring(0,format.length- str.length) + str) 
    } 
} 

截图:

enter image description here

+0

嗨@eyllanesc,至少在你的例子中,你给了我一个定向灯,谢谢! –

+0

嘿@eyllanesc,这真的是一个超级美丽和简单的解决方案,谢谢!当我结束这个项目时,我会在这里发布“最终剪辑”。 –

+0

如果我的回答有帮助,请将其标记为正确 – eyllanesc