QML 문자열 및 숫자 변환

숫자로 변환 된 문자열

QML 코드에서 문자열을 숫자로 만나면 Number (str)를 사용하여 str을 숫자 유형으로 변환 할 수 있습니다.

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    
    
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    property string testStr: "-100000"
    property int testInt: 44
    property double testDouble: 11.11
    property string testDoubleStr: "22.34432"
    Text {
    
    
        id: intTxt
        text: testInt * 2
        anchors.top: parent.top
        anchors.left: parent.left
        width: parent.width
        height: 20
    }
    Text {
    
    
        id: doubleTxt
        text: testDouble
        anchors.top: intTxt.bottom
        anchors.left: parent.left
        width: parent.width
        height: 20

    }
    MouseArea {
    
    
        anchors.fill: parent
        onClicked: {
    
    
            testInt = Number(testStr)
            testDouble = Number(testDoubleStr)
        }
    }
}


여기에 사진 설명 삽입
독립 실행 형 컴퓨터를 실행하면 다음 과 같이됩니다.
여기에 사진 설명 삽입

숫자는 문자열로 변환됩니다.

QML 코드에서 숫자를 문자열로 만난 경우 num.toString ()을 사용하여 숫자를 문자열 유형으로 변환 할 수 있습니다.

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    
    
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    property string testStr: "-100000"
    property int testInt: 44
    property double testDouble: 11.11
    property string testDoubleStr: "22.34432"
    Text {
    
    
        id: intTxt
        text: testStr
        anchors.top: parent.top
        anchors.topMargin: 40
        anchors.left: parent.left
        width: parent.width
        horizontalAlignment: Text.AlignHCenter
        height: 20
    }
    Text {
    
    
        id: doubleTxt
        text: testDoubleStr
        anchors.top: intTxt.bottom
        anchors.left: parent.left
        width: parent.width
        horizontalAlignment: Text.AlignHCenter
        height: 20

    }
    MouseArea {
    
    
        anchors.fill: parent
        onClicked: {
    
    
            testStr = testInt.toString()
            testDoubleStr = testDouble.toString()
        }
    }
}

운영
여기에 사진 설명 삽입

독립 실행 형 후에는 다음과 같이됩니다.
여기에 사진 설명 삽입

참고 기사 :
[1] QML에서 JavaScript 사용에 대한 자세한 설명 (1) ----- qml에서 문자열 유형 데이터를 정수 데이터로 변환
[2] 주소 선택, 파일 이름 가져 오기, 잘라내 기에 대한 QML의 작업

추천

출처blog.csdn.net/PRML_MAN/article/details/113371342