Python Forum
[PyQt] Using QSyntaxHighlighter to query a documents format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Using QSyntaxHighlighter to query a documents format
#1
The documentation for QSyntaxHighlighter Class indicates that the QSyntaxHighlighter class allows you to define syntax highlighting rules, and in addition you can use the class to query a document's current formatting; however, I cannot find any examples of how it can be used to query the existing format of a document.
Does anyone know of a basic example that might offer some insight? Ideally in python, but even C++ would help.
Reply
#2
have you seen: https://doc.qt.io/qtforpython-5/PySide2/...ghter.html
which has ample examples.
Reply
#3
This example uses state to help highlight multi-line strings.

https://wiki.python.org/moin/PyQt/Python...ghlighting

Here's some rather useless examples of userdata.

https://programtalk.com/python-examples/....__init__/

I get the impression that useredata is whatever you want it to be. If your formatting needs to keep track of more information that can be done using state, just make some user data.

format(pos) returns the QTextCharFormat object used at pos. Ths is not all loosy goosy like the state and userdata.

https://doc.qt.io/qt-5/qtextcharformat.html
Reply
#4
(Sep-30-2022, 02:37 AM)Larz60+ Wrote: have you seen: https://doc.qt.io/qtforpython-5/PySide2/...ghter.html
which has ample examples.

Thank you for replying; yes, I saw those examples, but all of those are based around setFormat() which applies a format. I have a document to which I have already applied formatting and saved to html, but now I want to able to reload that document, select a highlighted section and identify the format applied and then remove or change the format. The QSyntaxHighlighter Class suggests that this is possible - " in addition you can use the class to query a document's current formatting", but I see not specific examples showing how to do that:
The function : "QTextCharFormat QSyntaxHighlighter::format(int position) const - Returns the format at position inside the syntax highlighter's current text block." seems to be what one would use, but I cannot see examples of how that would be used to interrogate an existing formatted document.
At the moment I have a simple function which applies a format.


    def highlightWord(self):
        color = QColor(Qt.yellow).lighter()
        format = QTextCharFormat()
        format.setBackground(QBrush(color)) 
        cursor = self.editor.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)
        cursor.mergeCharFormat(format)
I am trying to create a function that can detect a highlighted section and offer the option to remove it.
Reply
#5
(Sep-30-2022, 03:48 AM)deanhystad Wrote: This example uses state to help highlight multi-line strings.

https://wiki.python.org/moin/PyQt/Python...ghlighting

Here's some rather useless examples of userdata.

https://programtalk.com/python-examples/....__init__/

I get the impression that useredata is whatever you want it to be. If your formatting needs to keep track of more information that can be done using state, just make some user data.

format(pos) returns the QTextCharFormat object used at pos. Ths is not all loosy goosy like the state and userdata.

https://doc.qt.io/qt-5/qtextcharformat.html



Thank you for your reply.
I checked out those examples, but again they seem more focused on applying syntax-highlighting rather than being able to interrogate a selection and determine what formatting has been applied. Perhaps userdata is the key - one needs to mark the formating and then try and use userdata to recover the formatting used.
Reply
#6
I think "query" is only for the purpose of applying formatting. The state example queried the stste of the previous line to highlite multi line strings. I could also see query using format(pos) used to implement a format painter tool. Click a button and it gets the state, user data and character formst from the current selection. Click agsin to apply same formatting to a different selrction.
Reply
#7
you can find a 'Query' example that uses formatting here: https://python-forum.io/thread-24127.html
see 'Query' paragraph
Reply
#8
@Larz60+, wrong kind of query. This is query QSyntaxHighlighter for format information about text in a QTextEdit object or similar, not query a database.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need code for buttons to move to previous and next documents JayCee 1 2,017 Apr-11-2020, 08:25 PM
Last Post: JayCee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020