Stanfillirenfro
7th June 2012, 07: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()
{
}
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()
{
}