ListView’s flickable problems
Page |
1 |
Hi everyone.
I noticed that when (vertical) listView’s items has different height, contentHeight value doesn’t equal to height of all items. When changing ListView’s height, contentHeight may be larger or smaller than it should be. Why is it so? How does ListView count it’s contentHeight?
The same issue when items have different height, and i am swapping two items and trying to flick listView on item’s height by changing contentY value;
[edit : typo in title fixed, Eddy]
21 replies
I understand that they are flickable objects. The problem is that if items have different height, the listView’s contentHeight is counted not correct;
Unfortunaly i am not able to post my code here, because it is to much of it, but i’ll try to post an example.
ListDelegate.qml
- Compenent{
- Item{
- height: edit.height
- TextEdit{
- id: edit
- text: someText //(role)
- }
- }
- }
The sometext value may be multiline, so each delegate may have different height
Mmmh… I see. The problem I think maybe in the text. As a matter of fact I have expereienced that the height of your edit example is not always correct if the text is higher. You should check the height of the text.
But, just to be clear, I understand that you don’t need to show your code, but this example is so simple. It is difficult to disucss about something that is not possible to see.
Partially true. The real text height depends on the font size and the number of characters. Then the program shows text with word wrap (I suppose yes in this case). This means that you can’t know before how much lines your text is really height. Thus you should manage the height of every list element based on the painted height. Eventually you can decide in your model to put a control like elide the test that exceeds the previous decided height or if the painted height exceeds the height of your list model you should remove lines or words from your sentence before it is shown.
I think yes. The reason will be that you try setting the value of the element that is built automatically depending on the ListModel, where you define the aspect of the elements that formerly are part of the generic abstract representation of any element of the list.
ListView just fill the model based on the ListElements (or any other element i.e. the XmlList). I think that you should try to set the sizes in the ListModel. Thus when you define the model you don’t know yet what will be the size of that particular instance of the model when the ListView will fill it with the ListData. If in the model you create an area that changes depending on every element content I think that everything works fine.
A short advice in your case: if you set a text in a rectangle the object that you manage as the size reference is the rectangle, despite the text size. To avoid this problem you should do something like the following:
- Create a rectangle set with the initial width and height you want.
- Set the rectangle opacity to 0 if it should be hidden: you only need a fake object
- Create a text object and bind the rectangle height to the full text height: this means that depending by how much text you have the rectangle will be always height to contain it.
- Align the text object on top margin of the rectangle (one over the other)
Try it and let me know if it works. I you have some doubt I will try to create this kind of coupled object.
See the following example. Sorry but I get it from a more complex piece of code so I think it can’t work. BTW you can get the idea applying it in your code without difficult.
- // Login form for registered users
- Item {
- id: login
- width: parent.width;
- // Height is set based on the height value of the text area.
- height: background.textHeight
- signal newuserClicked; signal loginClicked
- [... some custom properties definition ...]
- // Main background recatngle with information text
- Rectangle {
- id: background
- width: parent.width
- property alias textHeight: textIntro.paintedHeight
- height: parent.height
- anchors.horizontalCenter: parent.horizontalCenter
- anchors.bottom: parent.bottom
- color: "#d1cece"; border.color: "#eeeeee";
- border.width: Geometry.getFormBorder();
- radius: Geometry.getRadius()
- // Introductory text over the login area.
- Text {
- id: textIntro
- width: background.width * 0.95; anchors.top: background.top
- anchors.topMargin: 5;
- anchors.horizontalCenter: background.horizontalCenter
- color: "#ffffff"
- wrapMode: TextEdit.Wrap; font.pointSize: Geometry.getFontPoints();
- text: login.loginIntro
- }
- }
- [... other stuff ...]
- }
Hope it can help you.
Or it is me who make it wrong, or i don’t know :)
As for rectangles and text inside of it, in my current code text is in rectangle already, and my delegate’s height depends on text height and if Loader component is loaded, it’s height added to delegate’s height, and there is also separator line to separate two types of items in the view; I thought that something in my current code is making contentHeight value “jumping”, so i just created small example project and test everything using it;
I also noticed that contentHeight value changed when item with higher height scrolls away or returns to screen; So i think that contentHeight depends on items visibility or smth.
p.s. tried to set item’s height from model – didn’t help
Here is example that i am using
ListDelegate.qml
- Component{
- Item{
- height: layout.height
- Rectangle{
- id: layout
- height: index == 3 ? 50 : 20
- TextEdit{
- id: edit
- height: paintedHeight
- font {pixelSize: 20}
- text: title
- }
- }
- }
- }
ListModel.qml
- ListModel{
- id: model
- ListElement{
- title: "this is test text"
- }
- ListElement{
- title: "World"
- }
- ListElement{
- title: "Some more text"
- }
- ListElement{
- title: "And some more :)"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- ListElement{
- title: "Dummy text"
- }
- }
main.qml
- import QtQuick 1.0
- Rectangle {
- width: 360
- height: 360
- ListView{
- id: view
- anchors {fill: parent;}
- spacing: 5
- delegate: ListDelegate{}
- model: ListModel{}
- Rectangle{
- anchors {fill: view.contentItem;}
- color: "transparent"
- border {color: "red"; width: 2;}
- }
- onContentYChanged: {console.log(contentHeight)}
- }
- }
You must log in to post a reply. Not a member yet? Register here!


