Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Customizing QTextCursor



  • Hi,

    I'd like to implement my own QTextCursor class to reimplement deleteChar() and deletePreviousChar().
    Can anybody tell me how to do this?
    if I do it like this:
    @MyTextCursorClass myCursor(textEdit->textCursor());
    textEdit->setTextCursor(myCursor);@
    I run into a segmentation fault.
    EDIT:
    I run into a segmentation fault if I declare my deleteChar() and deletePreviousChar() as virtual.
    Otherwise my deleteChar() and deletePreviousChar() are not called at all...

    Thanks!

    Manuel



  • QTextCursor contains no virtual methods and thus is not meant to be subclassed.



  • Thanks, but how to I reimplement the char deletion event then?



  • [quote author="manuelsch" date="1297978558"]Thanks, but how to I reimplement the char deletion event then?[/quote]

    Sorry, I have no idea :-/



  • Maybe you can do:
    textCursor.installEventFilter(myObjectj);

    And catch the events related to KeyPress to forward them to your own methods and then return from eventFilter(QObject, QEvent): true or false, depending in what you need...



  • Sorry, my bad, qtextcursor doesn't inherit from qobject...
    But you can apply the behavior that i mention before to the text widget containing the qtextcursor.



  • What is your initial problem. I'm pretty sure that you are trying do something different and assumed that changing behavior of QTextCursor is a proper solution - but it isn't.
    I suspect that you are trying to implement something like deleting two chars in some spatial case or prevent of delete char (it would be bater if you explain your real problem).



  • Ok, I want to insert building blocks of text into my QTextEdit document.
    These building blocks consist of several words (like -MS Word's- OpenOffice Writer's form letters).
    So building blocks should be removed as a whole (not just a one or a few letters of it).

    And I think this could become pretty hard, because one also has to deal with the deletion of selected text fragments...

    Any ideas how to handle that?



  • I think you can implement that in the QTextEdit, and insert some QTextBlock containing QTextBlockUserData to identify that blocks you mention.



  • Problem is quite harsh, somewhere in the past I had similar (more complicated) problem and final result wasn't perfect.
    listen to the signals "QTextDocument::contentsChange":http://doc.trolltech.com/latest/qtextdocument.html#contentsChange and "QTextDocument::contentsChanged":http://doc.trolltech.com/latest/qtextdocument.html#contentsChanged.
    On first signal you will store where change has happened on second signal you will use those data to push changes forward (deleting whole block). You can't do it in contentsChange(int,int,int) signal because it will cause a crush.

    Try also experimenting with QTextObject it might help (I never use it but it looks promising).

    Some kind of solution can be a "QTextObjectInterface":http://doc.trolltech.com/lates/qtextobjectinterface.html. In this case you provide whole object which will paint those grouped words.
    This solution requires larger effort, but it will work for sure.



  • Yes, QTextObjectInterface is great!

    I'm only wondering how serialization is possible: It's using QChar::ObjectReplacementCharacter which does not seem to be written to the database using QDataWidgetMapper.


Log in to reply