I am writing code in qt 6, I use the mvc pattern in the rectangle delegate, I specified the x:100 (line 103) property, which was supposed to move the rectangle 100 pixels to the right, this works in qt 5, but in qt6 the position of the rectangle remains unchanged, which is why I am forced to use anchors, and they are not very performant. Why does this problem occur and how to fix it?
ListView {
id: viewMessage
width: _writeMes.x + _writeMes.width
height: 280
spacing: _page.margin
model: _messageModel
delegate: Rectangle {
height: 40
width: viewMessage.width-150
//anchors.left: isMyMessage ? undefined : parent.left
//anchors.right: isMyMessage ? parent.right : undefined
x: isMyMessage ? 100 : 0
radius: 10
color: isMyMessage ? _page.myMessageColor : _page.serverMessageColor
property bool isMyMessage: model.sendBy === _ws.myId
Label {
x: 10
color: _page.textColor
text: 'id: ' + model.sendBy
}
Label {
x: 10
y: 15
color: _page.textColor
text: model.text
}
}
}
as an alternative I use anchors, this gives the desired effect at the expense of performance.
I want to repeat it again on qt5 it works.
2
Answers
You need to let the ListView control the delegate position.
But simplest solution is simple: make delegate an Item, and put the Rectangle etc inside that, positioning them as you wish. Or use Qt.Quick.Controls which offer this functionality directly, I think.
For ease of seeing what is going on, you can add a border: a Rectangle the which fills the Item and has transparent background and colored border.
I tried your example in Qt6 and plugging in my sample data (_page, _ws, _messageModel, _writeMes) but, the example worked.
You can see for yourself