C++ QSyntaxHighlighter不创建QTextFragments
我正在使用Qt开发一个语法高亮器,我想在它上面添加单元测试,以检查格式是否得到了很好的应用。 但我无法按格式划分块。我使用了QTextBlock和QTextFragment,但当的文档说: 文本片段描述以单个字符格式存储的文本片段 以下是可运行的main.cpp文件中的代码:C++ QSyntaxHighlighter不创建QTextFragments,c++,qt,syntax-highlighting,syntaxhighlighter,C++,Qt,Syntax Highlighting,Syntaxhighlighter,我正在使用Qt开发一个语法高亮器,我想在它上面添加单元测试,以检查格式是否得到了很好的应用。 但我无法按格式划分块。我使用了QTextBlock和QTextFragment,但当的文档说: 文本片段描述以单个字符格式存储的文本片段 以下是可运行的main.cpp文件中的代码: #include <QApplication> #include <QTextEdit> #include <QSyntaxHighlighter> #include <QRegu
#include <QApplication>
#include <QTextEdit>
#include <QSyntaxHighlighter>
#include <QRegularExpression>
#include <QDebug>
class Highlighter : public QSyntaxHighlighter
{
public:
Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{}
protected:
void highlightBlock(const QString& text) override
{
QTextCharFormat classFormat;
classFormat.setFontWeight(QFont::Bold);
QRegularExpression pattern { "\\bclass\\b" };
QRegularExpressionMatchIterator matchIterator = pattern.globalMatch(text);
while (matchIterator.hasNext())
{
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), classFormat);
}
// ==== TESTS ==== //
qDebug() << "--------------------------------";
QTextDocument *doc = document();
QTextBlock currentBlock = doc->firstBlock();
while (currentBlock.isValid()) {
qDebug() << "BLOCK" << currentBlock.text();
QTextBlockFormat blockFormat = currentBlock.blockFormat();
QTextCharFormat charFormat = currentBlock.charFormat();
QFont font = charFormat.font();
// each QTextBlock holds multiple fragments of text, so iterate over it:
QTextBlock::iterator it;
for (it = currentBlock.begin(); !(it.atEnd()); ++it) {
QTextFragment currentFragment = it.fragment();
if (currentFragment.isValid()) {
// a text fragment also has a char format with font:
QTextCharFormat fragmentCharFormat = currentFragment.charFormat();
QFont fragmentFont = fragmentCharFormat.font();
qDebug() << "FRAGMENT" << currentFragment.text();
}
}
currentBlock = currentBlock.next();
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto *textEdit = new QTextEdit;
auto *highlighter = new Highlighter(textEdit->document());
Q_UNUSED(highlighter);
textEdit->setText("a class for test");
textEdit->show();
return a.exec();
}
#包括
#包括
#包括
#包括
#包括
类荧光灯:公共荧光灯
{
公众:
荧光笔(QTextDocument*父项)
:QSyntaxHighlighter(父级)
{}
受保护的:
无效highlightBlock(常量字符串和文本)覆盖
{
QTextCharFormat类格式;
classFormat.setFontWeight(QFont::Bold);
QRegularExpression模式{“\\b类\\b”};
QRegularExpressionMatchIterator matchIterator=pattern.globalMatch(文本);
while(matchIterator.hasNext())
{
QRegularExpressionMatch=matchIterator.next();
setFormat(match.capturedStart(),match.capturedLength(),classFormat);
}
//===测试===//
qDebug()firstBlock();
while(currentBlock.isValid()){
qDebug()好的,我从以下文档中找到:
请注意,通过此函数设置的格式不会修改文档本身
语法荧光灯应用的格式不存储在QTextBlock::charFormat中,而是以其他格式存储:
QVector<QTextLayout::FormatRange> formats = textEdit->textCursor().block().layout()->formats();
QVector formats=textEdit->textCursor().block().layout()->formats();