gpt4 book ai didi

qt - QML TableView 图像作为单元格元素

转载 作者:行者123 更新时间:2023-12-02 04:04:39 26 4
gpt4 key购买 nike

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

代码:

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" })
}
}
}

另请参阅上面代码的输出效果的屏幕截图。

enter image description here

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

最佳答案

我已经解决了

  1. TableView 中删除 itemDelegate

  2. 为每个 TableViewColumn 定义 delegate 项,例如,

    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
    }
  3. 最终为文本和图像列创建了单独的委托(delegate)项

     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 : ""
    }
    }
    }

关于qt - QML TableView 图像作为单元格元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33624258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com