1

Is it is possible QQuickItem layout change?

Is there a way to trigger function when layout/size ( x, y, width or hight) is changed?

There is way to connect on every propery signal change but this is not right way.

Already tried windowChanged signal but it doesn't work for this.

What I am trying to do is to call extrenal java function providing x(GLOBAL),y(GLOBAL),width,height every time my QQuickItem layout/size (any of these) change.

edit: I need to know the geometry after rendering is done. So I cannot connect to every property by itself.

Luka
  • 472
  • 4
  • 18
  • Take a look at this, maybe it helps [is-there-a-window-resize-event-signal](https://godotengine.org/qa/14009/is-there-a-window-resize-event-signal) it may help this function : `void QQuickItem::windowChanged(QQuickWindow *window)` [#windowChanged](http://doc.qt.io/qt-5/qquickitem.html#windowChanged) i never used it, so i cannot tell what behaviour have but from what i searched it's ok, signal is sent when item is resized. – Mara Black May 15 '18 at 12:02
  • @Skywrath Thanks, tried that one, but i never get that signal. – Luka May 15 '18 at 12:04
  • Hmm, then try to put QQuickItem into a container and check resize at parent? Or another bad idea: make a function that takes width and height and call it every time you make an action (press a button or something that you know that it will change the size ), if your function return different result then the original one, do what you wanted to do when resize was triggered. xD – Mara Black May 15 '18 at 12:20
  • 3
    Possible duplicate of [C++ QQuickItem: How to trigger a function on item size change](https://stackoverflow.com/questions/49667771/c-qquickitem-how-to-trigger-a-function-on-item-size-change) – Mitch May 15 '18 at 12:25
  • are you subclassing `QQuickItem` yourself? – GrecKo May 15 '18 at 12:25
  • I think it would be worth differentiating between signals and functions in your question. It seems that you don't want to connect to signals, so it's probably worth editing your question to say "function" instead. – Mitch May 15 '18 at 12:27
  • The thing is, I need to call external java function providing x,y,width,height every time my QQuickItem layout/size changes. – Luka May 15 '18 at 12:29
  • @Mitch i need to know the geometry after rendering is done. So I cannot connect to every property by itself. – Luka May 15 '18 at 12:38
  • 1. I don't understand why you can't connect to the changed signals independently. 2. Why do you need the geometry after the Item has been rendered? Do you expect the geometry to change in between the signal and the rendering? By what cause? When you have tried windowChanged... Is there something about a window that we should know about, to help you? – derM - not here for BOT dreams May 15 '18 at 15:52
  • @skywrath: why should the window change if an Item is resized? – derM - not here for BOT dreams May 15 '18 at 15:55
  • 1
    @derM i had something else in mind, but he edited the post so my solution is useless now xD – Mara Black May 17 '18 at 07:58
  • @Skywarth: The question is very confusing. – derM - not here for BOT dreams May 17 '18 at 08:19

1 Answers1

2

Connect each property to a single function which checks the "geometry" as compared to some saved variable, and if something doesnt match up, emit a new signal

Item {
   id: myItem
   onXChanged: { myItem.checkGeometry() }
   onYChanged: { myItem.checkGeometry() }
   onWidthChanged: { myItem.checkGeometry() }
   /* etc etc */

   signal: geometryChanged(var _x, var _y, var _width, var _height)

   property var  oldGeometry: [ myItem.x, myItem.y, myItem.width, myItem.height]
   function checkGeometry() { 
     var geometryToCheck = [mapToGlobal(myItem.x), mapToGlobal(myItem.y), myItem.width, myItem.height];
     for (var i=0; i<oldGeometry.length; i++) {
          if (oldGeometry[i] !== geometryToCheck[i]) {
               myItem.geometryChanged(mapToGlobal(myItem.x), mapToGlobal(myItem.y), myItem.width, myItem.height);
               myItem.oldGeometry = geometryToCheck;
               break;
          }
     }
   } 
mike510a
  • 2,102
  • 1
  • 11
  • 27