【问题标题】:How do I assign an TextInput to an int, in qml?如何在 qml 中将 TextInput 分配给 int?
【发布时间】:2011-09-26 21:09:21
【问题描述】:

如何在 qml 中将 TextInput 分配给 int?

int new_span_seconds

TextInput {
        id: editor
        width: 80
        height: 17
        color: "white"
        font.bold: true; font.pixelSize: 14
        text: "21"
        horizontalAlignment: TextInput.AlignHCenter

    }

    Keys.forwardTo: [ (returnKey), (editor)]

    Item {
        id: returnKey
        Keys.onReturnPressed: new_span_seconds = editor. <<< ?  >>>
        Keys.onEnterPressed:  new_span_seconds = editor. <<< ?  >>>
    }

【问题讨论】:

    标签: qt qml textinput qt-quick


    【解决方案1】:

    这只是一段 Javascript

    Keys.onReturnPressed: new_span_seconds = parseInt(editor.text)
    

    【讨论】:

      【解决方案2】:

      利用 js 的一个 jenky 部分,它允许您通过“相乘”将字符串转换为整数。前任let foo = "2"; let bar = foo * 1.

      以下是应用于您提供的特定代码时的外观:

      TextInput {
              id: editor
              width: 80
              height: 17
              color: "white"
              font.bold: true; font.pixelSize: 14
              text: "21"
              horizontalAlignment: TextInput.AlignHCenter
      
          }
      
          Keys.forwardTo: [ (returnKey), (editor)]
      
          Item {
              id: returnKey
              Keys.onReturnPressed: new_span_seconds = editor.text * 1
              Keys.onEnterPressed:  new_span_seconds = editor.text * 1
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-15
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        • 2014-10-10
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多