Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Prevent QTextFormat from extending after user input
Forum Updated to NodeBB v4.3 + New Features

Prevent QTextFormat from extending after user input

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 549 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeMamaV2
    wrote on last edited by JoeMamaV2
    #1

    Hello.

    I have a question regarding formatting that is used under the hood of QTextDocument. Suppose I have QTextEdit and 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 eventFilter where I have tried to (1) find if caret is next to my QTextCharFormat; (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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        J Offline
        J Offline
        JoeMamaV2
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Do you mean the code you originally posted ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          0
          • SGaistS SGaist

            Do you mean the code you originally posted ?

            J Offline
            J Offline
            JoeMamaV2
            wrote on last edited by
            #5

            @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.

            JonBJ 1 Reply Last reply
            0
            • J JoeMamaV2

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @JoeMamaV2
              But it is still going return false; in two places?

              J 1 Reply Last reply
              0
              • JonBJ JonB

                @JoeMamaV2
                But it is still going return false; in two places?

                J Offline
                J Offline
                JoeMamaV2
                wrote on last edited by JoeMamaV2
                #7

                @JonB said in Prevent QTextFormat from extending after user input:

                @JoeMamaV2
                But it is still going return 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 QTextEdit will 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.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JoeMamaV2
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved