2016-09-19 44 views
0

如何查詢在QSpinBox(PyQt)中選定值的小數點位置?假設QSpinBox包含值631.1205並且用戶選擇3,則答案應該是數十(即6 =數百,3 = Tens,1 = 1,1 =十分之一,2 =百分之一,0 =千分之一,5 =十分之一)千分之一)。PyQt/PySide在QSpinBOX中獲取所選值的小數點位置

+1

用戶如何選擇「3」?如在選擇旋轉框中的文本3?或者是否有下拉列表?或者是什麼?如果用戶選擇多個號碼會怎麼樣? –

+0

這是正常的QSpinBox(沒有任何下拉列表),用戶將通過鼠標選擇。如果用戶選擇更多的一位數字,結果將是較高的值(即631 =百位,31 =十位,205 =百位)。 – Ruchit

回答

0

假設您有兩個包含您想要返回的文本的列表(如果適合您的用例,請擴展至超過千* s): big = ['Ones','Tens',數百','數千' ] 小= [「十分之一」,「百分位」,「千」]

首先,你需要獲得與在QSpinBoxQLineEdit參考:

line_edit = spinBox.findChild(QLineEdit) 

然後找到其中的指數選擇開始:

selectionStart = line_edit.selectionStart() 

下一頁找到小數點指數:

try: 
    decimalPoint = str(line_edit.text()).index('.') 
except ValueError: 
    # no decimal point found so assume the number is an integer 
    decimalPoint = len(str(line_edit.text())) 

然後制定出你需要返回什麼:

# If something is selected 
if selectionStart > -1: 
    # if we have selected text starting on the left of the decimal 
    if selectionStart < decimalPoint: 
     return big[decimalPoint-selectionStart-1] 
    # else we selected text starting on the right of the decimal 
    else: 
     return small[selectionStart-decimalPoint-1] 

我建議把這個函數中,把它適當的時候因爲我沒有你想要做的事情!