How to build a QTextDocument with QTextBlocks from AST and maintain relation

298 Views Asked by At

I wrote a lexer and parser which outputs an AST of nodes with parent and child elements (see https://github.com/oohlaf/watchadoin/tree/main/src/watchadoin).

Now I want to create a GUI in QT with PySide6 and make a QTextEdit widget. Now I understand from https://doc.qt.io/archives/qt-5.7/richtext-structure.html it uses a QTextDocument as underlying document object with QTextBlocks and QTextFragmnents.

I could map the AST nodes to those elements, for example a textblock for a single line consisting of text fragments. But I don't see methods to instantiate those classes and set the contents. Should I construct that hierarchy from the AST using a visitor operating on a TextCursor? But how does one keep those trees in sync?

How do code editors do this? Could not find any relevant references on Google.

0

There are 0 best solutions below