qml class map cpp class - KerwinKoo/KerwinKoo.github.io GitHub Wiki

Qt5.4 QML 类型对应的 C++ 类型

使用 Component.onCompleted 附加信号,在附加信号处理器中输出类型信息即可。示例代码:

import QtQuick 2.0  
import QtQuick.Controls 1.1  
  
Rectangle {  
    width: 320;  
    height: 240;  
    color: "gray";  
      
    Text {  
        id: text1;  
        anchors.centerIn: parent;  
        text: "Hello World!";  
        color: "blue";  
        font.pixelSize: 32;  
    }  
      
    Button {  
        id: button1;  
        text: "A Button";  
        anchors.top: text1.bottom;  
        anchors.topMargin: 4;  
    }  
      
    Image {  
        id: image1;  
    }  
      
    Component.onCompleted: {  
        console.log("QML Text\'s C++ type - ", text1);  
        console.log("QML Button\'s C++ type - ", button1);  
        console.log("QML Image\'s C++ type - ", image1);  
    }  
}  

调试信息输出如下:

QML debugging is enabled. Only use this in a safe environment.
qml: QML Text's C++ type -  QQuickText(0xb02600)
qml: QML Button's C++ type -  Button_QMLTYPE_35(0xc8e6d0)
qml: QML Image's C++ type -  QQuickImage(0xc0b380)
⚠️ **GitHub.com Fallback** ⚠️