Qt Editable QGraphicsTextItem validating text and emitting signal on change - qt

I am really stuck up with a task relating to Qt GraphicsView. Any help or suggestions will be highly appreciated.
In my QGraphicsView application, I have a few editable QGraphicsTextItems that I have added to the scene. I need the following functionality:
Setting validator for float so that the user does not by mistake enter a character or new line in that text item.
Emitting a signal once the text is changed by the user.
Can anyone please suggest how I can implement this in my application? I have tried real hard but I am not able to find anything suitable. If there is any alternative or workaround, I'll be grateful to know.
Thanks!

QGraphicsTextItem does not support this ability, as I'm sure you have discovered. So you have a few options:
Reimplement focusOutEvent(QFocusEvent* event) and/or keyReleaseEvent(QKeyEvent* event) to detect when you validator needs to run. A QValidator can be created as a member of your text class, and queried either when focus is lost and/or a key is pressed (the enter key to signify completion, or on every letter). Then just create a custom signal for you when deem the editing to have finished or changed.
Use a GraphicsProxyWidget to hold a 'real' QLineEdit for text entry, just set it up with a validator as you would if placing in a traditional GUI form. You will need to 'forward' the editingFinished() or textEdited(const QString& text) signal from the QLineEdit to your QGraphicsTextItem, so you don't have to provide external access to the widget.
You could also use the internal QTextDocument of the QGraphicsTextItem, this is what actually holds and formats the text (access it with document()). However it doesn't support having a QValidator installed, so you would have to create a signal-slot loop whereby when the text is changed (signalled by contentsChanged()) it's received by the QGraphicsTextItem, validated, then either updated/cleared if it fails validation (which will update the QTextDocument, and trigger this process again) or ignored if it passes.
Neither is difficult to implement; the first requires more code but will give you more control over the visual appearance of the text box.

Related

How did I retrieve changes in the content of all text edit boxs in a widget

Now, I have a widget that used for conguring some parameters, There were some QlineEdit with default value and a save button on this widget. People may change the content of QlineEdit. And click the save button, so that the modified parameters can take effect. Here is my question:
How do retrieve changes in the content of all text line edit in this QWidget?
Once I know which edit content has changed, I can judge whether the modified values is legal, and then let the change take effect.
Can anyone give me some ideas?
OS: Windows10
QT: qt 5.9.0
For this situation, It's better to do one more step before manually validating the user's input. The step is to limit the user to enter invalid settings. If your setting value is a number, use QSpinBox or QDoubleSpinBox for floating-point values. If you want to let the user select from multiple predefined values, like gender(Male, Female), use QComboBox or QRadioButton and so forth. Here is the list of Qt's widgets. So bear in mind, using QLineEdit for all of the inputs is not a good idea.
If your input is something more complex, you can use validators. For getting the idea see this question.
At last, you connect the save button's clicked signal to the slot defined in your widget class using Qt's signals and slots mechanism and get values from all of your inputs and check them, and if everything is OK, apply them to your system.

How to intercept / modify excape key functionality in Qt

I am trying to fix a bug in a Qt app which I did not write. The window changes the background color of the entire window to red and puts up some buttons, dialog boxes, etc. When the escape key is pushed, the boxes and buttons go away, leaving an empty red screen. The Cancel button does the right thing in returning to the previous window. I think I need to somehow be notified of when the escape key is pushed, and then call the same function as the cancel pushbutton does. Hopefully, I can limit the scope of this special action to when the problem window is up. I am an experienced programmer but a complete Qt newbie. This app is purely C++. To my knowledge, it does not appear to use any QML. I am still searching through the Qt online documentation, but any suggestions / examples are appreciated.
This depends a lot on your specific Qt version and setup of your application. That said, I'll take a shot at helping. In the class where you're trying to intercept the escape key press, assuming it's inheriting QObject, simply override the virtual function eventFilter. Inside your overridden instance of eventFilter, check that the QEvent type is a QEvent::KeyPress, and then check whether the key of that KeyPress is the escape key, and handle as needed.
Be sure that you pass the event out of your function, else you'll see your overridden function eat all events. If you explicitly want the event to be "eaten", simply do not return it from your function. For specifics and examples, check out documentation of QObject::eventFilter().

QLineEdit password safety

In my application user types his password in QLineEdit.
QLineEdit works in Password echo mode.
Application must clear password from memory when it is no longer needed.
Does QLineEdit make sure that it clears all its internal memory buffers before they are freed? I cannot found such information in documentation.
If QLineEdit does not clear its content then what is the simplest way to implement such behavior? I want to reuse QLineEdit functionality as much as possible and do not want to implement my own password edit control from scratch. Is it possible?
Note that even when calling setText({}) is not completely safe - the string might get written to swap space if your application is swapped out. The only way to prevent that is to allocate the memory for the internal string of the lineEdit yourself and call mlock() on it to prevent swapping. For that you need to write your own lineEdit.
In addition, the text is quite trivial to figure out when attaching a run-time introspection tool like Gammaray to your application, as it is a normal QObject property, and stored obfuscated in RAM.
Also, by looking at the implementation of QWidgetLineControl::internalSetText (see the code), it seems like the line edit text is made available for the accessibility interface, which is accessible to everyone unless accessibility support was not compiled into Qt.
So, depending on your security level, you do need your own implementation.
I think calling
QLineEdit::setText("");
will do the job. As Qt documentation says:
Setting this property clears the selection, clears the undo/redo
history, moves the cursor to the end of the line and resets the
modified property to false.
In opposite, calling QLineEdit::clear() will clear only text, however Undo/Redo stack will still contain the previous text.

Disable 'Return' key in a QPlainTextEdit

Is there any way I can prevent the user from hitting the return key when entering text in a QPlainTextEdit widget? That is, even though I want to give the viewing space of multiple lines, I want that the if the user hits enter, a new line should not begin.
The reason for doing this is that I am adding a GUI layer on top of an existing command-line and if the user enters data using the return key, it might complicate stuff and I had really avoid changing the command line code.
Handle the key press event and filter out any return keys.
There isn't IIRC a 'allowed chars' validator for a qtextedit.
Although you can use QValidator with a QLineEdit.
See this example
ps. If you also have to deal with pasting in text with a CR, then you need to deal with the textchanged signal as well

Virtual keyboard for QtWebKit based browser or how can I get the currently focused text field?

I'm working on implementing a virtual keyboard for a QtWebKit based browser. I'm having a lot of difficulty understanding how QtWebKit paints the controls within the actual page. Initially I thought they were QLineEdit instances, but they are not. Diving into implementation it appears that the glue code between Qt and WebKit paints the text field using QStyle and QPainter. Unfortunately, I'm very new to Qt and so I dont understand where in the event loop the mouse presses for these events are interpreted. I found Editor::canEdit() deep in the call stack, and now I can bring up the virtual keyboard when the user clicks on a text field within the page. The virtual keyboard then expected a pointer to a QWidget instance, but Edito::canEdit() doesn't carry that information and I can't find anywhere where a QWidget like instance is exposed. I'm really stumped, any advice would be most welcome.
Thanks!
You might get better luck by hooking the virtual keyboard into the Qt input method system. Search for "InputMethod" in the source code of QtWebKit Api, i.e. the qweb*.* files.

Resources