PDA

View Full Version : QTableWidget || QListWIdget with syntax highlighting



ericV
16th July 2009, 09:55
Hi evryone,

I am faily new to QT so please dont shoot me for asking things that may be "dumb". :D


So to my question, I want to create a Table or List (to me a table with one colum) in wich i will have single lines of Script-Code, so I want them to be Highlighted.

I have already tried two things, using the QTextEdit as a delegate and a widget.

As a Widget id did exactly what I wanted, but I am worried that it might hurt performance as the table/List grows.

Because of that i would prefer to use the delegate...

When i tried the TextEdit delegate with syntax higlighter it only worked when i was in "editmode".
But as soon as the delegate was released the formating was lost... Since the table only takes plain text (?).

I have found little on the net to help me acomplish this. Only option that I have found is to create my own TableWidget subclass and reimplement the paint method. Is this truly the best way or is there something easier?

Greets,

Eric

spirit
16th July 2009, 10:03
you can still use an own delegate, all what you need it's a create your own syntaxhiglighter then attach it to QTextDocument and then render text document using QTextDocument::drawContents in your delegate.

ericV
16th July 2009, 15:25
Thank you for the fast reply... I will look into that.... Will post my results.

ericV
20th August 2009, 10:06
Hi, its been a while... but i was working on an other part of the project, so i have just circled back to this problem.

My Question is: How would i draw the contents of my QTextdocument onto a table-cell?

Haven't been able to find something similar in the QTableWidget documentation. Or would i have to subclass from it and re-implement the paintevent or repaint methods?

Greets

Eric

spirit
20th August 2009, 10:54
take a look at this example


#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);
}




#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





#include <QtGui>
#include <QApplication>
#include "test.h"

int main(int argc, char **argv)
{
QApplication app(argc, argv);

Test test;
test.show();

return app.exec();
}



Higlihgter is ther class from QTDIR/examples/richtext/syntaxhighlighter

ericV
20th August 2009, 11:06
Thank you! Thats great... was thinking way to complicated (as always) Was working with a QtextEdit in a delegate made from QStyledItemDelegate....

thanks again... will post if i succeeded. :)

ericV
21st August 2009, 13:18
I'm trying to implement your delegate, but it doesn't seem to be drawing the text into the cell. (?) I meen if I go int edit-mode the text is there, but when i leave it the cell is left blank. :(

I'm unsure if it has anything to do with my Highlighter class... since I re-implemented it to work on a QTextEdit instead of a QTextDocument...

(just tried it without the Highlighter and still not working)

Any ideas? Im going to experiment a litle with pen colors, but this shouldn't be the problem, right?

ericV
21st August 2009, 13:26
Just tried your code and there it works... (im stumped....)