Results 1 to 6 of 6

Thread: Using QtDesigner highlighter in my own application

  1. #1
    Join Date
    Jan 2006
    Posts
    371
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    14
    Thanked 18 Times in 17 Posts

    Default Using QtDesigner highlighter in my own application

    I see that this question raises a lot, and quite frankly besides scintilla is no reliable way to have in my application a simple widget which displays a text editor with syntax highlighter. Writing one is a very tedious job, and then maintaining the language definitions is a huge problem.

    I tried writing a QSyntaxHighlighter that knows how to load the KatePart definitions but it seemed like a lot of work, which as a part time hobbyist I can not spare. Then I found out that QtCreator 2.2 and above have this code. But I have not seen anyone who managed to rip this off that hugs ass project.

    I started working on this, slowly about two weeks ago and now I managed to load a simple langues (I started with SQL which does not include another definition). The code I use for the demo is quite simple, and not connected to the QtCreator application:

    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QPlainTextEdit>
    4. #include <QFile>
    5. #include <QIODevice>
    6.  
    7. #include "formats.h"
    8. #include "highlighter.h"
    9. #include "highlightdefinition.h"
    10. #include "highlighterexception.h"
    11. #include "qate/highlightdefinitionhandler-v2.h"
    12.  
    13. void load_text(QString fe, QPlainTextEdit *te );
    14. QSharedPointer<TextEditor::Internal::HighlightDefinition> get_highlighter_definition(QString definitionFileName);
    15.  
    16. int main( int argc, char* argv[] )
    17. {
    18. QApplication app( argc, argv );
    19. QString definitionFileName = "/usr/share/kde4/apps/katepart/syntax/sql.xml";
    20. QSharedPointer<TextEditor::Internal::HighlightDefinition> def = get_highlighter_definition(definitionFileName);
    21.  
    22. QPlainTextEdit *te = new QPlainTextEdit(mw);
    23. TextEditor::Internal::Highlighter *hl = new TextEditor::Internal::Highlighter;
    24.  
    25. hl->configureFormat(TextEditor::Internal::Highlighter::Normal, Formats::instance().charFormat() );
    26. hl->configureFormat(TextEditor::Internal::Highlighter::Keyword, Formats::instance().keywordFormat() );
    27. hl->configureFormat(TextEditor::Internal::Highlighter::Decimal, Formats::instance().decimalFormat() );
    28. hl->configureFormat(TextEditor::Internal::Highlighter::Comment, Formats::instance().commentFormat() );
    29. hl->configureFormat(TextEditor::Internal::Highlighter::Function, Formats::instance().functionFormat() );
    30.  
    31. hl->setParent(te);
    32. hl->setDocument(te->document());
    33. hl->setDefaultContext(def->initialContext());
    34. hl->rehighlight();
    35.  
    36. load_text("/home/elcuco/test.sql", te);
    37. mw->setWindowTitle("Kate syntax highter test");
    38. mw->setCentralWidget(te);
    39. mw->show();
    40. return app.exec();
    41.  
    42. Q_UNUSED(hl);
    43. }
    44.  
    45. void load_text(QString fe, QPlainTextEdit *te )
    46. {
    47. QFile f(fe);
    48. if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
    49. return;
    50. QString s = f.readAll();
    51. te->setPlainText(s);
    52. te->setLineWrapMode(QPlainTextEdit::NoWrap);
    53. }
    54.  
    55. // stollen from QSharedPointer<HighlightDefinition> Manager::definition(const QString &id)
    56. QSharedPointer<TextEditor::Internal::HighlightDefinition> get_highlighter_definition(QString definitionFileName)
    57. {
    58. QFile definitionFile(definitionFileName);
    59. if (!definitionFile.open(QIODevice::ReadOnly | QIODevice::Text))
    60. return QSharedPointer<TextEditor::Internal::HighlightDefinition>();
    61.  
    62. QSharedPointer<TextEditor::Internal::HighlightDefinition> definition(new TextEditor::Internal::HighlightDefinition);
    63. TextEditor::Internal::HighlightDefinitionHandlerV2 handler(definition);
    64.  
    65. QXmlInputSource source(&definitionFile);
    66. reader.setContentHandler(&handler);
    67. //m_isBuilding.insert(id);
    68. try {
    69. reader.parse(source);
    70. } catch (TextEditor::Internal::HighlighterException &) {
    71. definition.clear();
    72. }
    73. //m_isBuilding.remove(id);
    74. definitionFile.close();
    75.  
    76. // m_definitions.insert(id, definition);
    77. return definition;
    78. }
    To copy to clipboard, switch view to plain text mode 

    I also attach here a demo of this small application running. Is anyone else working on such thing? Did anyone manage to get the syntax highlighter used outside QtCreator on a simple QPlainTextEdit?
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Posts
    371
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    14
    Thanked 18 Times in 17 Posts

    Default Re: Using QtDesigner highlighter in my own application

    Now the code is working, and I can load simple syntax definitions. Next step is to use the language manager to nest syntax definitions, as KatePart really does. Code is available from SVN:

    Qt Code:
    1. svn co https://qtedit4.googlecode.com/svn/tools/qtsourceview
    To copy to clipboard, switch view to plain text mode 

    The code depends on the full QtCreator source. Just follow instructions to configure it (look inside the *.pro file).

    Feel free to use and abuse.

  3. #3
    Join Date
    Jan 2006
    Posts
    371
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    14
    Thanked 18 Times in 17 Posts

    Default Re: Using QtDesigner highlighter in my own application

    Good news:

    I managed to get the full syntax highlighter working, and I started testing it. I loaded the HTML syntax, and was able to see the colors for HTML/CSS/JavaScript - which means the main engine is working. I also "rebased" the code to the latest code from QtCreator (see the SVN history).

    The code should be usable for 3rd parties now.

  4. #4
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QtDesigner highlighter in my own application

    Very interesting to know that there is a syntax highlighter ready for use. Will keep this in mind for future applications!

  5. #5
    Join Date
    Jan 2006
    Posts
    371
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    14
    Thanked 18 Times in 17 Posts

    Default Re: Using QtDesigner highlighter in my own application

    I finally forked the code. The google code SVN now contains a small demo that loads definitions. Tested under Win7 and Linux, using Qt4.7. This should build on Qt4.2 and above, but I did not test it fully.

    If you are on windows, the example will not load as expected. This is because QtCreator does not use the Kate highlighter for C++ files, and thus it does not contain cpp.xml. Just download the corresponding XML from the QtCreator GUI:
    Tools->Options->Text Editor->Generic Highlighter->Download definitions.

    The XML will be saved in %HOME%\AppData\Roaming\Nokia\qtcreator\generic-highlighter, the code currently does load files from that dir, so don't worry.

    Check the SVN and send patches if you like this project.
    Attached Images Attached Images

  6. #6
    Join Date
    Jan 2006
    Posts
    371
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    14
    Thanked 18 Times in 17 Posts

    Default Re: Using QtDesigner highlighter in my own application

    I just released QtSourceView 0.0.3, with qate support: http://www.qtcentre.org/threads/3105-qtsourceview.

    Home page + download links: http://code.google.com/p/qtedit4/wiki/QtSourceView

Similar Threads

  1. Replies: 7
    Last Post: 4th February 2020, 13:41
  2. Tree Model Item Highlighter
    By sajis997 in forum Qt Programming
    Replies: 9
    Last Post: 9th January 2011, 17:58
  3. Replies: 4
    Last Post: 11th May 2010, 17:33
  4. Syntax Highlighter sample is wrong
    By kib2 in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2007, 21:24
  5. QTextEdit with syntax highlighter
    By sarode in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2006, 07:20

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.