QT qml---- loader使用方法

“简洁是智慧的灵魂,冗长是肤浅的藻饰”------------------《哈姆莱特》莎士比亚


Import Statement: import QtQuick 2.5

Loader is used to dynamically load QML components.

Loader can load a QML file (using the source property) or a Component object (using the sourceComponent property). It is useful for delaying the creation of a component until it is required: for example, when a component should be created on demand, or when a component should not be created unnecessarily for performance reasons.

The loaded object can be accessed using the item property.

If the source or sourceComponent changes, any previously instantiated items are destroyed. Setting source to an empty string or setting sourceComponent to undefined destroys the currently loaded object, freeing resources and leaving the Loader empty.

详细见QT文档。

小栗子:

import QtQuick 2.4
Item{

	Rectangle{//Rectangle内显示的内容由loader决定
	
            Loader{
                id : viewPanel
                anchors.fill: parent

            }

        }
		
	MouseArea {//当有信号来时,才load文件,生成实例
        anchors.fill: parent
        onClicked: {
                viewPanel.source = "selfDefined.qml" //自定义qml类型
                viewPanel.item.x= lineEdit1.x   //------The loaded object can be accessed using the item property
                viewPanel.item.y= lineEdit1.y+lineEdit1.height+2
                viewPanel.item.state="active"

                }
            }

}

猜你喜欢

转载自blog.csdn.net/qq_35865125/article/details/80347995