2013-09-27 39 views
0

的代碼創建兩個輸入文本框:如何在QtQuick中將焦點從一個文本框移到另一個文本框?

import QtQuick 1.0 

Column 
{ 
    width: 500 
    height: 200 

    Text 
    { 
    id: userInfoTextA 
    focus: false 
    text: "Enter a value from 0 to 10:" 
    } 

    TextInput 
    { 
    id: userInputA 
    focus: true 

    anchors.left : userInfoTextA.right 
    anchors.leftMargin : 20 

    validator: IntValidator { bottom:0; top: 20 } 

    Keys.onReturnPressed: 
    { 
     console.log (userInputA.text) 
    } 
    } 

    Text 
    { 
    id: userInfoTextB 
    focus: false 
    text: "Enter a value from 10 to 20:" 
    } 

    TextInput 
    { 
    id: userInputB 
    focus: true 

    anchors.left : userInfoTextB.right 
    anchors.leftMargin : 20 

    validator: IntValidator { bottom:0; top: 20 } 

    Keys.onReturnPressed: 
    { 
     console.log (userInputB.text) 
    } 
    } 
} 

在輸出窗口中的焦點,在默認情況下,設置文本框A. 我應該如何通過移動焦點到文本框中B:
1.鍵盤
2.鼠標

回答

1

因此,大量的小東西:

1)當你用鼠標點擊它應該已經轉移焦點;無需擔心這一點。

2)你的佈局在列格式困難,默認情況下將TextInput沒有寬度,這使得它很難使用

3)真正的答案:您可以將焦點設置在項目上進行切換它(見下文)

4)但是,您也應該使用KeyNavigation系統,也可以在下面的示例中看到,因爲人們習慣於使用Tab/Shift-Tab切換項目。

所以,這裏的一些修改示例代碼,你想要做什麼:

import QtQuick 1.0 

Grid 
{ 
    width: 500 
    height: 200 
    columns: 2 

    Text 
    { 
    id: userInfoTextA 
    text: "Enter a value from 0 to 10:" 
    width: 200 
    } 

    TextInput 
    { 
    id: userInputA 
    focus: true 
    width: 100 

    anchors.left : userInfoTextA.right 
    anchors.leftMargin : 20 

    validator: IntValidator { bottom:0; top: 20 } 

    KeyNavigation.priority: KeyNavigation.BeforeItem 
    KeyNavigation.tab: userInputB 

    Keys.onReturnPressed: 
    { 
     console.log (userInputA.text) 
     userInputA.focus = false 
     userInputB.focus = true 
     event.accepted = true; 
    } 
    } 

    Text 
    { 
    id: userInfoTextB 
    text: "Enter a value from 10 to 20:" 
    width: 200 
    } 

    TextInput 
    { 
    id: userInputB 
    width: 100 

    anchors.left : userInfoTextB.right 
    anchors.leftMargin : 20 

    validator: IntValidator { bottom:0; top: 20 } 

    KeyNavigation.priority: KeyNavigation.BeforeItem 
    KeyNavigation.backtab: userInputA 

    Keys.onReturnPressed: 
    { 
     console.log (userInputB.text) 
    } 
    } 
} 
+0

你說:'當你用鼠標點擊它應該已經轉移焦點;不需要擔心這一點。「實際上,我在那裏看不到一個文本框(用我的代碼),所以我不知道在哪裏點擊。 –

相關問題