添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode . Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

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

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

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

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

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

@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

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

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