2015-04-17 39 views
0

我正在使用Qt 5.4.1及其Qt Quick模塊應用程序。我從/images目錄加載一些.svg圖片,然後在ListView中顯示它們,這很好。但是,如何在每個加載的.svg圖像周圍添加陰影漸變?這裏是MWE:Qt快速添加梯度框架圖片

import QtQuick 2.4 
import QtQuick.Controls 1.3 
import QtQuick.Window 2.2 
import QtQuick.Dialogs 1.2 

import Qt.labs.folderlistmodel 2.1 

Rectangle 
{ 
    id: ueMainWindow 

    visible: true 

    width: 800 
    height: 1280 

    color: "black" 

    property string ueRootDirectory:"/images" 
    property real ueImagesLoadProgress; 
    property bool ueImageLoading; 

    Rectangle 
    { 
     id: ueContainerThumbnails 

     antialiasing: true 

     color: "black" 

     anchors.bottom: ueMainWindow.bottom 
     width: ueMainWindow.width 

     height: 256 

     gradient: Gradient 
     { 
      GradientStop { position: 0.0; color: "black" } 
      GradientStop { position: 1.0; color: "grey" } 
     } 

     Text 
     { 
      id: ueTextImageName 

      antialiasing: true 

      color: "white" 

      anchors.horizontalCenter: ueContainerThumbnails.horizontalCenter 

      text: qsTr("TestApp") 
     } 

     ListView 
     { 
      id: ueViewThumbnails 

      antialiasing: true 

      orientation: ListView.Horizontal 

      anchors 
      { 
       topMargin: parent.height-(parent.height-50) 
       fill: parent 
      } 

      FolderListModel 
      { 
       id: ueModelImages 

       folder: "file://"+ueRootDirectory 
       nameFilters: ["*.svg"] 
      } 

      Component 
      { 
       id: ueDelegateImage 

       Image 
       { 
        id: ueImage 

        source: ueModelImages.folder + "/" + fileName 

        antialiasing: true 

        asynchronous: true 

        horizontalAlignment: Image.AlignHCenter 
        verticalAlignment: Image.AlignVCenter 

        width: 192 
        height: 192 

        fillMode: Image.PreserveAspectFit 
       } 
      } 

      focus: true 
      spacing: 10 
      leftMargin: 10 
      rightMargin: 35 
      visible: ueModelImages.status==FolderListModel.Ready 

      model: ueModelImages 
      delegate: ueDelegateImage 
     } 
    } 
} 
+1

「陰影漸變」的含義並不完全清楚。張貼您想要達到的效果的圖像。 – dtech

+0

@ddriver我想在圖像周圍放一個「框架」,該框架是從黑色到白色的漸變。 – KernelPanic

+1

在這種情況下,「陰影」具有誤導性,陰影應該表示某種形狀的投影。 – dtech

回答

1

那麼,你應該把那個梯度以某種方式放入你的委託。您可以:

  • 創建一個空Item,把RectangleImage裏面

例如:

Component { 
    id: ueDelegateImage 
    Item { // container 
     Rectangle { 
      // gradient rectangle 
     } 
     Image { 
      // image 
     } 
    } 
} 
  • 或將ImageRectangle

例如:

Component { 
    id: ueDelegateImage   
    Rectangle { 
     // gradient rectangle acts as a container 
     Image { 
      // image 
     }    
    } 
} 

在兩種情況下堆疊順序將繪製圖像後面的梯度矩形。代表只能有一個根元素,但不限於只有一個元素,您可以根據需要嵌套多個元素。

+0

這種方法是否會給硬件造成沉重的負擔,例如,如果我在ListView中有500個帶有漸變矩形的.svg文件,您認爲如何? – KernelPanic

+1

開銷將是SVG文件,而不是漸變。 QtQuick2使用優化的渲染器,這非常適合繪製大量相同的對象。此外,列表視圖根據需要加載元素,並不是所有的元素都在同一時間加載,它只顯示當前適合屏幕的元素,以及之前和之後緩存​​的元素。 – dtech