Official website for Linux User & Developer

Develop for the Nokia N900 and Maemo 5 OS!

by Kunal Deo

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;

Pages: 1 2 3 4 5 6 7
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • Ray McConaghy

      When I finally run the program (codeColor) I get the following error ( this is, however the latest SDK for Maemo5). Can you help?

      [sbox-FREMANTLE_X86: ~/codeColor] > ./codeColor
      codeColor[3820]: GLIB CRITICAL ** Gtk – gtk_widget_set_sensitive: assertion `GTK_IS_WIDGET (widget)’ failed

      Also, I got confused with the article… it asked me to create a directory called …/codeColor… but the prompt in your article showed you running in scratchbox in a directory …/codeEditor ( where did that come from?). I am a newbie so perhaps these are stupid questions!!

      Best regards

    • Ricardo

      I’m not able to access this full article, clicking the diferent page numbers or even directly loading the URL in my browsers always redirects to first page.

      Tested with Firefox 3.6.8 and Opera 10.61.

      Bug? Anyone else having the same problem?

    • Sean

      Same problem here, can’t get past page 1. Using IE 7 on Vista.

    • florian

      will not go past Page 1.
      it would be awesome if someone could fix this.Thanks.

      Florian, using maemo 5 browser

    • Russell Barnes

      Fixed – thanks for picking up on it.

    • ZIA

      living in CIS Russia . My question how to reinstall Maemo sofware 5 or latest without USB internet or bluetooth support (which not working in my N900 )? Is there any way how to reinstall this from flash memory card 2GB any support or download stand alone Exe File any software ? Thanks if you can reply my email waiting