import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Window {
id:mainwindow
visible: true
width: 600
height: 480
title: qsTr("This is qml Window")
color: "#ffffff"
function init()
{
if(!sdkasfksdlfsd.count)
{
return
}
var widthaList = {}
for(var i = 0;i < sdkasfksdlfsd.count;i++)
{
widthaList[i] = (mylistview.itemAtIndex(i).texxx.width+30)
console.log(widthaList[i])
}
//排序
for( i = 0;i < sdkasfksdlfsd.count;i++)
{
for( let j = 0;j < sdkasfksdlfsd.count -i;j++)
{
if (widthaList[j] < widthaList[j + 1])//满足条件进行交换
{
var temp = widthaList[j];
widthaList[j] = widthaList[j + 1];
widthaList[j + 1] = temp;
}
}
}
//判断
for( i = 0;i < sdkasfksdlfsd.count ;i++)
{
console.log(widthaList[i]+"---"+i)
if( widthaList[i] === scroll.contentWidth)
{
return
}
else{
scroll.contentWidth = widthaList[i]
if (dshfkajsdkfkasldf.width > scroll.contentWidth)
{
scroll.contentWidth = dshfkajsdkfkasldf.width
}
return
}
}
}
Rectangle{
id:dshfkajsdkfkasldf
width: 530
height: 300
anchors.centerIn: parent
ScrollView{
id:scroll
width:parent.width
height: parent.height
clip: true
anchors.centerIn: parent
contentWidth: parent.width
contentHeight: sdkasfksdlfsd.count*30>300?sdkasfksdlfsd.count*30:300
ListModel{
id:sdkasfksdlfsd
}
ListView{
id:mylistview
width: parent.width
height: parent.height
leftMargin: 10
topMargin: 10
rightMargin: 10
bottomMargin: 10
model:sdkasfksdlfsd
clip: true
delegate: Rectangle{
width: parent.width
height: 30
color: mylistview.currentIndex == index?"#0079FE":"#ffffff"
property alias texxx: dadfasdfsd
Text {
id:dadfasdfsd
text: modeDtas
font.pointSize: 12
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.leftMargin: 5
color: mylistview.currentIndex == index?"black":"black"
onWidthChanged: {
if((width+30)>scroll.contentWidth)
{
scroll.contentWidth = width+30
}
}
}
MouseArea{
anchors.fill: parent
hoverEnabled: true
onClicked: {
mylistview.currentIndex = index
}
}
}
}
}
}
Timer{
id:updateWidth
running: false
interval: 300
onTriggered: {
init()
}
}
Row{
width: parent.width
height: 30
anchors.top: dshfkajsdkfkasldf.bottom
anchors.topMargin: 10
spacing: 10
TextField{
id:textEdit
width: 150
height:30
anchors.verticalCenter: parent.verticalCenter
selectByMouse: true
text:""
font.pointSize: 10
}
Button{
text: "添加"
width: 60
height: 30
onClicked: {
sdkasfksdlfsd.append({"modeDtas":textEdit.text})
}
}
Button{
text: "清空"
width: 60
height: 30
onClicked: {
sdkasfksdlfsd.clear()
scroll.contentWidth = 300
}
}
Button{
text: "删除"
width: 60
height: 30
enabled: !updateWidth.running
onClicked: {
sdkasfksdlfsd.remove(mylistview.currentIndex)
updateWidth.start()
}
}
}
}
ListView搭配ScrollView实现动态更改横向宽度
于 2022-11-17 10:45:35 首次发布