maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   SailfishOS (https://talk.maemo.org/forumdisplay.php?f=52)
-   -   QML question(s) (https://talk.maemo.org/showthread.php?t=100482)

pichlo 2018-09-28 15:53

QML question(s)
 
A QML newbie here. The title is deliberately vague to allow continuing in the future with different QML related questions in the same thread.

To start it off...

I have a layout that changes depending on some external parameter, for example a screen orientation.
For example:
Code:

Grid {
  id: myGrid
  ...

  Rectangle {
    id: myRectangle1
    ...
  }

  Rectangle {
    id: myRectangle2
    ...
  }

  Rectangle {
    id: myRectangle3
    ...
  }
}

I also have another element in a completely different module that I want to anchor to one of the elements in my layout, e.g. myRectangle3 in the above example. Remember it can move around.
How do I do it?

A simple and naive solution below does not work:
Code:

Rectangle {
  id: myOtherRectangle
  anchorItem: myGrid.myRectangle3
  ...
}

I can anchor to myGrid but not to myGrid.myRectangle3. I am sure there is a simple solution that I am missing. I tried RTFM but if anything, it did my head in even more.

Zeta 2018-09-28 17:36

Re: QML question(s)
 
I may be able to help, but need some more details.
you say that those two parts being in two "completely differents" modules. what kind of separation is there between them (parents, childrens, separate files....)

I am not sure where you found this anchoritem properties (no computer at hand...).
I usually use the bunch of anchors.* properties you can find in this doc : http://doc.qt.io/qt-5/qml-qtquick-item.html

there are several kind of anchors and dedicated properties for each. anchors.fill for exemple sets all sides to match another item, but you can also align only some sides or the vertical and horizontal center as you wish.
maybe explain what you need the anchored item do relative to the target (fill it, center in it, right align...).

coderus 2018-09-28 18:10

Re: QML question(s)
 
in grid:
Code:

property alias rect1: myRectangle1
in external rectangle:
Code:

anchor.left: grid.rect1.left

pichlo 2018-09-28 18:33

Re: QML question(s)
 
@Zeta,
myGrid and myOtherRectangle are siblings.
I found anchorItem in some other source where I took the inspiration from. I did not find it myself.

@coderus,
Thanks. I had tried something similar but that did not work either. Apparently you can only anchor to a parent or a sibling. myGrid is a siblibg, myGrid.rect1 is not.

Never mind, I think I figured it out. I need to refactor the code at a higher level. I could write more details when I get back to a PC.

pichlo 2018-09-28 20:59

Re: QML question(s)
 
Quote:

Originally Posted by pichlo (Post 1548977)
@coderus,
Thanks. I had tried something similar but that did not work either. Apparently you can only anchor to a parent or a sibling. myGrid is a siblibg, myGrid.rect1 is not.

For the peace of mind, I tried it exactly as suggested (my previous solution did not use 'property alias', it used 'property Rectangle'). But, sadly,
Code:

QML Notification: Cannot anchor to an item that isn't a parent or sibling.
Refactoring it is ;)

Markkyboy 2018-10-01 09:10

Re: QML question(s)
 
Quote:

Originally Posted by pichlo (Post 1548972)
.... I am sure there is a simple solution that I am missing. I tried RTFM but if anything, it did my head in even more.....

I hear that one, the examples set on many Qt/Qml pages are brief to say the least, or they plunge into great deal, causing those headaches and leaving me none the wiser. It strikes me that the authors of these pages assume you already have an understanding of how this stuff works, either way it's all mostly rather vague and what may work in Qt Creator is not always applicable to SFOS because of proprietary modules.


All times are GMT. The time now is 19:36.

vBulletin® Version 3.8.8