Restricting allowed values in GtkScale

Hi there!

In my application (using gtkmm-4.0), I’d like to restrict the values of a GtkScale such that only odd numbers can be entered. So far I’ve checked the docs and found the “step-increment” property on GtkAdjustment – but setting this property doesn’t seem to change the behaviour of the GtkScale

Here’s the relevant snippet of my XML:

...
<object class="GtkScale">
    <property name="round-digits">0</property>
    <property name="draw-value">1</property>
    <property name="adjustment">
        <object class="GtkAdjustment">
            <property name="lower">1</property>
            <property name="upper">99</property>
            <property name="step-increment">2</property>
            <property name="value">11</property>
        </object>
    </property>
</object>
...

I could probably subclass GtkScale and implement the desired behaviour, but surely there’s a way to do this with GtkScale/GtkAdjustment already?

Another thing - with “draw-value” set, the GtkScale shows its current value above the slider. Is there a way to show this as an integer (ie. with no decimal point)? Having <property name="round-digits">0</property> does seem to work in that only integer values are shown, but the decimal point is still there.

Any help is greatly appreciated!

I ended up scrapping the idea of using a GtkScale, in favour of using a GtkSpinButton. Looking back, it seems that GtkScale was a poor choice of widget for my purposes anyway.

That being said, I did figure out a workaround before switching to GtkSpinButton:
Hook into the “value-changed” signal, and grab the value of the scale. Convert the value to an integer. If the value is even, add 1 and write it back to the scale.
Important to note however, if you want to display the scale’s value above the scale, you’re still stuck with the trailing ‘.0’ - I never figured out how to change this behaviour

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.