Python Forum

Full Version: Cancel update of text of QPlainTextEdit if terms not satisfied
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a QPlainTextEdit 's textChanged event connected to _criterion_txt_textChanged function.
I want the user not to able to enter a value grater than 2 and smaller than 1.
It can be int or decimal (float).
When terms are not satisfied, I want to set it's text to '1'.
(or maybe cancel update if possible)

This is what I've done, but not working.


def _criterion_txt_textChanged(self):
        if not re.match("\d*\.?\d*", criterion_txt.toPlainText()):
            msg =  "Only integers and decimals allowed, from 1 up to 2"
            print(msg)
            criterion_txt.setPlainText("1")
        try:
            text = float(criterion_txt.toPlainText())
        except:
            criterion_txt.setPlainText("1")
            msg = "Only integers and decimals allowed, from 1 up to 2"
            print(msg)
            
How can I achieve this?