1

I have a Qt app written against Qt 5.15.8. I have QML item declared on it. I know that following is a way I can figure out when my QML item is resized. Basically I get to know when width or height is changed.

Item {
    id: my_item

    property double dimensions: width * height
    onDimensionsChanged: {
       if(my_item.visible)
           console.log("Dimension changed")
           // Some heavy processing logic to run which I want to run if dimension change is complete.
    }
}

Question:
Is there a way I can get to know when the width or height or dimension change is finished or stopped? Due to reasons internal to my code, I have to do a heavy processing when the size of my QML item changes. I want to trigger the heavy processing when the size change is finished. Is there a way to figure out when the size change is finished?

If Qt/QML does not have a built in event, smart C++ or QML tricks are also welcome for the answer?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • Sounds like you want to debounce the signal. An approach along these lines should work for you: https://stackoverflow.com/a/31930209/1055722 – David K. Hess Jan 06 '22 at 15:23

1 Answers1

2

One thing that might help is to use Qt.callLater(). This is used to help reduce redundant calls to a function. Instead of calling your function directly, it will post an event. And if you use callLater() multiple times in a row, it's smart enough to still only call your function once. Try something like this:

Item {
    id: my_item

    property double dimensions: width * height
    onDimensionsChanged: {
       Qt.callLater(doHeavyProcessing);
    }

    function doHeavyProcessing() {
       // Some heavy processing logic to run which I want to run if dimension change is complete.
    }
}
JarMan
  • 7,589
  • 1
  • 10
  • 25