Python Forum
[PyQt] Using QSyntaxHighlighter to query a documents format - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] Using QSyntaxHighlighter to query a documents format (/thread-38334.html)



Using QSyntaxHighlighter to query a documents format - DrakeSoft - Sep-29-2022

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.


RE: Using QSyntaxHighlighter to query a documents format - Larz60+ - Sep-30-2022

have you seen: https://doc.qt.io/qtforpython-5/PySide2/QtGui/QSyntaxHighlighter.html
which has ample examples.


RE: Using QSyntaxHighlighter to query a documents format - deanhystad - Sep-30-2022

This example uses state to help highlight multi-line strings.

https://wiki.python.org/moin/PyQt/Python%20syntax%20highlighting

Here's some rather useless examples of userdata.

https://programtalk.com/python-examples/PyQt5.Qt.QTextBlockUserData.__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


RE: Using QSyntaxHighlighter to query a documents format - DrakeSoft - Sep-30-2022

(Sep-30-2022, 02:37 AM)Larz60+ Wrote: have you seen: https://doc.qt.io/qtforpython-5/PySide2/QtGui/QSyntaxHighlighter.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.


RE: Using QSyntaxHighlighter to query a documents format - DrakeSoft - Sep-30-2022

(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%20syntax%20highlighting

Here's some rather useless examples of userdata.

https://programtalk.com/python-examples/PyQt5.Qt.QTextBlockUserData.__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.


RE: Using QSyntaxHighlighter to query a documents format - deanhystad - Sep-30-2022

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.


RE: Using QSyntaxHighlighter to query a documents format - Larz60+ - Sep-30-2022

you can find a 'Query' example that uses formatting here: https://python-forum.io/thread-24127.html
see 'Query' paragraph


RE: Using QSyntaxHighlighter to query a documents format - deanhystad - Sep-30-2022

@Larz60+, wrong kind of query. This is query QSyntaxHighlighter for format information about text in a QTextEdit object or similar, not query a database.