Planned maintenance: From Sunday 8th December 10:00 CET there will be changes to try and solve the caching issues that have been experienced. If anyone has a problem connecting after this period then please PM @AndyS or any of the moderators.

Add custom text wrap logic to QPlainTextEdit



  • Is there a way to add a custom text wrap logic to a QPlainTextEdit, maybe extending it or any of its related classes?

    My goal is to create a simple text editor for my students to implement a Dynamic Programming algorithm for laying text on a canvas. As it should be simple, I'm hoping there is a simple solution to this problem.

    In short, I will receive a list of valid cursor positions in which the editor is supposed to visually break the lines.

    I already read a little about QTextDocument, QPlainTextDocumentLayout, QTextLayout and the likes, but I could not find by myself where to put the custom logic on.

    Thank you.



  • Another way to put this: How do I inject a custom QTextLayout in a QPlainTextEdit?



  • Apparently it is virtually impossible to do what I want because the QTextBlock itself hardcodes a QTextLayout in its ::layout() method (see here). Moreover, it seems that every hook we could use is only available in the private classes...

    Now, the problem is that it prevents everybody wanting to implement custom behavior to create anything based on the QTextDocument and its associates. I wonder how the people who created text editors got away with this (probably implemented everything from scratch).

    Well, at the end, the only thing I found that could work was to manually insert unicode U+002028 (Line Separator) wherever I want using QTextCursor, which is terrible because everytime the editor area width changes I have to remove the old ones and insert new ones...

    As I'm not happy with the solution I found, I'll not mark this question as solved.