I have change the color of each line text in QTextWidget one by one
Unsolved
General and Desktop
-
I have change the color of each line text in QTextWidget one by one. I have multiple lines QtextEdit .
Could you please some one suggest how to do that
-
Hi
Did you use HTML or just plaintext ?
https://stackoverflow.com/questions/2857864/qtextedit-with-different-text-colors-qt-c -
I have lines
line1
line2
line3
intially lines are not editednow I have C++ function
colorEach:Line() {
getEachline from QtexLine and
change the color of ltext in QtextEdit}
-
QTextCursor cursor(textEdit->document()); cursor.movePosition(QTextCursor::Start); while(cursor.movePosition(QTextCursor::EndOfLine,QTextCursor::MoveAnchor)){ QTextCharFormat tempFormat = cursor.charFormat(); tempFormat.setBackground(Qt::yellow); cursor.setCharFormat(tempFormat); cursor.movePosition(QTextCursor::NoMove); //this might need to be cursor.movePosition(QTextCursor::NextCharacter) unsure }
-
@Qt-Enthusiast
an alternative to @VRonin examplevoid changeColors(QTextEdit *edit){ QString newText; QVector<QString> colors { "<font color=\"red\">", "<font color=\"green\">", "<font color=\"blue\">" }; QString endFont = "</font>"; QString ParagraphEnd = "</p>"; QString text = edit->toHtml(); auto ref = text.split(ParagraphEnd); for(int i(0); i < ref.size();i++){ QString s = ref.at(i); int insert = s.lastIndexOf(">"); s.insert(insert+1,colors.at(i%3)); s.append(endFont+ParagraphEnd); newText.append(s); } edit->setHtml(newText); }