Prevent QTextFormat from extending after user input
-
Hello.
I have a question regarding formatting that is used under the hood of
QTextDocument. Suppose I haveQTextEditand I want to format a substring in it, however when I put caret right before/after my substring, new text will have the same formatting. For my task that is not acceptable, and substring formatting should be extending.I have tried multiple ways to "protect" my formatting but (as you can guess) with no avail. Most of my attempts were done using
eventFilterwhere I have tried to (1) find if caret is next to myQTextCharFormat; (2) if so, append text with no formatting. Here, however, I cannot seem to figure out how I am supposed to differentiate between actual symbols and gibberish.bool MyTextEdit::eventFilter(QObject *object, QEvent *event) { if (event->type() == QEvent::KeyPress) { auto keyEvent = static_cast<QKeyEvent *>(event); QTextCursor cursor = textCursor(); // extra logic regarding my formatted object if (!IsMySpecialFormatting(cursor)) { return false; } const auto *keyEvent = dynamic_cast<QKeyEvent *>(event); cursor.insertText(keyEvent->text(), QTextCharFormat()); return true; } return false; }Code like this results in weird symbols appending to my input such as backspace, ctrl + a and so on.
Is a way to stop Qt from extending my formatting?
PS> Sorry if there are threads regarding this issue. I am having a real hard time trying to figure out how should I search for this particular issue.
-
Hi and welcome to devnet,
Usually when you don't handle anything particular, you should return the result of the base class implementation of the function. Just returning false in your case will not allow the standard processing of your QTextEdit to occure.
-
Hi and welcome to devnet,
Usually when you don't handle anything particular, you should return the result of the base class implementation of the function. Just returning false in your case will not allow the standard processing of your QTextEdit to occure.
@SGaist said in Prevent QTextFormat from extending after user input:
Hi and welcome to devnet,
Usually when you don't handle anything particular, you should return the result of the base class implementation of the function. Just returning false in your case will not allow the standard processing of your QTextEdit to occure.
Indeed, I somehow botched an example when creating a thread. Fixed it.
-
Do you mean the code you originally posted ?
-
@SGaist said in Prevent QTextFormat from extending after user input:
Do you mean the code you originally posted ?
Yes, first copy was incorrect. Current code is basically copy pasted from my project excluding uninteresting parts.
@JoeMamaV2
But it is still goingreturn false;in two places? -
@JoeMamaV2
But it is still goingreturn false;in two places?@JonB said in Prevent QTextFormat from extending after user input:
@JoeMamaV2
But it is still goingreturn false;in two places?Yes, that's fine, no? If the cursor is not on my special formatting object, then I have nothing to do in this handler and
QTextEditwill does everything I want.Regardless, guys, my question is not about the snippet I presented. The question is whether can I stop Qt from extending formatting.
-
Video example of what I am talking about. What I want to do, is when I append text to a formatted string it does not extent. Meaning, I should be getting following result: "this is an examplethis is not a desirable result string". Formatting in my program is not a regular bold; it has a lot of stuff going with it.