No colourisation with Qt

43 views
Skip to first unread message

Alexander Sorokin

unread,
Jun 6, 2024, 6:43:19 PMJun 6
to scintilla-interest
I am setting CPP lexer to Scintilla but there is no colourisation. Tried in Windows and Ubuntu. Result is the same. What am I doing wrong?

Code reports that lexer is set. Tried setting text after setting lexer - not working too.

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    scintilla = new ScintillaEdit(this);

    scintilla->setText("#include \"mainwindow.h\"\r\n\
\r\n\
#include <QApplication>\r\n\
\r\n\
int main(int argc, char *argv[])\r\n\
{\r\n\
    QApplication a(argc, argv);\r\n\
    MainWindow w;\r\n\
    w.show();\r\n\
    return a.exec();\r\n\
}\r\n\
\r\n\
");
#if _WIN32
    typedef void *(__stdcall *CreateLexerFn)(const char *name);
    typedef int (__stdcall *GetLexerCountFn)();
    typedef void (__stdcall *GetLexerNameFn)(unsigned int index, char *name, int buflength);
#else
    typedef void *(*CreateLexerFn)(const char *name);
    typedef int (*GetLexerCountFn)();
    typedef void (*GetLexerNameFn)(unsigned int index, char *name, int buflength);
#endif
    QFunctionPointer fn = QLibrary::resolve("lexilla", "GetLexerCount");
    int lexCount = ((GetLexerCountFn)fn)();
    qDebug() << "Lexer count =" << lexCount;
    if (lexCount > 0) {
        fn = QLibrary::resolve("lexilla", "GetLexerName");
        for (int i = 0; i < lexCount; i++) {
            char name[100] = "";
            ((GetLexerNameFn)fn)(i, name, sizeof(name));
            qDebug() << name;
        }

        fn = QLibrary::resolve("lexilla", "CreateLexer");
        void *lexCpp = ((CreateLexerFn)fn)("cpp");
        if (lexCpp != NULL) {
            qDebug() << "cpp lexer created.";
            sptr_t old_lexer = scintilla->lexer();
            scintilla->setILexer((sptr_t)(void *)lexCpp);

            sptr_t new_lexer = scintilla->lexer();
            if (new_lexer == old_lexer) {
                qDebug() << "Lexer is not set!";
            }
            else {
                qDebug() << scintilla->lexerLanguage() << "lexer set!";
            }
        }
        else {
            qDebug() << "Lexer is NULL!";
        }
    }

    setCentralWidget(scintilla);

    scintilla->colourise(0, -1);
}

Neil Hodgson

unread,
Jun 6, 2024, 6:53:14 PMJun 6
to Scintilla mailing list
Alexander Sorokin:

> I am setting CPP lexer to Scintilla but there is no colourisation. Tried in Windows and Ubuntu. Result is the same. What am I doing wrong?
>
> Code reports that lexer is set. Tried setting text after setting lexer - not working too.

You need to specify visual styles like colours for the lexical classes. For a very simple example, see Bait.
https://www.scintilla.org/bait.zip

Neil

seasoned_geek

unread,
Jun 29, 2024, 5:09:19 PMJun 29
to scintilla-interest
If you haven't found your answer yet, you may wish to look at Example 5 for CsScintilla.

Yes, it is using CopperSpice and a CopperSpice port of Scintilla, but there aren't a lot of "Hello World!" type examples for Scintilla anymore. The SCiTE text editor was that, but now it is the test bed for everything so a bit difficult to find the tree for the forest. 

Reply all
Reply to author
Forward
0 new messages