take a look at this example
#include <QtGui>
#include "test.h"
#include "highlighter.h"
HihghlighterDelegate
::HihghlighterDelegate(QObject *parent
){
m_highlighter = new Highlighter(m_textDocument);
}
{
Q_UNUSED(option);
m_textDocument->setPlainText(text);
pixmap.fill(Qt::transparent);
m_textDocument->drawContents(&p);
painter->drawPixmap(rect, pixmap);
}
{
table->horizontalHeader()->setCascadingSectionResizes(true);
table
->horizontalHeader
()->setResizeMode
(QHeaderView::Interactive);
table->horizontalHeader()->setStretchLastSection(true);
table->setItemDelegate(new HihghlighterDelegate(table));
keywords << "char" << "class" << "const"
<< "double" << "enum" << "explicit"
<< "friend" << "inline" << "int"
<< "long" << "namespace" << "operator"
<< "private" << "protected" << "public"
<< "short" << "signals" << "signed"
<< "slots" << "static" << "struct"
<< "template" << "typedef" << "typename"
<< "union" << "unsigned" << "virtual"
<< "void" << "volatile";
for (int r = 0; r < table->rowCount(); ++r) {
for (int c = 0; c < table->columnCount(); ++c) {
table->setItem(r, c, item);
}
}
vbl->addWidget(table);
}
#include <QtGui>
#include "test.h"
#include "highlighter.h"
HihghlighterDelegate::HihghlighterDelegate(QObject *parent)
: QItemDelegate(parent)
{
m_textDocument = new QTextDocument(this);
m_highlighter = new Highlighter(m_textDocument);
}
void HihghlighterDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
{
Q_UNUSED(option);
m_textDocument->setPlainText(text);
QPixmap pixmap(rect.size());
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
m_textDocument->drawContents(&p);
painter->drawPixmap(rect, pixmap);
}
Test::Test(QWidget *parent)
: QDialog(parent)
{
QVBoxLayout *vbl = new QVBoxLayout(this);
QTableWidget *table = new QTableWidget(10, 10);
table->horizontalHeader()->setCascadingSectionResizes(true);
table->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
table->horizontalHeader()->setStretchLastSection(true);
table->setItemDelegate(new HihghlighterDelegate(table));
QStringList keywords;
keywords << "char" << "class" << "const"
<< "double" << "enum" << "explicit"
<< "friend" << "inline" << "int"
<< "long" << "namespace" << "operator"
<< "private" << "protected" << "public"
<< "short" << "signals" << "signed"
<< "slots" << "static" << "struct"
<< "template" << "typedef" << "typename"
<< "union" << "unsigned" << "virtual"
<< "void" << "volatile";
for (int r = 0; r < table->rowCount(); ++r) {
for (int c = 0; c < table->columnCount(); ++c) {
QTableWidgetItem *item = new QTableWidgetItem(keywords.at(qrand() % keywords.size()));
table->setItem(r, c, item);
}
}
vbl->addWidget(table);
}
To copy to clipboard, switch view to plain text mode
#ifndef TEST_H
#define TEST_H
#include <QDialog>
#include <QItemDelegate>
class Highlighter;
{
Q_OBJECT
public:
HihghlighterDelegate
(QObject *parent
= 0);
protected:
private:
Highlighter *m_highlighter;
};
{
Q_OBJECT
public:
};
#endif//TEST_H
#ifndef TEST_H
#define TEST_H
#include <QDialog>
#include <QItemDelegate>
class Highlighter;
class QTextDocument;
class HihghlighterDelegate: public QItemDelegate
{
Q_OBJECT
public:
HihghlighterDelegate(QObject *parent = 0);
protected:
void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const;
private:
QTextDocument *m_textDocument;
Highlighter *m_highlighter;
};
class Test: public QDialog
{
Q_OBJECT
public:
Test(QWidget *parent = 0);
};
#endif//TEST_H
To copy to clipboard, switch view to plain text mode
#include <QtGui>
#include <QApplication>
#include "test.h"
int main(int argc, char **argv)
{
Test test;
test.show();
return app.exec();
}
#include <QtGui>
#include <QApplication>
#include "test.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Test test;
test.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Higlihgter is ther class from QTDIR/examples/richtext/syntaxhighlighter
Bookmarks