Results 1 to 6 of 6

Thread: multicolor text in QPlainTextEdit

  1. #1
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default multicolor text in QPlainTextEdit

    I need to draw several text line in a QPlainTextEdit, with different color.

    I have the following class:
    Qt Code:
    1. #include <QDialog>
    2.  
    3. namespace Ui {
    4. class ProvaColori;
    5. }
    6.  
    7. class ProvaColori : public QDialog
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit ProvaColori(QWidget *parent = 0);
    13. ~ProvaColori();
    14.  
    15. private:
    16. Ui::ProvaColori *ui;
    17.  
    18. public slots:
    19. void RossoClicked(void);
    20. void BluClicked(void);
    21. void updateText(int, const QString &);
    22.  
    23. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "provacolori.h"
    2. #include "ui_provacolori.h"
    3.  
    4. ProvaColori::ProvaColori(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::ProvaColori)
    7. {
    8. ui->setupUi(this);
    9.  
    10. connect(ui->RossoBtn, SIGNAL(clicked()), this, SLOT(RossoClicked()));
    11. connect(ui->BluBtn, SIGNAL(clicked()), this, SLOT(BluClicked()));
    12. }
    13.  
    14. ProvaColori::~ProvaColori()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void ProvaColori::RossoClicked(void)
    20. {
    21. updateText(Qt::red, "Red text");
    22. }
    23.  
    24. void ProvaColori::BluClicked(void)
    25. {
    26. updateText(Qt::blue, "Blue text");
    27. }
    28.  
    29. void ProvaColori::updateText(int col, const QString &s)
    30. {
    31. tf = ui->plainTextEdit->currentCharFormat();
    32. tf.setTextOutline(QPen(col));
    33. ui->plainTextEdit->setCurrentCharFormat(tf);
    34. ui->plainTextEdit->appendPlainText(s);
    35. }
    To copy to clipboard, switch view to plain text mode 
    don't mind on why updateText() is a slot.

    What happen is that text is drown black, but the font is changed.

    Where I am wrong?
    How can I change the text color?

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multicolor text in QPlainTextEdit

    I think you should use setForeground() and not setTextOutline() for the text color.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multicolor text in QPlainTextEdit

    Quote Originally Posted by high_flyer View Post
    I think you should use setForeground() and not setTextOutline() for the text color.
    Now works
    I have changed in this way:
    Qt Code:
    1. void ProvaColori::updateText(int col, const QString &s)
    2. {
    3. tf = ui->plainTextEdit->currentCharFormat();
    4. tf.setForeground(QBrush((Qt::GlobalColor)col));
    5. ui->plainTextEdit->setCurrentCharFormat(tf);
    6. ui->plainTextEdit->appendPlainText(s);
    7. }
    To copy to clipboard, switch view to plain text mode 

    now it happends a strange thing:
    redSelected.png
    I push red button, and a red string will appear in plainTextEdit, the I select some characters.

    Then I push Blue button
    redSelectedBlue.png

    And then I deselect
    RedBlue.png
    And what was previously selected become blue, even if it was red.

    How can Avoid this behaviour?

    thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multicolor text in QPlainTextEdit

    Then I push Blue button
    Well, what should happen when you push the blue button?
    It looks like the blue button is coloring the selected text in blue, which makes sense.
    What is the behavior you would like to have?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multicolor text in QPlainTextEdit

    Quote Originally Posted by high_flyer View Post
    Well, what should happen when you push the blue button?
    It looks like the blue button is coloring the selected text in blue, which makes sense.
    What is the behavior you would like to have?
    I need that only new text will be blue, and red selected text will remain red

    bye

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multicolor text in QPlainTextEdit

    Well, you need to build some logic to do that.
    If for example, it is true for your case to say, that new text is only the text in the last line, then its easy, you just don't do anything in the blue slot if the text is in the last line.
    If the criteria for "new text" is more complex, then you will have to adjust that logic to comply with your criteria.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Selecting Text QPlainTextEdit
    By smhall316 in forum Newbie
    Replies: 0
    Last Post: 7th December 2010, 20:09
  2. How to limit the input text in QPlaintextedit?
    By sudhansu in forum Qt Programming
    Replies: 4
    Last Post: 10th March 2010, 10:42
  3. read text from QPlainTextEdit
    By chs in forum Newbie
    Replies: 2
    Last Post: 21st November 2009, 12:07
  4. QPlainTextEdit text format question
    By vieraci in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2009, 12:19
  5. QPlainTextEdit reload text
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2009, 14:24

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.