Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a text editor in qt , i am having this error when i try to implemet a simple texthighlighter

here is my code

myhighlighter.cpp

C++
#include "myhighlighter.h"
#include <QTextCharFormat>
#include <QDebug>

myhighlighter::myhighlighter(QWidget *parent) : QSyntaxHighlighter(parent)
{
    QDebug("myhighlighter initiated");
}

void myhighlighter::highlightblock(const QString &text)
{
    QTextCharFormat format;
    QRegExp rx(pattern);
    if(!rx.isValid() || rx.isEmpty() || rx.exactMatch("")){
        setFormat(0,text.length(),format);
    }
    format.setBackground(Qt::yellow);
    int index = rx.indexIn(text);
    while(index >= 0)
    {
        int length = rx.matchedLength();
        setFormat(index,length,format);
        index = rx.indexIn(text , length + index);
    }
}



myhighlihter.h

C++
#ifndef MYHIGHLIGHTER_H
#define MYHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextDocument>
#include <QString>

class myhighlighter : public QSyntaxHighlighter
{
    Q_OBJECT
public:
    explicit myhighlighter(QWidget *parent = 0);
virtual void highlightblock(const QString &text)=0;
    void setPattern(QString str){ pattern = str ; }
private:
    QString pattern;
};

#endif // MYHIGHLIGHTER_H



main part in mainwindow.cpp
C++
#include <QFile>
#include <QKeyEvent>
#include "myhighlighter.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QFont font;
    font.setFamily("monokai");
    font.setStyleHint(QFont::Monospace);
    font.setFixedPitch(true);
    font.setPointSize(10);
    ui->textEdit->setFont(font);
    ui->textEdit->acceptRichText();
    const int tabStop = 4;  // 4 characters
    QFontMetrics metrics(font);
    ui->textEdit->setTabStopWidth(tabStop * metrics.width(' '));
    this->setCentralWidget(ui->textEdit);
    highlighter = new myhighlighter(this);     // here i get the error
    highlighter->setDocument(ui->textEdit->document());
    // highlighter->setPattern(QString("class"));
    // highlighter->highlightblock(QString(""));
}



i get an error as the title of the project in the line displayed in comment

please let me know what i am missing
Posted
Updated 28-Jun-15 6:17am
v2
Comments
Afzaal Ahmad Zeeshan 28-Jun-15 9:56am    
I am not a Qt expert, but at least I know that you cannot create abstract object. Are you missing inheritance?
Sergey Alexandrovich Kryukov 28-Jun-15 11:15am    
Of course. Look at the code sample. At least one bug is apparent.
Please see Solution 1.
—SA
bhawin parkeria 28-Jun-15 10:16am    
i am creating an object of "myhighlighter" class which inherits "QSyntaxhighlighter" , bt its giving error which proves that myhighighter is abstract . , can u help me with that

1 solution

I can see at least one bug: the help page on QSyntaxHighlighter said that it has a function
C++
virtual void highlightBlock(const QString & text) = 0;


This is the function which, by this document, keeps the class abstract, because it is pure virtual, which is also often called abstract function. This class is derived from QObject class which is non-abstract (can be considered pseudo-abstrac), so, to have a concrete (non-abstract) class(es), you have to create a class derived from QSyntaxHighlighter and eventually override QSyntaxHighlighter::highlightBlock.

You haven't done that. Note that you just added this function:
C++
void highlightblock(const QString &text);

This function has nothing to do with virtual void QSyntaxHighlighter::highlightBlock, it has different name. So, you just created a brand-new non-virtual function, instead of overriding one which has to be overridden.

(I hope you know that C++ is case-sensitive; probably you simply misspelled the name.)

—SA
 
Share this answer
 
v3
Comments
Afzaal Ahmad Zeeshan 28-Jun-15 11:49am    
+5, I did know that there was something missing related to inheritance that caused this problem.
Sergey Alexandrovich Kryukov 28-Jun-15 12:01pm    
Thank you, Afzaal.
—SA
bhawin parkeria 28-Jun-15 11:52am    
this was a good help , but it still didn't work for me , i am still getting the same error
Sergey Alexandrovich Kryukov 28-Jun-15 12:02pm    
I didn't see your fix. You code sample is not comprehensive. Declare exactly this in you derived class and override:
virtual void highlightBlock(const QString & text)
Check up that there are no more pure abstract functions.
—SA
bhawin parkeria 28-Jun-15 12:14pm    
i have also read this http://stackoverflow.com/questions/23827014/invalid-new-expression-of-abstract-class-type and this post contains an answer for qt but i dont understand that , may be you are experienced in coding enough to help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900