QML表视图图像作为单元格元素

Har*_*ris 4 qt qml

我试图在一行QML表视图组件中显示“图像和文本”,我选择itemDelegate来完成它,但是结果显示为每一行上的粗体文本,并且在图像列中同时显示了图像和文本。似乎模型项在表上显示了两次。

码:

Rectangle{
        width:parent.width
        height:parent.height
        color: "#333333"
        id: root


    ListModel {
        id: live_alertmodel

    }

    TableView {
       // anchors.top: download_bt.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        width: root.width
        height: 100

        TableViewColumn {
            role: "time"
            title: "Time"
            width: root.width/4
        }
        TableViewColumn {
            role: "location"
            title: "Location"
            width: root.width/4
        }

        TableViewColumn {
            role: "alert"
            title: "Alert"
            width: root.width/4
        }

        TableViewColumn {
            role: "image"
            title: "Image"
            width: root.width/4

        }
        model: live_alertmodel

       itemDelegate: Item {
            Text {
                anchors.verticalCenter: parent.verticalCenter
                color: styleData.textColor
                //elide: styleData.elideMode
                text: styleData.value
            }

            Text {
                anchors.verticalCenter: parent.verticalCenter
                color: styleData.textColor
                //elide: styleData.elideMode
                text: styleData.value
            }

            Text {
                anchors.verticalCenter: parent.verticalCenter
                color: styleData.textColor
                //elide: styleData.elideMode
                text: styleData.value
                font.bold: false
            }

            Image {
                id: myIcon;
                width:root.width/4;
                //height:50;
                anchors.horizontalCenter: parent.horizontalCenter;
                source: styleData.value;
                fillMode: Image.PreserveAspectFit
                height:20
                cache : true;
                asynchronous: true;
            }
       }


        Component.onCompleted: {
            for(var i=0;i<10;i++)
              live_alertmodel.append({ time:"12/15/2015",
                              location:"location",
                              alert:"access",
                              image:"http://images.freeimages.com/images/premium/previews/4852/48521810-globe-icon-flat-icon-with-long-shadow.jpg" })
        }
    }
    }
Run Code Online (Sandbox Code Playgroud)

还要查看屏幕快照,上面的代码显示输出如何。

在此处输入图片说明

上面的代码有什么问题吗?

Har*_*ris 5

我已经解决了

  1. itemDelegate从中删除TableView

  2. delegate为每个TableViewColumn喜欢定义商品,

    TableViewColumn {
        role: "time"
        title: "Time"
        width: root.width/4
        delegate:textDelegate
    }
    TableViewColumn {
        role: "location"
        title: "Location"
        width: root.width/4
         delegate:textDelegate
    }
    TableViewColumn {
        role: "alert"
        title: "Alert"
        width: root.width/4
        delegate:textDelegate
    }
    
    TableViewColumn {
        role: "image"
        title: "Image"
        width: root.width/4
        delegate:imageDelegate
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 最终为text和image列创建了单独的委托项

     Component  {
                    id: textDelegate
                    Item {
                        id: f_item
                        height: cell_txt.height
                        Text {
                            id: cell_txt
                            width: parent.width
                            verticalAlignment: Text.AlignVCenter
                            horizontalAlignment: Text.AlignHCenter
                            //font.bold: true
                            text: styleData.value
                            elide: Text.AlignHCenter
                            color: "white"
                            renderType: Text.NativeRendering
                        }
                    }
                }
    
    
    Component {
        id: imageDelegate
        Item {
            Image {
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                fillMode: Image.PreserveAspectFit
                height:20
                cache : true;
                asynchronous: true;
                source: styleData.value// !== undefined  ? styleData.value : ""
            }
        }
     }
    
    Run Code Online (Sandbox Code Playgroud)