Highlight text question



  • hello guys,

    I wrote a program to highlight a text as requested from the mainwindow but my problem is that the highlight appears only when text is changed and I want it to appear when a corrected character is added in mainwindow highlighter->addmistake(1); . here is my code

    highlighter.h
    @

    #ifndef HIGHLIGHTER_H
    #define HIGHLIGHTER_H

    #include <QSyntaxHighlighter>
    #include <QHash>
    #include <QTextCharFormat>

    QT_BEGIN_NAMESPACE
    class QTextDocument;
    QT_END_NAMESPACE

    //! [0]
    class Highlighter : public QSyntaxHighlighter
    {
    Q_OBJECT

    public:
    Highlighter(QTextDocument *parent = 0);
    void addmistake(int pos);
    QList <int> corrections;

    protected:
    void highlightBlock(const QString &text);

    private:

    struct Mistakes
    {
        int position;
    };
    QVector <Mistakes> mistakes;
    
    void updatefn();
    

    };
    //! [0]

    #endif

    @

    highlighter.cpp

    @
    #include <QtGui>
    #include <QDebug>
    #include "highlighter.h"

    QString doctext;

    Highlighter::Highlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
    {
    }

    void Highlighter::addmistake(int pos)
    {
    // call this function to add reference to a mistake character
    corrections.append(pos);
    qDebug() << "added " << pos;

    }

    void Highlighter::highlightBlock(const QString &text)
    {
    qDebug () << "correction size " << corrections.size();
    updatefn();
    qDebug() << text << "block";

    }

    void Highlighter::updatefn()
    {
    // this function updates the coloring
    for (int i=0; i < corrections.size(); ++i)
    {
    setFormat(corrections.at(i),1,QColor(255,0,0));
    }

    setCurrentBlockState(0);
    

    }

    @

    mainwindow
    @
    #include <QtGui>
    #include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {

    QFont font;
    font.setFamily("Courier");
    font.setFixedPitch(true);
    font.setPointSize(30);
    
    editor = new QTextEdit;
    editor->setFont(font);
    
    highlighter = new Highlighter(editor->document());
    
    editor->setPlainText("Hello World");
    setCentralWidget(editor);
    highlighter->addmistake(1); // change color of 2nd character
    

    }
    @


Log in to reply
 

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