PDA

View Full Version : multicolor text in QPlainTextEdit



mastupristi
5th January 2011, 16:20
I need to draw several text line in a QPlainTextEdit, with different color.

I have the following class:

#include <QDialog>

namespace Ui {
class ProvaColori;
}

class ProvaColori : public QDialog
{
Q_OBJECT

public:
explicit ProvaColori(QWidget *parent = 0);
~ProvaColori();

private:
Ui::ProvaColori *ui;

public slots:
void RossoClicked(void);
void BluClicked(void);
void updateText(int, const QString &);

};

#include "provacolori.h"
#include "ui_provacolori.h"

ProvaColori::ProvaColori(QWidget *parent) :
QDialog(parent),
ui(new Ui::ProvaColori)
{
ui->setupUi(this);

connect(ui->RossoBtn, SIGNAL(clicked()), this, SLOT(RossoClicked()));
connect(ui->BluBtn, SIGNAL(clicked()), this, SLOT(BluClicked()));
}

ProvaColori::~ProvaColori()
{
delete ui;
}

void ProvaColori::RossoClicked(void)
{
updateText(Qt::red, "Red text");
}

void ProvaColori::BluClicked(void)
{
updateText(Qt::blue, "Blue text");
}

void ProvaColori::updateText(int col, const QString &s)
{
QTextCharFormat tf;
tf = ui->plainTextEdit->currentCharFormat();
tf.setTextOutline(QPen(col));
ui->plainTextEdit->setCurrentCharFormat(tf);
ui->plainTextEdit->appendPlainText(s);
}
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

high_flyer
5th January 2011, 16:33
I think you should use setForeground() and not setTextOutline() for the text color.

mastupristi
14th January 2011, 15:55
I think you should use setForeground() and not setTextOutline() for the text color.

Now works :)
I have changed in this way:

void ProvaColori::updateText(int col, const QString &s)
{
QTextCharFormat tf;
tf = ui->plainTextEdit->currentCharFormat();
tf.setForeground(QBrush((Qt::GlobalColor)col));
ui->plainTextEdit->setCurrentCharFormat(tf);
ui->plainTextEdit->appendPlainText(s);
}


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

Then I push Blue button
5761

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

How can Avoid this behaviour?

thanks

high_flyer
14th January 2011, 16:21
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?

mastupristi
14th January 2011, 17:00
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

high_flyer
14th January 2011, 21:37
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.