问题描述
我正在qt中构建一个文本编辑器,当我尝试实现一个简单的texthighlighter时我遇到了这个错误
这里是我的代码
myhighlighter.cpp
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
#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
myhighlihter.h
#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
mainwindow.cpp的主要部分
main part in mainwindow.cpp
#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得到错误,因为评论中显示的项目标题
请让我知道我错过了什么
i get an error as the title of the project in the line displayed in comment
please let me know what i am missing
推荐答案
virtual void highlightBlock(const QString & text) = 0;
这个函数通过本文档保持类抽象,因为它是纯虚拟,通常也称为抽象函数。这个类派生自 QObject
类,它是非抽象的(可以被认为是 pseudo-abstrac ),因此,具有一个具体的(非抽象)class(es),你必须创建一个派生自 QSyntaxHighlighter
的类,并最终覆盖 QSyntaxHighlighter :: highlightBlock
。
你还没有这样做。请注意你刚刚添加了这个功能:
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:
void highlightblock(const QString &text);
此函数无关虚拟空白 QSyntaxHighlighter :: highlightBlock
,它有不同的名称。 所以,你刚刚创建了一个全新的非虚拟功能,而不是覆盖一个必须被覆盖的功能。
(我希望你知道C ++是区分大小写的;可能你只是拼错了名字。)
这篇关于抽象类类型的新表达式无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!