000
13.08.2005, 19:11 Uhr
firefoxxi
|
Hi,
ich arbeite unter Windows mit MinGW und Qt4. Nun habe ich folgendes Programm geschrieben:
C++: |
// Highlighter.h
#ifndef HIGHLIGHTER_H #define HIGHLIGHTER_H
#include <QHash> #include <QString> #include <QStringList> #include <QTextBlock> #include <QTextCharFormat>
class QTextDocument;
class Highlighter : public QObject { Q_OBJECT public: Highlighter(QObject *parent = 0); void addToDocument(QTextDocument *doc); void addKeyword(const QString keyword, const QTextCharFormat format); private slots: void highlight(int pos, int removed, int added); private: void highlightBlock(QTextBlock block);
QHash<QString,QTextCharFormat> keywords; }
#endif
|
C++: |
// highlighter.cpp
#include <QtGui>
#include "highlighter.h"
Highlighter::Highlighter(QObject *parent) : QObject(parent) { }
void Highlighter::addToDocument(QTextDocument *doc) { connect(doc, SIGNAL(contentsChange(int,int,int)),this, SLOT(highlight(int,int,int))); }
void Highlighter::addKeyword(const QString keyword, const QTextCharFormat format) { keywords[keyword] = format; }
void Highlighter::highlight(int pos, int removed, int added) { QTextDocument *doc = qobject_cast<QTextDocument *>(sender()); QTextBlock block = doc->findBlock(pos); if(!block.isValid()) return; QTextBlock endBlock; if(added > removed) endBlock = doc->findBlock(pos+added); else endBlock = block; while(block.isValid() && !(endBlock < block)) { highlightBlock(block); block.next(); } }
void Highlighter::highlightBlock(QTextBlock block) { QTextLayout *layout = block.layout(); const QString text = block.text(); QList<QTextLayout::FormatRange> overrides; foreach(QString keyword, keywords.keys()) { if(keyword == text) { QTextLayout::FormatRange range; range.start = 0; range.length = text.length(); range.format = keywords[keyword]; overrides << range; } } layout->setAdditionalFormats(overrides); const_cast<QTextDocument *>(block.document())->markContentsDirty( block.position(),block.length()); }
|
C++: |
// MainWindow.h
#ifndef __MAINWINDOW_H #define __MAINWINDOW_H
#include <QMainWindow> #include <QTextEdit>
class MainWindow : public QMainWindow { public: MainWindow(QWidget *parent = 0);
private: QTextEdit *editor; };
#endif
|
C++: |
// MainWindow.cpp
#include <QtGui> #include "MainWindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { editor = new QTextEdit(this); setCentralWidget(editor); Highlighter hl; hl.addToDocument(editor->document()); QTextCharFormat keywords; keywords.setFontItalic(true); hl.addKeyword("html",keywords); }
|
C++: |
// main.cpp
#include <QtGui> #include "highlighter.h" #include "MainWindow.h"
int main(int argc, char* argv[]) { QApplication app(argc,argv); MainWindow win; win.show(); return app.exec(); }
|
Jetzt erhalte ich allerdings folgenden Fehler
Code: |
MainWindow.h:9: error: multiple types in one declaration
|
Edit: Der Code der Highlighter Klasse ist gröstenteils dem Qt Beispielprogramm entnommen -- Gruß firefoxxi
http://firebird-browser.de/ Dieser Post wurde am 13.08.2005 um 19:13 Uhr von firefoxxi editiert. |