Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm making an app with the Ubuntu SDK where a user has to enter a number as fast as possible. I have a textfield in a page, where he has to enter the number:

TextField {
    id: inputQuestion
    width: 100
    height: 50
    errorHighlight: true
    validator: IntValidator { }
    text: '?'
}

I have two questions about that:

  1. How can I make it so the TextField is automatically selected when the page opens? (the cursor is in the textbox)
  2. How can I make it so the numpad of the smartphone/tablet automatically opens when the page opens?
share|improve this question

1 Answer 1

up vote 2 down vote accepted

It is quite simple to do the above. To set automatic focus to the TextField component, you can call forceActiveFocus() from within your TextField component as shown below,

Component.onCompleted: {
    forceActiveFocus()
}

This sets the focus to the textfield immediately after it is loaded.

As for showing the numpad, you can do so by setting the input hint to numbers only,

inputMethodHints: Qt.ImhDigitsOnly

Your TextField should look like,

TextField {
    id: inputQuestion

    Component.onCompleted: {
        forceActiveFocus()
    }

    width: units.gu(5)
    height: units.gu(2)

    errorHighlight: true
    validator: IntValidator { }
    text: '?'
    inputMethodHints: Qt.ImhDigitsOnly
}

Note: Do not set the width and height in pixels. Use grid units as shown in the example above since that will ensure that the textfield has the same dimensions on all devices regardless of their pixel density.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.