Open hankadler opened 1 year ago
Here's how I'm solving this at the moment:
// utils/filters.ts
export function filterFloat(text: string): string {
return text
.replace(/[^\d.-]/g, "")
.replace(/(?<=\..*)\./g, "")
.replace(/(?<=-.*)-/g, "");
}
export function filterUnsignedFloat(text: string): string {
return text
.replace(/[^\d.]/g, "")
.replace(/(?<=\..*)\./g, "");
}
// areaCalculator.ts
// ...
import { filterUnsignedFloat } from "../utils/filters";
// ...
const widthLineEdit = new QLineEdit();
widthLineEdit.setPlaceholderText("0.0");
widthLineEdit.addEventListener(
"textChanged", (text) => widthLineEdit.setText(filterUnsignedFloat(text))
);
It'd be great if widgets that accept user input like
QLineEdit
instances implemented asetValidator
method that accepted a function that prohibits input for which it evaluates to false.For an example of how this is done in PyQt see here.
In JavaScript nodegui it could look something like this: