Develop for the Nokia N900 and Maemo 5 OS!

Learn how to program the next-generation smartphone-come-internet tablet, the Nokia N900…

No registration/deregistration/invocation code should be written, because Qt’s meta object compiler (moc) automatically generates the required infrastructure.

4. About the sample application (CodeEditor)

For this tutorial we will be building an application called CodeEditor. CodeEditor will have the have the syntax colour support for C and C++ source code files. It will be able to open and save files.
This tutorial will make use of the built-in regexp functions of Qt to detect and colour the syntax of the source code files. Our application will subclass the already available QSyntaxHighlighter class to describe our own highlighting rules. Our implementation consists of the following files :
1. main.cpp: Entry point for the application.
2. codeColor.h/codeColor.cpp: Defines class codeColor a subclass of QSyntaxHighlighter. It defines the rules of syntax highlighting.
3. mainwindow.h/mainwindow.cpp: Uses the codeColor class to implement the syntax highlighting.
4. This file is the project file for the project. It is generated automatically using qmake.
You need to create a directory ~/Desktop/sbhome/codeColor. Keep all the source files inside the same directory.

5. main.cpp
main.cpp acts as the entry point for the application. It loads the Application window and resizes it to the requested dimensions.

#include <QApplication>
#include “mainwindow.h”
int main(int argc, char *argv[])
QApplication app(argc, argv);
MainWindow window;
window.resize(640, 512);;
return app.exec();

6. codeColor.h/codeColor.cpp
Here we are creating a new class called codeColor, which we are subclassing from QSyntaxHighlighter. QRegExp is used in detecting various syntax elements. We are also defining our own custom text formats using QTextCharFormat.

#include <QSyntaxHighlighter>
#include <QHash>
#include <QTextCharFormat>
class QTextDocument;
class codeColor : public QSyntaxHighlighter
codeColor(QTextDocument *parent = 0);
void highlightBlock(const QString &text);
struct HighlightingRule
QRegExp pattern;
QTextCharFormat format;
QVector<HighlightingRule> highlightingRules;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QTextCharFormat keywordFormat;
QTextCharFormat classFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat multiLineCommentFormat;
QTextCharFormat quotationFormat;
QTextCharFormat functionFormat;

Implementation of codeColor class:

#include <QtGui>
#include “codeColor.h”
codeColor::codeColor(QTextDocument *parent)
: QSyntaxHighlighter(parent)
HighlightingRule rule;
QStringList keywordPatterns;