PDA

View Full Version : QTextCursor - setTextColor()



Dalamar
20th February 2006, 18:34
I've ported my Simple Telnet Client (called Dalamud) from QT3 to QT4.

Before adding new features (see my previuos post (http://www.qtcentre.org/forum/showthread.php?t=687))
I want to correct one bad behaviuor.

I've a QTextEdit to display Telnet Ouput , I want it clickable for selecting text, but this way a click changes cursor position for inserting new text (insertPlainText()).
So I recoded the function for displaying output to reposition the cursor at the end of the document before inserting new Text.
It works, but I've got a very bad side-effect. Text seems to get One Color and never changes it even if another function calls setTextColor()...

Probably you need some code to help me better, I don't know how if it's better to choose some lines of code or to attach all the sources.
Now I try the first option...

The Output - QTextEdit


OutText::OutText(QWidget * parent): QTextEdit(parent)
{
setSizePolicy(QSizePolicy::Expanding,QSizePolicy:: Expanding);
//Settaggio Font
QFont Font=currentFont();
Font.setFamily("Courier New");
setCurrentFont(Font);
//Settaggio dei Colori
QPalette Pal;
Pal=palette();
Pal.setColor(QPalette::Base,Qt::black);
setPalette(Pal);
// setPaper(QBrush("black"));
setTextColor(Qt::green);
insertPlainText("STIKAZZI");
setTextColor(Qt::white);
insertPlainText("!");
setReadOnly(TRUE);
}

void OutText::writeIn(QString str)
{
QTextCursor Cursor=textCursor();
Cursor.setPosition(document()->toPlainText().length());
setTextCursor(Cursor);

insertPlainText(str);
QScrollBar* Vbar;
Vbar=verticalScrollBar();
Vbar->setValue(Vbar->maximum());
}


The slot that would change Text Color (it's on the parent class of OutText, the instance of OutText it's called OutT)



void MainDialog::changeColor()
{
QColor NewColor;
if (Telnet.Color.high)
{
switch(Telnet.Color.foreground)
{
case Foreground::BLACK:
NewColor=Qt::gray;
break;
case Foreground::RED:
NewColor=Qt::red;
break;
case Foreground::GREEN:
NewColor=Qt::green;
break;
case Foreground::YELLOW:
NewColor=Qt::yellow;
break;
case Foreground::BLUE:
NewColor=Qt::blue;
break;
case Foreground::MAGENTA:
NewColor=Qt::magenta;
break;
case Foreground::CYAN:
NewColor=Qt::cyan;
break;
case Foreground::WHITE:
NewColor=Qt::white;
break;
}
}
else
{
switch(Telnet.Color.foreground)
{
case Foreground::BLACK:
NewColor=Qt::black;
break;
case Foreground::RED:
NewColor=Qt::darkRed;
break;
case Foreground::GREEN:
NewColor=Qt::darkGreen;
break;
case Foreground::YELLOW:
NewColor=Qt::darkYellow;
break;
case Foreground::BLUE:
NewColor=Qt::darkBlue;
break;
case Foreground::MAGENTA:
NewColor=Qt::darkMagenta;
break;
case Foreground::CYAN:
NewColor=Qt::darkCyan;
break;
case Foreground::WHITE:
NewColor=Qt::lightGray;
break;
}
}
OutT.setTextColor(NewColor);
}


Screenshots (http://spazioinwind.libero.it/aidoru/problem.html)