Home > OS >  How to get notified when a QML item's dimensions change?
How to get notified when a QML item's dimensions change?

Time:12-01

I understand that I can get to know if the width or height of a QML item changed using the slots onWidthChanged and onHeightChanged. This is by doing something like below.

import QtQuick 2.12

Item {
    id: my_item

    onWidthChanged: {
        if (my_item.visible) {
            console.log("Dimension chnaged")
        }
    }

    onHeightChanged: {
        if (my_item.visible) {
            console.log("Dimension chnaged")
        }
    }
}

Above works well. But I am only interested to know if the dimension of my QML item changed. I just need a callback when width or the height changed. I don't need a callback for both.
Is there a QML signal to listen only for a dimension change?

I am using Qt 5.15.7 commercial version.

CodePudding user response:

As a workaround, you can create a property which is bound to both width and height and connect a handler to the changed signal of that property:

property double dimensions: width * height
onDimensionsChanged: {
   if(my_item.visible)
       console.log("Dimension changed")
}

There is a small risk, namely the number won't change when swapping width & height, but that might be worth it in your situation

  • Related