QGraphicsTextItem does not support this ability, as I am sure you discovered. Thus, you have several options:
- Reimplement
focusOutEvent(QFocusEvent* event) and / or keyReleaseEvent(QKeyEvent* event) to determine when to keyReleaseEvent(QKeyEvent* event) validator. A QValidator can be created as a member of your text class and requested either when you lose focus or when you press a key (the enter key means completion or for each letter). Then just create a custom waveform for you if you think the editing is complete or changed. - Use
GraphicsProxyWidget to store the βrealβ QLineEdit for text input, just set it using the validator, as if you placed it in the traditional form of the graphical interface. You will need to βforwardβ the editingFinished() or textEdited(const QString& text) signal from QLineEdit to QGraphicsTextItem , so you do not need to provide external access to the widget. - You can also use the internal
QTextDocument QGraphicsTextItem , this is what actually executes and formats the text (accessed via document() ). However, it does not support the installation of QValidator , so you will need to create a signal slot loop, as a result of which when changing the text (with contentsChanged() message) it will be received by QGraphicsTextItem , checked, or updated / cleared if it does not pass the check (which will update QTextDocument and starts this process again) or will be ignored if it passes.
It is also difficult to implement; the first requires more code, but will give you more control over the visual appearance of the text field.
source share