[英]C++ QQuickItem: How to trigger a function on item size change
我在C ++中有一個QQuickItem,它在QML應用程序中實例化。 如果我的QQuickItem的width屬性更改,我想在c ++端觸發自定義函數。
在QML中,我將執行onWidthChanged:{我的代碼}。 我該如何在c ++方面做到這一點?
如果要在自定義QQuickItem
響應大小更改,請重新實現geometryChanged()
函數。 這樣的好處是不必為每個項目創建額外的信號槽連接 ,如果要連接到widthChanged()
/ heightChanged()
信號,則widthChanged()
必要。
這是在Qt Quick Controls 2中重新實現的示例。
width
是qproperty
具有widthChanged
信號,這樣就可以將該信號連接到某些時隙。
class FooItem : public QQuickItem
{
Q_OBJECT
public:
FooItem(QQuickItem *parent=Q_NULLPTR): QQuickItem(parent){
connect(this, &FooItem::widthChanged, this, &FooItem::onWidthChanged);
}
Q_SLOT void onWidthChanged(){
qDebug()<<width();
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.