1 Attachment(s)
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:
Code:
#include <QApplication>
#include <QMainWindow>
#include <QPlainTextEdit>
#include <QFile>
#include <QIODevice>
#include "formats.h"
#include "highlighter.h"
#include "highlightdefinition.h"
#include "highlighterexception.h"
#include "qate/highlightdefinitionhandler-v2.h"
void load_text
(QString fe, QPlainTextEdit
*te
);
QSharedPointer<TextEditor
::Internal::HighlightDefinition> get_highlighter_definition
(QString definitionFileName
);
int main( int argc, char* argv[] )
{
QString definitionFileName
= "/usr/share/kde4/apps/katepart/syntax/sql.xml";
QSharedPointer<TextEditor::Internal::HighlightDefinition> def = get_highlighter_definition(definitionFileName);
QPlainTextEdit *te = new QPlainTextEdit(mw);
TextEditor::Internal::Highlighter *hl = new TextEditor::Internal::Highlighter;
hl->configureFormat(TextEditor::Internal::Highlighter::Normal, Formats::instance().charFormat() );
hl->configureFormat(TextEditor::Internal::Highlighter::Keyword, Formats::instance().keywordFormat() );
hl->configureFormat(TextEditor::Internal::Highlighter::Decimal, Formats::instance().decimalFormat() );
hl->configureFormat(TextEditor::Internal::Highlighter::Comment, Formats::instance().commentFormat() );
hl->configureFormat(TextEditor::Internal::Highlighter::Function, Formats::instance().functionFormat() );
hl->setParent(te);
hl->setDocument(te->document());
hl->setDefaultContext(def->initialContext());
hl->rehighlight();
load_text("/home/elcuco/test.sql", te);
mw->setWindowTitle("Kate syntax highter test");
mw->setCentralWidget(te);
mw->show();
return app.exec();
Q_UNUSED(hl);
}
void load_text
(QString fe, QPlainTextEdit
*te
) {
return;
te->setPlainText(s);
te->setLineWrapMode(QPlainTextEdit::NoWrap);
}
// stollen from QSharedPointer<HighlightDefinition> Manager::definition(const QString &id)
QSharedPointer<TextEditor
::Internal::HighlightDefinition> get_highlighter_definition
(QString definitionFileName
){
QFile definitionFile
(definitionFileName
);
return QSharedPointer<TextEditor::Internal::HighlightDefinition>();
QSharedPointer<TextEditor::Internal::HighlightDefinition> definition(new TextEditor::Internal::HighlightDefinition);
TextEditor::Internal::HighlightDefinitionHandlerV2 handler(definition);
reader.setContentHandler(&handler);
//m_isBuilding.insert(id);
try {
reader.parse(source);
} catch (TextEditor::Internal::HighlighterException &) {
definition.clear();
}
//m_isBuilding.remove(id);
definitionFile.close();
// m_definitions.insert(id, definition);
return definition;
}
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?
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:
Code:
svn co https://qtedit4.googlecode.com/svn/tools/qtsourceview
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.
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.
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!
1 Attachment(s)
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.
Re: Using QtDesigner highlighter in my own application