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. QLineEdit and pasted text

QLineEdit and pasted text

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.1k Views 4 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    I have a line edit control with no validator set. When text is copied and pasted from an external source, I would like to be able to filter the text before, or possibly after, the text is inserted.

    I cannot overload the public slot QLineEdit::paste() because it is not virtual. That would probably be the easiest and most straight-forward option.

    I can connect to the textChanged() signal, but I would need to know that the text was being pasted and not typed. Also, I would need to clear the current contents of the control before inserting the pasted text. Would the sender() function help here?

    The actual use case is to try out a regular expression on a website such as www.regex101.com, copy the working pattern to the clipboard, and then paste it into my app. That site always prepends "/" and appends "/gm" to each pattern, so I would like to strip those off automatically if they are present.

    R M 2 Replies Last reply
    0
    • R Robert Hairgrove

      I have a line edit control with no validator set. When text is copied and pasted from an external source, I would like to be able to filter the text before, or possibly after, the text is inserted.

      I cannot overload the public slot QLineEdit::paste() because it is not virtual. That would probably be the easiest and most straight-forward option.

      I can connect to the textChanged() signal, but I would need to know that the text was being pasted and not typed. Also, I would need to clear the current contents of the control before inserting the pasted text. Would the sender() function help here?

      The actual use case is to try out a regular expression on a website such as www.regex101.com, copy the working pattern to the clipboard, and then paste it into my app. That site always prepends "/" and appends "/gm" to each pattern, so I would like to strip those off automatically if they are present.

      R Offline
      R Offline
      Robert Hairgrove
      wrote on last edited by
      #2

      Another option would be to provide a button for inserting the text from the clipboard, where I have more control over the text. But how to know that some text was copied to the clipboard?

      Is there some kind of clipboard watcher in Qt I could use?

      Pl45m4P 1 Reply Last reply
      0
      • R Robert Hairgrove

        Another option would be to provide a button for inserting the text from the clipboard, where I have more control over the text. But how to know that some text was copied to the clipboard?

        Is there some kind of clipboard watcher in Qt I could use?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @Robert-Hairgrove said in QLineEdit and pasted text:

        Is there some kind of clipboard watcher in Qt I could use?

        You're welcome :)

        • https://doc.qt.io/qt-6/qclipboard.html#dataChanged

        In general, QClipboard might be a good idea to use in your case

        • https://doc.qt.io/qt-6/qclipboard.html#text

        Edit:

        When text is copied and pasted from an external source, I would like to be able to filter the text before, or possibly after, the text is inserted.

        Why not check every time the text changes?! Then you could make use of textChanged() signal...

        Another thing which comes to my mind, but not tested and no idea if this would work:

        Catch the keyPressEvent and check for "Insert" and "Ctrl + V" QKey / QKeyCombination


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        R 1 Reply Last reply
        0
        • R Robert Hairgrove

          I have a line edit control with no validator set. When text is copied and pasted from an external source, I would like to be able to filter the text before, or possibly after, the text is inserted.

          I cannot overload the public slot QLineEdit::paste() because it is not virtual. That would probably be the easiest and most straight-forward option.

          I can connect to the textChanged() signal, but I would need to know that the text was being pasted and not typed. Also, I would need to clear the current contents of the control before inserting the pasted text. Would the sender() function help here?

          The actual use case is to try out a regular expression on a website such as www.regex101.com, copy the working pattern to the clipboard, and then paste it into my app. That site always prepends "/" and appends "/gm" to each pattern, so I would like to strip those off automatically if they are present.

          M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #4

          @Robert-Hairgrove said in QLineEdit and pasted text:

          I cannot overload the public slot QLineEdit::paste() because it is not virtual.

          But it is a slot:

          class LineEdit : public QLineEdit
          {
          		Q_OBJECT
          public slots:
          		void paste()
          		{
          			qDebug()<<"paste"<<qApp->clipboard()->text();
          			insert(qApp->clipboard()->text());
          		}
          };
          
          

          and you can insert/modify the text you want.

          SGaistS R 2 Replies Last reply
          0
          • M mpergand

            @Robert-Hairgrove said in QLineEdit and pasted text:

            I cannot overload the public slot QLineEdit::paste() because it is not virtual.

            But it is a slot:

            class LineEdit : public QLineEdit
            {
            		Q_OBJECT
            public slots:
            		void paste()
            		{
            			qDebug()<<"paste"<<qApp->clipboard()->text();
            			insert(qApp->clipboard()->text());
            		}
            };
            
            

            and you can insert/modify the text you want.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Why not create your own validator for your use case ?
            You can reimplement the fixup method of the validator to clean things.

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

            R 1 Reply Last reply
            0
            • M mpergand

              @Robert-Hairgrove said in QLineEdit and pasted text:

              I cannot overload the public slot QLineEdit::paste() because it is not virtual.

              But it is a slot:

              class LineEdit : public QLineEdit
              {
              		Q_OBJECT
              public slots:
              		void paste()
              		{
              			qDebug()<<"paste"<<qApp->clipboard()->text();
              			insert(qApp->clipboard()->text());
              		}
              };
              
              

              and you can insert/modify the text you want.

              R Offline
              R Offline
              Robert Hairgrove
              wrote on last edited by
              #6

              @mpergand I meant to say that I cannot override the slot; it would hide the base class function. Therefore, it will never be called.

              1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                Why not create your own validator for your use case ?
                You can reimplement the fixup method of the validator to clean things.

                R Offline
                R Offline
                Robert Hairgrove
                wrote on last edited by
                #7

                @SGaist I thought about it, but I am stuck with the same problem I would have when implementing a slot to catch the textChanged() signal:

                Where is the text coming from?

                jsulmJ 1 Reply Last reply
                0
                • R Robert Hairgrove

                  @SGaist I thought about it, but I am stuck with the same problem I would have when implementing a slot to catch the textChanged() signal:

                  Where is the text coming from?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Robert-Hairgrove said in QLineEdit and pasted text:

                  Where is the text coming from?

                  From the signal, or what is your question?

                  "I meant to say that I cannot override the slot" - you can always call the base class version in your override.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • Pl45m4P Pl45m4

                    @Robert-Hairgrove said in QLineEdit and pasted text:

                    Is there some kind of clipboard watcher in Qt I could use?

                    You're welcome :)

                    • https://doc.qt.io/qt-6/qclipboard.html#dataChanged

                    In general, QClipboard might be a good idea to use in your case

                    • https://doc.qt.io/qt-6/qclipboard.html#text

                    Edit:

                    When text is copied and pasted from an external source, I would like to be able to filter the text before, or possibly after, the text is inserted.

                    Why not check every time the text changes?! Then you could make use of textChanged() signal...

                    Another thing which comes to my mind, but not tested and no idea if this would work:

                    Catch the keyPressEvent and check for "Insert" and "Ctrl + V" QKey / QKeyCombination

                    R Offline
                    R Offline
                    Robert Hairgrove
                    wrote on last edited by
                    #9

                    @Pl45m4 Thank you for the useful suggestions. I think I shall connect a slot to the QClipboard::dataChanged() signal in order to show a button which the user can press to paste the new text into the control.

                    Of course, if the user pastes directly into the control with Ctrl-V, for example, or a context menu, then the text will not be filtered. Also not if typing, which is why I do not want to use QValidator. It is only for convenience, so using a separate button to insert the clipboard text gives me greater flexibility.

                    1 Reply Last reply
                    0
                    • R Robert Hairgrove has marked this topic as solved on

                    • Login

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