How to get ScrollView to scroll when children are reparented into it?
-
Do this :
State { name: "EXPANDED"; when: textArea.text ParentChange { target: textArea parent: addResourceScrollView.contentItem.contentItem x: 0; y: 0 width: window.width } }
You don't need to set the
height
since you don't want it to be constrained.As for
parent: addResourceScrollView.contentItem.contentItem
:
addResourceScrollView.contentItem
is the defaultFlickable
of theScrollView
, and you want to put your TextArea in the contentItem of the Flickable. Personnaly I would have directly used a Flickable instead. -
Aha, thanks,I would have never thought of using that parent. I'll read up on
Flickable
, I've been ignoring it because I focus on desktop platforms.ScrollView
now scrolls as it should, however reverting to the previous state does not go exactly to plan:What would be the issue? I'm at a loss.
-
Hi @Vadi2 , it seems to be working fine in my case, have made any changes to the code which you had posted?
-
Hi @Vadi2
Your code works fine.I just changed the state of TextArea to test on button click to
Button { id: settingsButton text: "☰" onClicked: { textArea.state = "MINIMAL" addResourcesPage.state = "EDITING_SETTINGS" } }
And the contents are back to their original places.
-
Hi @Vadi2 , can you share your latest complete code, because for me its working fine.
-
Here it is: https://transfer.sh/F5ceH/dodgy-scrollview.zip
-
Your code only from the above post.
-
Hi @Vadi2 , you need to specify height and width to the Row.
I have specified this and it works fine for me:-
width: hammerLabel.width height: hammerLabel.height
And one more suggestion, when you enter the text you reparent it to the ScrollView and once it gets reparented to the ScrollView you need to again click on the TextArea to edit the Text, if you want to continue to edit the TextArea even when you reparent it , you can use the onParentChanged signal. So instead of using onWidthChanged, you can use onParentChanged like this:
onParentChanged: textArea.forceActiveFocus()
-
@Shrinidhi-Upadhyaya Cool.
@Vadi2 Try this. -
Hm, it kind of works - but I want the Row to auto-size the the contents of its children, I don't want to force it into an explicit width/height.
Also the TextArea seems to lose it's border? You can see it in https://ddgobkiprc33d.cloudfront.net/8b195d8c-bd58-4dc1-ba36-9c3a10e7aba2.png :(
Thanks for all the help so far, really appreciate it.
-
Hi @Vadi2
Is this what you expected ?
-
So close!
The textArea's height would be
window.height - buttonsRow.height
- that is, not overlap the buttons below. It should also only resize on the presence of text, not via the ☰ button (it's actually a settings button).How did you manage to get it to size back down okay?
-
Hi @Vadi2 , i guess there is some problem with ScrollView.
1. Problem 1 (Sample Code)
TextArea { id: textArea height: 100 width: 100 anchors.left: view.right anchors.leftMargin: 100 } ScrollView { id: view height: 100 width: 100 clip: true TextArea { id: textArea1 width: parent.width //####Uncomment the below line the,there will be no border line wrapMode: "WrapAtWordBoundaryOrAnywhere" } }
I dont knoe how but TextArea inside the ScrolllView behaves weird, if you set wrapMode property, the border does not appear, but if dont set it ,border appears but does not take width specified.
2. Problem 2 (Sample Code)
ScrollView { id: view height: 100 width: 100 clip: true } Rectangle { id: redRect height: 100 width: 100 color: "transparent" border.color: "red" anchors.centerIn: parent TextArea { id: textArea width: parent.width parent: text ? view.contentItem.contentItem : redRect onParentChanged: { textArea.forceActiveFocus() } } }
here if you enter the text , you will see that TextArea changes its parent to ScrollView,after that if you delete the text,it should come back to Rectangle, but you will see that borderLine of TextArea remains in the ScrollView,but the text comes back to Rectangle(Weird)
So as @GrecKo suggested the best solution would be to use Flickable:-
Here is the sample code:-
Flickable { id: view height: 100 width: 200 clip: true flickableDirection: Flickable.VerticalFlick contentHeight: textArea.contentHeight contentWidth: 200 } Rectangle { id: redRect height: 100 width: 100 color: "red" anchors.centerIn: parent TextArea { id: textArea width: parent.width wrapMode: "WrapAtWordBoundaryOrAnywhere" anchors.centerIn: parent parent: text?view.contentItem:redRect } }
-
@Vadi2
Yes, Height should be window height - row height from which you can avoid the overlap.Just remove the
textArea.state = "MINIMAL"
from Button clicked to avoid the behavior (I did just to test the code)
I Used the Flickable { } in place of ScrollView {}. If you want the ScrollBar you can add it to the Flickable {}
Also now parent will change as its Flickable {} directly
State { name: "EXPANDED"; when: textArea.text ParentChange { target: textArea parent: addResourceScrollView.contentItem x: 0; y: 0 width: window.width height: window.height } }
And also in this case you don't have to set the width & height for the Row {}
-
@Vadi2
There is an explanation from @Shrinidhi-Upadhyaya with sample code.
You can check the code once by using his examples.Below is height fix for your TextArea