Hi. I'm Jesper Pederson from KDAB. If you just joined our video series now, welcome!
You might want to rewind and see some of the previous episodes where we were doing layout manually, because the topic of this video today is layout using anchor layouts. [Intro Music] In QML, we have four different ways of laying out. We've got three layout managers plus the manual one by positioning with x and y.
That's why I was almost saying three, because I got three layout managers and the manual positioning. We saw the manual positioning already. We saw the disadvantages in doing manual positioning, namely that you had to calculate the position based on the size and the position of other elements in your layout.
In reality, what we want is something at a higher level of the abstraction. That is what layout managers are. And we have three layout managers in QML.
We've got anchor layout that we're going to see in this and the next video. We've got positioners, which we'll see when we talk about repeaters, and so on. That's our column, and row, and grid.
And we got Qt Quick controls layout which is the third layout manager. In preparation for this video, I asked around in KDAB which layout managers they prefer and, trust me, this odd one out here, the fourth finger, nobody prefers to layout with x and y. Like I said, that's a the fallback position.
If the other layout managers cannot do exactly what you want, then you can always do it with setting the x and the y and the width and the height just as you need. But for most other purposes, you have one of those three and the the winner was 50/50, by default, prefer an anchor layout and, by default, prefer a Qt Quick controls layout. I'll let you choose which you prefer.
In the notes underneath here, I'll put in a link to the others two layout managers, so you can look at them right away. They might not be ready when you watch this, if you're an early adapter of our video series. But, stay tuned.
Subscribe to our channel. Then, you'll know the second they are up there. Layout using anchor layout works like this.
You have a number of lines on your element that you can layout to. You've got the the left line or the right line, the top, the bottom, the horizontalCenter or the verticalCenter. And you can take one element -- its right line, for example -- and glue that to the left line of another one.
So, they are glued together. If this one, for some reason, moves around, then, due to the glue, the other one will move with it. Here's some code.
I got two rectangles. I got my light blue, that's a bigger one here. And I've got my green sitting in there.
My light blue has a width and a height. My green has a height and a width. I don't know why it is opposite there, but nevermind.
And then it got a y position. What you'll notice is that it doesn't have an x position. The reason for that is that it says anchors.
right: background. right. The right side of this green rectangle is glued to the right side of the blue rectangle.
And if the blue rectangle moves around, it might be moving around on the screen but it might also be moving around inside its parent, if it's not at the top of the hierarchy. When that happens, the green will move around with it. There's one thing to notice in this example, namely that it says anchors.
right but it doesn't say background. anchors. right.
So, the anchors down here have decided it doesn't add any value to write background. anchors. right.
So, they made the QML interpreter have that understood. There's another example here. Let's see what that one does.
I got my rectangle again. The light blue is exactly as before. I have a y position here, just as before.
But this time, I say anchors. right is background. right.
So just as before, glue the right side and it says anchors. top: background. top.
So, not only am I gluing the right side of my blue rectangle to the right side of my green rectangle, I'm also gluing the top to the top -- the top part of these two elements. And you will see how we have a bit of a competition in here. Who's gonna win?
Is it y 25 that should be 25 pixels from the top, or is it the anchor? Well I'm not gonna bet any money on this because I can see on the screen who wins. The anchor does.
So, the anchor, as a result of doing the anchoring, will update the y. Let me just run this example in Qt Creator because I'd like to make a small modification to it. I'd like to make this code more like what I would see in projects.
So, we can see anchors. right, background. right, anchors.
top background. top. What I really would do in my project looks like this.
[types] What you see here is this combined composed elements, the anchors. right. I can write that simply anchors and then open brace so everything inside that brace means anchors.
and then so anchors. right, anchors. top.
It's potato potato. But whenever I read code, at least, I find it easier to just see I got color, I got y, I got height and width, I got anchor. I don't care about the anchors.
That's not what I'm looking at to understand in my code right now. So I'll just read from this brace, just jump straight down to this brace and I don't need to pass these two lines here. Here's another example.
This time, I'm not only changing the x and y position. If you look at the code, you'll see that I'm anchoring anchors. left: background.
left. , anchors. right: background.
right. So, what I'm basically saying is that my left side should be glued to the left, my right side to the right. And now, however my parent is changing, I'm glued.
And you can see it's not only x and y. Now it's also width that I'm changing. So, anchoring is both x and/or y and width and/or height.
So x, y, width, and height can be affected by anchoring. And in this example, x anchored up top. That means that I'm changing the y position.
Anchors. left and anchors. right means that I'm changing the x and that width.
Now, the height is the only one not anchored by these three lines of code, and that's what I have here in line 10, height: 50. I'm sure that you have seen one of those applications that suck, one of those applications that is not very good at getting enough space in between. And if you have seen one of those applications, then you'll notice that it's actually pretty hard for you to navigate because you think things that are close together belong together.
Things that are far apart you think of as two separate entities. So therefore, we want some space into our layout. We do that here with anchors.
So again, anchors. left, anchors. right.
So, anchor. leftMargin is saying my book up here is being anchored to the left side of the background. So the book is on the left side of the background.
And there is background. width divided by 16. Don't ask me why.
We have exactly that number. And there could have been as many as down here where it's just 32 pixels, unconditionally. But we have a bit of space in between here.
And similarly, in my text element, I anchor the left side of the text to the right side of the book, and I get 32 pixels in between, so 32 pixels sitting here. I also anchor the baseline. We didn't see that in the previous image.
So the baseline basically is we've got some text here. The letters WR and INT are standing on this, but G goes underneath that baseline. So I'm anchoring the baseline of the text to the vertical center of the book, which means that this line that goes underneath these letters is anchored to the vertical line of the book, meaning that the two are lined up.
Again, let me just scratch a bit of itch in this example here. To me, it's very noisy. Let me just fix up all that noise in my example here.
You can agree or disagree with me that the end result looks better. Doesn't that look easier to pass anchors and then left to background left, leftMargin to background and blah blah, and verticalCenter to background. verticalCenter?
At least I think it's easier. And when I read this example here again, it's purely for inclusion on the slides. I'm sure that somebody did it like this.
But I would like to have it look more like this here. Then I can see my image has an ID, the source of the image, anchoring. At this point in my mental process of understanding what's going on here, I don't really want to know details about the anchoring.
So, hey that's cool whatever we do in here. We have two special anchors. It's almost like an abuse of the anchor, because anchor anchors one thing to the other thing.
But we've got two special ones that are nevertheless very useful. One is called "centerIn" and the other is called "fill. " We see the "centerIn" on this slide here.
It says I have a rectangle here that's light blue. I've got another rectangle here that is green. You can see them on the application running here.
And its width and height are 50,50. So, I'm missing specifying the x and the y. For that, I say anchors.
centerIn: parent. You don't really need to understand anything else. You just need to listen to what is going on here.
It says you should center in parent. That's exactly what anchors. centerIn does.
It specifies an element and that element will be specifying that. Maybe this is actually the first time that we see parents. So let me just put a word on that one.
You can refer to the element's parent without an ID, simply by calling out the name "parent". So, when you look at the code here, it doesn't say parent anywhere here. It doesn't say ID: parent or anything like it.
It simply refers to that. So that was the "centerIn. " The other one is "fill.
" And you see this beautiful image that I have over here with two rectangles, blue rectangle that has a child that is a green rectangle. My blue rectangle has a given size and my green rectangle fills the parent with ten pixels of margins -- 10 pixels there, 10 pixels there, 10 pixels there, and 10 pixels there, which gives me this effect of a border or something. Okay, that's it for this video.
But please do not go and do anything else now. You want to see the next video. Trust me!
In the next video, I am gonna answer one question. I'm gonna solve a problem that I will bet a million, trillion dollars -- I'm not gonna take that bet with any of you. But I could bet a million, trillion dollars on that you will face that problem very soon after you start playing with anchoring.
The problem is called a binding loop. Stay tuned for the video. Until next time, have a great time.