LineEdit - Numeric Only - Restricted Range

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By plambrecht

I am using a LineEdit node for a numerical input (have that figured out), but also need to restrict how high or low the number is. If they enter a number above the range, it should display the highest number possible, if lower, then the opposite. Kind of like what “clamp” does, but I can’t seem to get clamp to work in my code.

I am using the following code:

extends LineEdit
var regex = RegEx.new()
var oldtext = ""
func _ready():
    regex.compile("^[0-9]*$")
func _on_LineEdit_QTY_text_changed(new_text):
    if regex.search(new_text):
	    text = new_text   
	    oldtext = text
    else:
	    text = oldtext
	    set_cursor_position(text.length())
func get_value():
    return(int(text))

Any help is greatly appreciated.

Can’t you convert the string into an integer and clamp it using clamp(value, min, max)? I think it should work.

Bubu | 2021-04-16 14:11

:bust_in_silhouette: Reply From: exuin

Why not use a SpinBox? It has that functionality already.

But if you want to use a LineEdit yeah just use Bubu’s answer.

I am using a modified LineEdit because you cannot modify the font size in a SpinBox.

plambrecht | 2021-04-16 15:58

You can. If you check the Remote tab, you can see that the SpinBox has a LineEdit inside of it. Just change the font size of the LineEdit in the Theme.

exuin | 2021-04-16 16:20

Thanks for this tip. However, when I try to assign a font (switch to New dynamic font), it immediately switches back to [empty]. Same for any of the other custom styles.

plambrecht | 2021-04-16 17:16

Really? That’s strange, it shouldn’t do that

exuin | 2021-04-16 17:21

Was finally able to get the theme working, but now I have the issue that when a larger font is used, it expands the width of the SpinBox and you can’t adjust it smaller.

plambrecht | 2021-04-16 19:35

You can try adjusting the top and bottom spacing of the font but yeah the font sets the min size

exuin | 2021-04-16 19:36