Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] QSlider handle size after stylesheet



  • Is it possible to get the size of the handle of a QSlider if a stylesheet changed it?

    The following code seems only to work if nothing changed the handles size

    @
    QStyleOptionSlider opt;
    initStyleOption(&opt);
    QRect handle = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle);
    @


  • Lifetime Qt Champion

    Hi,

    You could parse the stylesheet for that information


  • Moderators

    [quote author="butterface" date="1389193865"]
    The following code seems only to work if nothing changed the handles size
    [/quote]
    why doesn't this code work for you?
    How does the output differ from what you expect?
    How does your stylesheet look like?



  • I don't know why.

    The output gives me a width of the handle of 15 but it should be 18.

    @
    QSlider::handle:horizontal {
    background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);
    border: 1px solid #0c457e;
    width: 18px;
    margin: -2px 0;
    border-radius: 3px;
    }
    @



  • @SGaist Of course this would be an option but I would prefer to ask the slider itself for its handle size.


  • Moderators

    seems that setting the handle size via stylesheet style isn't possible at all.
    This is a fixed pixel metric (QStyle::PM_SliderLength) value in the platform styles (at least on Windows).



  • That is not correct. width changes the size of the handle.


  • Moderators

    correct, my fault.
    I've just tested the style definition you posted, and this isn't working as it is (only when you also style the slider's groove i think)

    To come back to your problem:
    Your code is almost correct. Just keep in mind that every time you call QStyle methods and an optional QWidget parameter is available you need to pass it:
    @
    QStyleOptionSlider opt;
    initStyleOption(&opt);
    QRect handle = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
    @
    This will work.



  • I should have posted the whole slider style.

    The this indeed fixed the problem. Thanks!


Log in to reply