PyQt/PySide Get Decimal Place of selected value in QSpinBOX
<p>How to query Decimal Place of selected value in QSpinBox(PyQt) ?</p>

<p>Let say QSpinBox contain value 631.1205 and user select 3 then answer should be tens.(i.e 6=Hundreds, 3=Tens, 1=Ones, 1=Tenths, 2=Hundredths, 0=Thousandths, 5=Ten-Thousandths).</p>

<p>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'] </p>

<p>First you need to get a reference to the <code>QLineEdit</code> in the <code>QSpinBox</code> with:</p>

<pre><code>line_edit = spinBox.findChild(QLineEdit)
</code></pre>

<p>Then find the index of where the selection starts:</p>

<pre><code>selectionStart = line_edit.selectionStart()
</code></pre>

<p>Next find the index of the decimal point:</p>

<pre><code>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()))
</code></pre>

<p>Then work out what you need to return:</p>

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

<p>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!</p>

python pyqt pyside