QTextEdit::append should have a QColor or QTextCharFormat parameter



  • Hi,

    I stumbled upon a use case where i would like to add text in a new color at the end of my document without losing or repainting my selection or scrolling in case of isBottom.

    This is basically what append does. But if i wanna change my new textcolor with setTextColor I repaint my selection.

    What is the best way of doing this? Or is it simply unimplemented?

    thx Megamouse



  • To be precise...
    It would be necessary to add the color part here (see the comments):

    void QTextEdit::append(const QString &text) // add param: const QColor& color
    {
        Q_D(QTextEdit);
        const bool atBottom = isReadOnly() ?  d->verticalOffset() >= d->vbar->maximum() :
                d->control->textCursor().atEnd();
        d->control->append(text); // add color
        if (atBottom)
            d->vbar->setValue(d->vbar->maximum());
    }
    
    void QWidgetTextControlPrivate::append(const QString &text, Qt::TextFormat format)
    {
        QTextCursor tmp(doc);
        tmp.beginEditBlock();
        tmp.movePosition(QTextCursor::End);
    	
        // Old code:
        //if (!doc->isEmpty())
        //    tmp.insertBlock(cursor.blockFormat(), cursor.charFormat());
        //else
        //    tmp.setCharFormat(cursor.charFormat());
    	
        // New code:
        QTextCharFormat newCharFormat = cursor.charFormat();
        if (color.isValid())
            newCharFormat.setForeground(color);
        if (!doc->isEmpty())
            tmp.insertBlock(cursor.blockFormat(), newCharFormat);
        else
            tmp.setCharFormat(newCharFormat);
    	
        // preserve the char format
        QTextCharFormat oldCharFormat = cursor.charFormat();
    #ifndef QT_NO_TEXTHTMLPARSER
        if (format == Qt::RichText || (format == Qt::AutoText && Qt::mightBeRichText(text))) {
            tmp.insertHtml(text);
        } else {
            tmp.insertText(text);
        }
    #else
        Q_UNUSED(format);
        tmp.insertText(text);
    #endif // QT_NO_TEXTHTMLPARSER
        if (!cursor.hasSelection())
            cursor.setCharFormat(oldCharFormat);
        tmp.endEditBlock();
    }
    


  • On another note: The append function should get a flag that skips the insertBlock


  • Lifetime Qt Champion

    Hi,

    Can you explain what your use case is ?



  • @SGaist a log that has different colors depending on error types etc.


  • Lifetime Qt Champion

    Looks rather like a job for QSyntaxHighlighter.



  • I am not sure but I get the feeling the Syntaxhighlighter is too slow for a log


  • Qt Champions 2017

    @Megamouse
    Hi
    Just as note. I found QTextEdit with append too slow with huge logs
    files and switch to a listView with a delegate.


  • Lifetime Qt Champion

    @Megamouse said in QTextEdit::append should have a QColor or QTextCharFormat parameter:

    I am not sure but I get the feeling the Syntaxhighlighter is too slow for a log

    That's one crucial detail you forgot to say in your problem description. @mrjj is right here, to show logs like that, the model/view architecture will work better.



  • It you stick with QTextEdit the QTextCursor interface is public so you can use QTextEdit::cursor() and do exactly what QWidgetTextControlPrivate::append does changing what you need


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.