Results 1 to 5 of 5

Thread: Coloring each character of a QString with QSyntaxHighlighter

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Coloring each character of a QString with QSyntaxHighlighter

    Hi!
    I am facing a problem of coloring each character of a QString using QSyntaxHighlighter.
    Let's imagine the following QString:
    Qt Code:
    1. QString text = "L&TG3J";
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. #ifndef DEF_COLORSEQUENCES
    2. #define DEF_COLORSEQUENCES
    3.  
    4. #include <QtGui>
    5.  
    6. class ColorSequences: public QSyntaxHighlighter
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. ColorSequences(QTextDocument *parent = 0);
    12. ~ColorSequences();
    13.  
    14. public slots:
    15. void highlightBlock(const QString &text);
    16.  
    17. private:
    18.  
    19.  
    20. };
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    MyWindow.h file
    Qt Code:
    1. #ifndef DEF_MYWINDOW
    2. #define DEF_MYWINDOW
    3.  
    4. #include <QtGui>
    5. #include "ColorerSequences.h"
    6.  
    7. class myWindow: public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. myWindow();
    13. ~myWindow();
    14.  
    15. public slots:
    16. void openTextWindow();
    17.  
    18. private:
    19. QTextEdit *m_myText;
    20. QPushButton *m_showDetails;
    21. ColorSequences *colorSequences;
    22.  
    23. };
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    ColorSequences.cpp file
    Qt Code:
    1. #include "ColorSequences.h"
    2.  
    3. ColorSequences::ColorSequences(QTextDocument *parent): QSyntaxHighlighter(parent)
    4. {
    5.  
    6. }
    7.  
    8. void ColorSequences::highlightBlock(const QString &text)
    9. {
    10. for(int i = 0; i<text.size(); i++)
    11. {
    12. if(text.at(i) == QChar('A'))
    13. {
    14. setFormat(i, 1, Qt::green);
    15. }
    16. else if(text.at(i) == QChar('C'))
    17. {
    18. setFormat(i, 1, Qt::blue);
    19. }
    20. else if(text.at(i) == QChar('T'))
    21. {
    22. setFormat(i, 1, Qt::red);
    23. }
    24. }
    25.  
    26. }
    27.  
    28. ColorSequences::~ColorSequences()
    29. {
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    myWindow.cpp file
    Qt Code:
    1. #include "myWindow.h"
    2. #include "ColorSequences.h"
    3.  
    4. myWindow::myWindow()
    5. {
    6. m_myText = new QTextEdit(this);
    7. m_myText->setFixedSize(300, 127);
    8. colorSequences = new ColorSequences(m_monTexte->document());
    9.  
    10. connect(m_showDetails, SIGNAL(clicked()), this, SLOT(openTextWindow());
    11. }
    12.  
    13. void myWindow::openTextWindow()
    14. {
    15. for(int i = 0; i<text.size(); i++)
    16. {
    17. if(text.at(i) == QChar('A'))
    18. {
    19. setFormat(i, 1, Qt::green);
    20. }
    21. else if(text.at(i) == QChar('C'))
    22. {
    23. setFormat(i, 1, Qt::blue);
    24. }
    25. else if(text.at(i) == QChar('T'))
    26. {
    27. setFormat(i, 1, Qt::red);
    28. }
    29. }
    30.  
    31. }
    32.  
    33. myWindow::~myWindow()
    34. {
    35.  
    36. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Coloring each character of a QString with QSyntaxHighlighter

    What's this openTextWindow() method for? Where is myWindow::setFormat() defined?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Coloring each character of a QString with QSyntaxHighlighter

    openTextWindow() shoule not exist. Was a mistake from me.
    I bring the following modifications:

    colorSequences.h
    Qt Code:
    1. #ifndef DEF_COLORSEQUENCES
    2. #define DEF_COLORSEQUENCES
    3.  
    4. #include <QtGui>
    5.  
    6. class ColorSequences: public QSyntaxHighlighter
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. ColorSequences(QTextDocument *parent = 0);
    12. ~ColorSequences();
    13.  
    14. public slots:
    15. void highlightBlock(const QString &text);
    16.  
    17. private:
    18.  
    19.  
    20. };
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    myWindow.h file
    Qt Code:
    1. #ifndef DEF_MYWINDOW
    2. #define DEF_MYWINDOW
    3.  
    4. #include <QtGui>
    5. #include "ColorerSequences.h"
    6.  
    7. class myWindow: public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. myWindow();
    13. ~myWindow();
    14.  
    15. public slots:
    16.  
    17.  
    18. private:
    19. QWidget *m_centralWidget;
    20. QTextEdit *m_myText;
    21. ColorSequences *colorSequences;
    22.  
    23. };
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    colorSequences.cpp file
    Qt Code:
    1. #include "ColorSequences.h"
    2.  
    3. ColorSequences::ColorSequences(QTextDocument *parent): QSyntaxHighlighter(parent)
    4. {
    5.  
    6. }
    7.  
    8. void ColorSequences::highlightBlock(const QString &text)
    9. {
    10. for(int i = 0; i<text.size(); i++)
    11. {
    12. if(text.at(i) == QChar('A'))
    13. {
    14. setFormat(i, 1, Qt::green);
    15. }
    16. else if(text.at(i) == QChar('C'))
    17. {
    18. setFormat(i, 1, Qt::blue);
    19. }
    20. else if(text.at(i) == QChar('T'))
    21. {
    22. setFormat(i, 1, Qt::red);
    23. }
    24. }
    25.  
    26. }
    27.  
    28. ColorSequences::~ColorSequences()
    29. {
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    myWindow.cpp file

    Qt Code:
    1. #include "myWindow.h"
    2. #include "ColorSequences.h"
    3.  
    4. myWindow::myWindow()
    5. {
    6. m_centralWidget = new QWidget;
    7. m_centralWidget->setFixedSize(400, 200);
    8. m_myText = new QTextEdit(this);
    9. m_myText->setFixedSize(300, 127);
    10.  
    11. setCentralWidget(m_centralWidget)
    12. colorSequences = new ColorSequences(m_monTexte->document());
    13. }
    14.  
    15.  
    16.  
    17. myWindow::~myWindow()
    18. {
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Coloring each character of a QString with QSyntaxHighlighter

    And what the problem is now (apart from not using layouts)?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Coloring each character of a QString with QSyntaxHighlighter

    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.

Similar Threads

  1. QString Special Character Problem
    By hakiim35 in forum Qt Programming
    Replies: 1
    Last Post: 3rd October 2011, 14:38
  2. Replies: 5
    Last Post: 8th September 2011, 10:38
  3. Using QSyntaxHighlighter
    By chris_helloworld in forum Qt Programming
    Replies: 0
    Last Post: 5th October 2010, 11:22
  4. Replies: 8
    Last Post: 14th April 2010, 05:54
  5. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.