PDA

View Full Version : Restore default text color in QTextEdit (with Qss style)



leander
9th May 2010, 14:14
Hello,
I have some difficulty with a problem which seem simple (at beginning).
I created a console class (for logging) which inherits from QTextEdit. I have some Qt property for text color (one for each type of message). This property could be initialize by Qss.

In the function where I append the text, I want to save the current text color, modify it, and restore it (I really need this feature).

See the code below (that doesn't work):

// There, the color is invalid because I didn't defined it in Qss for
// my class (I want to not defined in certain case to manage
// default style without qss.).
color = textcolor();
// I change the color and display new text
settextcolor(myNewColor);
append(strText);
// But code below doesn't restore the previous color
// (I Think it's because the color is Invalid)
settextcolor(color);


I have tried these too (but doesn't work...) :


// Try to save context
QTextCharFormat oFormat = currentCharFormat();

setTextColor(oColor);
append(strText);

// But, the code below doesn't work.
setCurrentCharFormat(oFormat);

I also try with QPalette (palette, setpalette()), but it doesn't work too.

I have search for the same mechanism as QPainter with save() and restore(), but I didn't thind any solution.

Is someone known how to save text color, backgound color and font in a context and restore them after modification ?
The problem in my case, is that If the user (in Qss) doesn't set a color for my property, Qt initialize it with invalid color and take the parent in Qss (but it do it in intern, textcolor() return invalid color). And I didn't found solution to restore this default color.

I think there iis a solution, but I didn't find it.
I hope someone could help me.