Results 1 to 8 of 8

Thread: QTableWidget || QListWIdget with syntax highlighting

  1. #1
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question QTableWidget || QListWIdget with syntax highlighting

    Hi evryone,

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


    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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget || QListWIdget with syntax highlighting

    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget || QListWIdget with syntax highlighting

    Thank you for the fast reply... I will look into that.... Will post my results.

  4. #4
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget || QListWIdget with syntax highlighting

    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

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget || QListWIdget with syntax highlighting

    take a look at this example
    Qt Code:
    1. #include <QtGui>
    2. #include "test.h"
    3. #include "highlighter.h"
    4.  
    5. HihghlighterDelegate::HihghlighterDelegate(QObject *parent)
    6. : QItemDelegate(parent)
    7. {
    8. m_textDocument = new QTextDocument(this);
    9. m_highlighter = new Highlighter(m_textDocument);
    10. }
    11.  
    12. void HihghlighterDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
    13. {
    14. Q_UNUSED(option);
    15. m_textDocument->setPlainText(text);
    16.  
    17. QPixmap pixmap(rect.size());
    18. pixmap.fill(Qt::transparent);
    19. QPainter p(&pixmap);
    20.  
    21. m_textDocument->drawContents(&p);
    22.  
    23. painter->drawPixmap(rect, pixmap);
    24. }
    25.  
    26. Test::Test(QWidget *parent)
    27. : QDialog(parent)
    28. {
    29.  
    30. QVBoxLayout *vbl = new QVBoxLayout(this);
    31. QTableWidget *table = new QTableWidget(10, 10);
    32. table->horizontalHeader()->setCascadingSectionResizes(true);
    33. table->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
    34. table->horizontalHeader()->setStretchLastSection(true);
    35. table->setItemDelegate(new HihghlighterDelegate(table));
    36.  
    37. QStringList keywords;
    38. keywords << "char" << "class" << "const"
    39. << "double" << "enum" << "explicit"
    40. << "friend" << "inline" << "int"
    41. << "long" << "namespace" << "operator"
    42. << "private" << "protected" << "public"
    43. << "short" << "signals" << "signed"
    44. << "slots" << "static" << "struct"
    45. << "template" << "typedef" << "typename"
    46. << "union" << "unsigned" << "virtual"
    47. << "void" << "volatile";
    48.  
    49. for (int r = 0; r < table->rowCount(); ++r) {
    50. for (int c = 0; c < table->columnCount(); ++c) {
    51. QTableWidgetItem *item = new QTableWidgetItem(keywords.at(qrand() % keywords.size()));
    52. table->setItem(r, c, item);
    53. }
    54. }
    55.  
    56. vbl->addWidget(table);
    57. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4. #include <QDialog>
    5.  
    6. #include <QItemDelegate>
    7.  
    8. class Highlighter;
    9.  
    10. class HihghlighterDelegate: public QItemDelegate
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. HihghlighterDelegate(QObject *parent = 0);
    16.  
    17. protected:
    18. void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const;
    19.  
    20. private:
    21. QTextDocument *m_textDocument;
    22. Highlighter *m_highlighter;
    23. };
    24.  
    25. class Test: public QDialog
    26. {
    27. Q_OBJECT
    28.  
    29. public:
    30. Test(QWidget *parent = 0);
    31. };
    32.  
    33. #endif//TEST_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3. #include "test.h"
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. Test test;
    10. test.show();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Higlihgter is ther class from QTDIR/examples/richtext/syntaxhighlighter
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget || QListWIdget with syntax highlighting

    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.

  7. #7
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget || QListWIdget with syntax highlighting

    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?
    Last edited by ericV; 21st August 2009 at 15:04.

  8. #8
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget || QListWIdget with syntax highlighting

    Just tried your code and there it works... (im stumped....)

Similar Threads

  1. Replies: 13
    Last Post: 15th December 2006, 12:52
  2. QTableWidget or QListWidget
    By bpetty in forum Newbie
    Replies: 3
    Last Post: 6th December 2006, 20:40
  3. Refreshing syntax highlighting
    By jpn in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2006, 21:09
  4. A Qt4 editor part with syntax highlighting and stuff?
    By mgoettsche in forum General Discussion
    Replies: 3
    Last Post: 22nd January 2006, 17:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.