Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. In PyQt5, how can I do some specific task when a user clicks on a specific span of text in a QTextEdit
Forum Updated to NodeBB v4.3 + New Features

In PyQt5, how can I do some specific task when a user clicks on a specific span of text in a QTextEdit

Scheduled Pinned Locked Moved Unsolved Qt for Python
11 Posts 4 Posters 1.5k Views 2 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.
  • I Offline
    I Offline
    Inhahe
    wrote on last edited by
    #1

    How can I tag a span of text in a QTextEdit so that when a user rightclicks or doubleclicks on it the program can retrieve an object (or at least an id or class name) that was arbitrarily associated with the specific span of text the user clicked on?

    I'm writing an IRC client, and when the user doubleclicks or rightclicks on a nick in the channel scroll (where it says who a message is from), I want the program to know (a) that they clicked on a nick, and (b) what the nick was.

    I'm guessing I'd use insertHtml and add '<span class="nick">some_nick</span>', but I don't know how to retrieve the class and the span text in the click function. Thanks

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Inhahe
      wrote on last edited by
      #2

      Ok, I did some more searching and found

      void QTextEdit::clicked ( int para, int pos ) [signal]
      This signal is emitted when the mouse is clicked on the paragraph para at character position pos.
      

      and

      virtual void insertParagraph ( const QString & text, int para )
      

      Are those the two functions I would use? I want to always append the paragraph, but then I want to know what value of its para is. Do I have to keep track of the para values myself and use insertParagraph and pass a para value that's one more than the current count? Or am I misunderstanding or is there a better way?

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Inhahe
        wrote on last edited by
        #3

        Oh, turns out PyQt5 has no function insertParagraph, I have no idea how to do this then...

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

          Hi and welcome to devnet,

          QTextEdit has no such method. Maybe you are looking for QTextEdit::append ?

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

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Inhahe
            wrote on last edited by
            #5

            It appears to be something in Qt but not in PyQt. Which is unfortunate because it seems to be what I need. Append won't do what I need (it won't associate a specific part of the text in the QTextEdit with a specific object/string/int/whatever to be retrieved when it's clicked on..)

            JonBJ 1 Reply Last reply
            0
            • I Inhahe

              It appears to be something in Qt but not in PyQt. Which is unfortunate because it seems to be what I need. Append won't do what I need (it won't associate a specific part of the text in the QTextEdit with a specific object/string/int/whatever to be retrieved when it's clicked on..)

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

              @Inhahe said in In PyQt5, how can I do some specific task when a user clicks on a specific span of text in a QTextEdit:

              It appears to be something in Qt but not in PyQt.

              What do you mean by this? To the best of my knowledge there aren't any functions exposed in Qt5 which are not available in PyQt5. Please state what exact function in Qt5 (provide a link to the Qt5 docs page for it) you are trying to call which you say is not present in PyQt5.

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Inhahe
                wrote on last edited by
                #7

                @Inhahe said in In PyQt5, how can I do some specific task when a user clicks on a specific span of text in a QTextEdit:

                insertParagraph

                https://doc.qt.io/archives/3.3/qtextedit.html#insertParagraph

                I tried print(f"{window.insertParagraph=}") where window is an object of a subclassed QTextEdit, and I got builtins.AttributeError: 'QTextEdit' object has no attribute 'insertParagraph'

                Also in the PyQt5 documentation I didn't see an overload of QTextEdit.clicked() that takes a 'para' argument as in here: https://doc.qt.io/archives/3.3/qtextedit.html#insertParagraph

                By the way, I came across a class that may be the key to what I want to do, QTextBlock, but I'm not sure how to use it to do what I want..

                JonBJ VRoninV 2 Replies Last reply
                0
                • I Inhahe

                  @Inhahe said in In PyQt5, how can I do some specific task when a user clicks on a specific span of text in a QTextEdit:

                  insertParagraph

                  https://doc.qt.io/archives/3.3/qtextedit.html#insertParagraph

                  I tried print(f"{window.insertParagraph=}") where window is an object of a subclassed QTextEdit, and I got builtins.AttributeError: 'QTextEdit' object has no attribute 'insertParagraph'

                  Also in the PyQt5 documentation I didn't see an overload of QTextEdit.clicked() that takes a 'para' argument as in here: https://doc.qt.io/archives/3.3/qtextedit.html#insertParagraph

                  By the way, I came across a class that may be the key to what I want to do, QTextBlock, but I'm not sure how to use it to do what I want..

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

                  @Inhahe said in In PyQt5, how can I do some specific task when a user clicks on a specific span of text in a QTextEdit:

                  https://doc.qt.io/archives/3.3/qtextedit.html#insertParagraph

                  As you can see from the URL, this is archived documentation for Qt 3.3....! The documentation page you are choosing to use is from 2005. You need to use documentation for the version of Qt you are using.

                  I wrote about Qt5 & PyQt5.

                  1 Reply Last reply
                  2
                  • I Inhahe

                    @Inhahe said in In PyQt5, how can I do some specific task when a user clicks on a specific span of text in a QTextEdit:

                    insertParagraph

                    https://doc.qt.io/archives/3.3/qtextedit.html#insertParagraph

                    I tried print(f"{window.insertParagraph=}") where window is an object of a subclassed QTextEdit, and I got builtins.AttributeError: 'QTextEdit' object has no attribute 'insertParagraph'

                    Also in the PyQt5 documentation I didn't see an overload of QTextEdit.clicked() that takes a 'para' argument as in here: https://doc.qt.io/archives/3.3/qtextedit.html#insertParagraph

                    By the way, I came across a class that may be the key to what I want to do, QTextBlock, but I'm not sure how to use it to do what I want..

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @Inhahe said in In PyQt5, how can I do some specific task when a user clicks on a specific span of text in a QTextEdit:

                    By the way, I came across a class that may be the key to what I want to do, QTextBlock, but I'm not sure how to use it to do what I want..

                    BINGO!

                    See https://doc.qt.io/qt-5/qtextblockuserdata.html

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    2
                    • I Offline
                      I Offline
                      Inhahe
                      wrote on last edited by
                      #10

                      Help! I'm using a QTextFrame within my QTextEdit (because I can't figure out how to add text to and insert a QTextBlock instead, and besides, I hear using those puts newlines in your document because they're supposed to be paragraphs), and I can't figure out how to retrieve the QTextFrame at a given mouse location.

                      So far I have this:

                      print(f"{self.cursorForPosition(e.pos()).currentFrame().document().toPlainText()=}")

                      Just to see if that retrieves the right one, and I get the text of the whole document, not the small part I put into its own frame, whether I click on that part or elsewhere in the QTextEdit..

                      Thanks

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        Inhahe
                        wrote on last edited by
                        #11

                        Also I'm having trouble inserting the QTextFrame without it putting a newline in my QTextEdit. I don't know how to make it do that. The funny thing is it puts one before the QTextFrame but not one after it.

                        Here's my code:

                        def addmsg(textedit, nick, message):
                          if config.show_timestamp:
                            obj_now = datetime.now()
                            textedit.insertPlainText(f"[{obj_now.hour: >2}:{obj_now.minute: >2}] ")
                          textedit.cursor.insertText("<")
                          nickframe = textedit.cursor.insertFrame(QTextFrameFormat())
                          nickframe.nick = nick
                          framecursor = nickframe.firstCursorPosition()
                          framecursor.insertText(nick)
                          textedit.cursor.insertText("> ")
                          colorify(textedit, message)
                          textedit.cursor.insertHtml("<br>")
                        

                        Here's how it looks:

                        <
                        inhahe_> test

                        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