PDA

View Full Version : Coloring each character of a QString with QSyntaxHighlighter



Stanfillirenfro
7th June 2012, 08:43
Hi!
I am facing a problem of coloring each character of a QString using QSyntaxHighlighter.
Let's imagine the following QString:

QString text = "L&TG3J";
The idea is to affect the color green to the character 'L', the color red to the character '&', the color yellow to the character 'T' for example. That means, everytime a character will be found at least one time, it will be affected with a specific color.
The problem is that after clicking on the button showDetails to get to the textEdit window, the program does not work anymore.
Does anybody have an idea why? I would be greatfull to any answer.
Thanks in advance!

I have the following code:

colorSequences.h file

#ifndef DEF_COLORSEQUENCES
#define DEF_COLORSEQUENCES

#include <QtGui>

class ColorSequences: public QSyntaxHighlighter
{
Q_OBJECT

public:
ColorSequences(QTextDocument *parent = 0);
~ColorSequences();

public slots:
void highlightBlock(const QString &text);

private:


};
#endif

MyWindow.h file

#ifndef DEF_MYWINDOW
#define DEF_MYWINDOW

#include <QtGui>
#include "ColorerSequences.h"

class myWindow: public QMainWindow
{
Q_OBJECT

public:
myWindow();
~myWindow();

public slots:
void openTextWindow();

private:
QTextEdit *m_myText;
QPushButton *m_showDetails;
ColorSequences *colorSequences;

};
#endif

ColorSequences.cpp file

#include "ColorSequences.h"

ColorSequences::ColorSequences(QTextDocument *parent): QSyntaxHighlighter(parent)
{

}

void ColorSequences::highlightBlock(const QString &text)
{
for(int i = 0; i<text.size(); i++)
{
if(text.at(i) == QChar('A'))
{
setFormat(i, 1, Qt::green);
}
else if(text.at(i) == QChar('C'))
{
setFormat(i, 1, Qt::blue);
}
else if(text.at(i) == QChar('T'))
{
setFormat(i, 1, Qt::red);
}
}

}

ColorSequences::~ColorSequences()
{

}

myWindow.cpp file

#include "myWindow.h"
#include "ColorSequences.h"

myWindow::myWindow()
{
m_myText = new QTextEdit(this);
m_myText->setFixedSize(300, 127);
colorSequences = new ColorSequences(m_monTexte->document());

connect(m_showDetails, SIGNAL(clicked()), this, SLOT(openTextWindow());
}

void myWindow::openTextWindow()
{
for(int i = 0; i<text.size(); i++)
{
if(text.at(i) == QChar('A'))
{
setFormat(i, 1, Qt::green);
}
else if(text.at(i) == QChar('C'))
{
setFormat(i, 1, Qt::blue);
}
else if(text.at(i) == QChar('T'))
{
setFormat(i, 1, Qt::red);
}
}

}

myWindow::~myWindow()
{

}

wysota
7th June 2012, 10:30
What's this openTextWindow() method for? Where is myWindow::setFormat() defined?

Stanfillirenfro
7th June 2012, 11:50
openTextWindow() shoule not exist. Was a mistake from me.
I bring the following modifications:

colorSequences.h

#ifndef DEF_COLORSEQUENCES
#define DEF_COLORSEQUENCES

#include <QtGui>

class ColorSequences: public QSyntaxHighlighter
{
Q_OBJECT

public:
ColorSequences(QTextDocument *parent = 0);
~ColorSequences();

public slots:
void highlightBlock(const QString &text);

private:


};
#endif

myWindow.h file

#ifndef DEF_MYWINDOW
#define DEF_MYWINDOW

#include <QtGui>
#include "ColorerSequences.h"

class myWindow: public QMainWindow
{
Q_OBJECT

public:
myWindow();
~myWindow();

public slots:


private:
QWidget *m_centralWidget;
QTextEdit *m_myText;
ColorSequences *colorSequences;

};
#endif

colorSequences.cpp file

#include "ColorSequences.h"

ColorSequences::ColorSequences(QTextDocument *parent): QSyntaxHighlighter(parent)
{

}

void ColorSequences::highlightBlock(const QString &text)
{
for(int i = 0; i<text.size(); i++)
{
if(text.at(i) == QChar('A'))
{
setFormat(i, 1, Qt::green);
}
else if(text.at(i) == QChar('C'))
{
setFormat(i, 1, Qt::blue);
}
else if(text.at(i) == QChar('T'))
{
setFormat(i, 1, Qt::red);
}
}

}

ColorSequences::~ColorSequences()
{

}

myWindow.cpp file


#include "myWindow.h"
#include "ColorSequences.h"

myWindow::myWindow()
{
m_centralWidget = new QWidget;
m_centralWidget->setFixedSize(400, 200);
m_myText = new QTextEdit(this);
m_myText->setFixedSize(300, 127);

setCentralWidget(m_centralWidget)
colorSequences = new ColorSequences(m_monTexte->document());
}



myWindow::~myWindow()
{

}

wysota
7th June 2012, 15:37
And what the problem is now (apart from not using layouts)?

Stanfillirenfro
7th June 2012, 18:42
I have the main window and the TextEdit window on it, displaying my Qstring, but its characters are not color as expected. It was from the begining my main problem.
I would really appreciate your help.