Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Highlight text question
Forum Updated to NodeBB v4.3 + New Features

Highlight text question

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 775 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SherifOmran
    wrote on last edited by
    #1

    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
    

    }
    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved