简体   繁体   English

PyQt / PySide获取QSpinBOX中所选值的小数位

[英]PyQt/PySide Get Decimal Place of selected value in QSpinBOX

How to query Decimal Place of selected value in QSpinBox(PyQt) ? 如何在QSpinBox(PyQt)中查询所选值的小数位?

Let say QSpinBox contain value 631.1205 and user select 3 then answer should be tens.(ie 6=Hundreds, 3=Tens, 1=Ones, 1=Tenths, 2=Hundredths, 0=Thousandths, 5=Ten-Thousandths). 假设QSpinBox包含值631.1205,并且用户选择3,则答案应该是十。(即6 =百,3 =十,1 =千,1 =十,2 =百,0 =千,5 =十万)。

Let's assume you have two lists that contain the text you want to return (expand beyond thousand*s if appropriate for your use case): big = ['Ones', 'Tens', Hundreds', 'Thousands'] small = ['Tenths', 'Hundredths', 'Thousandths'] 假设您有两个包含要返回的文本的列表(如果适合您的用例,则扩展到超过1000 * s):big = ['Ones','Tens',Hundreds','Thousands'] small = [十分之几”,“百分之几”,“千分之几”]

First you need to get a reference to the QLineEdit in the QSpinBox with: 首先,您需要使用以下命令在QSpinBox获得对QLineEdit的引用:

line_edit = spinBox.findChild(QLineEdit)

Then find the index of where the selection starts: 然后找到选择开始的索引:

selectionStart = line_edit.selectionStart()

Next find the index of the decimal point: 接下来找到小数点的索引:

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()))

Then work out what you need to return: 然后计算出您需要返回的内容:

# 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]

I'd recommend putting this in a function and calling it when appropriate as I have no context of what you are trying to do! 我建议将其放在函数中,并在适当时调用它,因为我没有上下文要尝试做的事情!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM