0047 – SpinButton
This one’s both easy and tricky. Let me explain…
Having a gander at the first example, you’ll see that the only new-ish bit is this:
One thing to notice here is that the
SpinButton doesn’t work alone. It needs…
Adjustment isn’t a
Widget because it’s derived directly from
ObjectG. This means that it doesn’t have a visual presence, but it does give you control over the range and behaviour of the
Adjustment also helps out a bunch of other
Widgets including (but not limited to):
Containerand its offspring:
ScaleButton, and its child…
In short, anything that needs adjusting. And the
Adjustment is also the most complicated part of setting up and using a
At the top of the
MySpinButton class definition, there’s a bunch of stuff initialized for use when instantiating the
Adjustment, and as long as you keep these values sane, you’ll have very little trouble. These values are:
maximum– straightforward, these are the upper and lower limits of the spinner,
step– the increment added to or subtracted from the current value each time the spinner buttons are clicked… or—while the
SpinButtonhas focus—each time the up and down arrow keys are pressed,
initialValue– again straightforward, and then there’s…
pageIncrement– how much is added to or subtracted from the spinner’s current value when you hit the Page-up or Page-down keys, and
pageSize… Now, this is an odd one…
Oddity of the pageSize Argument
pageSize is best set to ‘0.’
Variations on a SpinButton – Floating Point Values
In our second example, you’ll find (among others) the
The objective here is to use and show floating point values. But, notice that all the initialized parameters for the
Adjustment are floats except for
step. This is because of an oddity in the
Adjustment object that seems to take two different forms, but rather than bore you with a long explanation, I’ll just give you the short version and jump right to the workaround for the current use-case…
SpinButton has two signals you can hook up to:
And there are a bunch of sane variable types you can use for the values:
- you can even fake a Boolean by limiting the callback to checking for 0 or 1 only.
For some reason, when combining certain variable types with a certain one or the other of the signals, clicking on the adjustment buttons will cause the signal to fire twice.
Note: This will also happen if you change the current value by typing into the in-built
Entry and hitting Enter. It won’t double-fire then, but it will next time you click on one of the adjustment buttons. At present, I know of no way to avoid this, so if this behaviour will be noticable by your users, you may want to let them know so you don’t field a whole raft of bug reports on it.
Troubleshooting a SpinButton
So, rule of thumb for using the
SpinButton or any other widget using the
- If you’re getting double signal firings with the
onValueChangedsignal, switch to the
onOutputsignal… and vice versa.
- If switching signals doesn’t help, try a different variable type (
Now have a look at the first chunk of the
In the initialization section, we’ve got another variable:
It’s used in the constructor with
setDigits() to set the number of decimal places to show in the spinner.
Now you know how to tame the
Everything but the Kitchen Sink
And just for fun, I’ve included this third example with
Byte-, and that fake
BoolSpinButtons I mentioned earlier, just to show how versatile this widget can be.
Note: The FloatSpinButton example in this file falls victim to GTK’s rounding error and so my recommendation is: don’t use the SpinButton for floating point values unless you’re prepared to deal with the rounding error yourself.
That’s it for this time around. Have a great day.
If you'd like to leave a comment...
Until I get time to research and install a commenting system, I ask that you try one of these options:
- come on over to the D Language Forum and look for one of the gtkDcoding announcement posts,
- drop by the gtkD Forum,
- follow the link below to email me, or
- go to the gtkDcoding Facebook page.
You can also subscribe via RSS so you won't miss anything.
© Copyright 2019 Ron Tarrant