On Fri, Mar 17, 2017 at 10:15 PM Rémi Deletrain <[email protected]>
wrote:

> Hello everyone, I have a little problem with the PySide QSlider. I'm
> trying to draw text on the ticks of my QSlider. I saw that we could use
> "sliderValueFromPosition" to find the position but impossible to make it
> work correctly ... It always returns 0. I'll show you my code.
>
> def __range_to_pos(self, value):
>
>     qt_options = QtGui.QStyleOptionSlider()
>     self.initStyleOption(qt_options)
>     style = QtGui.QApplication.style()
>
>     gr = style.subControlRect(style.CC_Slider, qt_options, style.
> SC_SliderGroove, self)
>     sr = style.subControlRect(style.CC_Slider, qt_options, style.
> SC_SliderHandle, self)
>
>     if self.orientation() == QtCore.Qt.Horizontal:
>         slider_length = sr.width()
>         slider_min = gr.x()
>         slider_max = gr.right() - slider_length + 1
>     else:
>         slider_length = sr.height()
>         slider_min = gr.y()
>         slider_max = gr.bottom() - slider_length + 1
>
>     return style.sliderValueFromPosition(
>         self.minimum(),
>         self.maximum(),
>         value,
>         slider_max - slider_min,
>         qt_options.upsideDown
>     )
>
>
>
Can you provide a small working reproduction of your problem? It was not
clear to me what you were passing for "value" because the name of the
function is misleading. It is called "range to pos", yet the value
parameter should mean the pixel position and the return value should be the
slider value.

A quick test seems to be working:

from PySide import QtCore, QtGui

slider = QtGui.QSlider()
slider.setRange(0,100)
slider.resize(200,30)
slider.setOrientation(QtCore.Qt.Horizontal)
slider.show()
slider.raise_()

qt_options = QtGui.QStyleOptionSlider()
slider.initStyleOption(qt_options)
style = QtGui.QApplication.style()

gr = style.subControlRect(style.CC_Slider, qt_options,
style.SC_SliderGroove, slider)
sr = style.subControlRect(style.CC_Slider, qt_options,
style.SC_SliderHandle, slider)
if slider.orientation() == QtCore.Qt.Horizontal:
    slider_length = sr.width()
    slider_min = gr.x()
    slider_max = gr.right() - slider_length + 1else:
    slider_length = sr.height()
    slider_min = gr.y()
    slider_max = gr.bottom() - slider_length + 1

pos = 100

ret = style.sliderValueFromPosition(
    slider.minimum(),
    slider.maximum(),
    pos,
    slider_max - slider_min,
    qt_options.upsideDown
)print ret# 53

​


> I made an alternative but I have a small lag on my offset.
> def __range_to_pos(self, value):
>
>     i_percent_interval = 100.0 / (self.maximum() - 1.0)
>
>     # Size from orientation
>     if self.orientation() == QtCore.Qt.Horizontal:
>         f_size = self.rect().width()
>     else:
>         f_size = self.rect().height()
>
>     #   Calculate offset
>     qt_font_metric = QtGui.QPainter(self).fontMetrics()
>     f_text_size = float(qt_font_metric.width(str(value)))
>     i_percent_offset = (f_text_size / f_size) * 100.0
>     f_offset = f_size / 100.0 * i_percent_offset
>
>     if value == 0:
>         f_offset = 0.0
>     elif value != self.maximum() - 1:
>         f_offset /= 2.0
>
>     #   Position
>     x_pos = f_size / 100.0 * (i_percent_interval * value)
>     if self.orientation() == QtCore.Qt.Horizontal:
>         qt_pos = QtCore.QPoint(x_pos - f_offset, self.rect().bottom())
>     else:
>         qt_pos = QtCore.QPoint(self.rect().bottom(), x_pos - f_offset)
>
>     return qt_pos
>
> I find it a pity not to be able to operate the command of PySide. Does
> anyone know the solution?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/9e2f012c-0fad-4687-81e6-d6561c2bdfed%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/9e2f012c-0fad-4687-81e6-d6561c2bdfed%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2u1hqz51_qsPw-Cfd%3Dsr7%2B4V5DRnSxBJn8Oetwn55BZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to