-3

I have a QString like this ">>> This is an Arrow." , now I want to have >>> in bold and red, How to do that ?

i have looked over some pages and found this syntax

QString redPart = QString("<span style=" color:#ff0000;">%1</span>").arg(">>>");

but it gives some error like ")" missing and, I am not able to figure it out.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
the_learnist
  • 69
  • 1
  • 8
  • Your problem is the second " ends the string literal. – drescherjm Aug 19 '20 at 17:58
  • See "[How to Set custom text Color in QTextEdit?](//stackoverflow.com/q/5757540/90527)", "[QTextEdit with different text colors (Qt / C++)](//stackoverflow.com/q/2857864/90527)", "[How to change a color of QString item in QListView](//stackoverflow.com/q/34685635/90527)", … – outis May 13 '22 at 20:34

2 Answers2

3

QString is not a visual component, and therefore does not have a font or color property. It's just a data structure that holds text. If you want to display that text, you can use one of the many visual objects like a QLabel.

The compiler error you're getting is probably because your use of quotation marks is incorrect. Try it like this instead:

QString redPart = QString("<span style='color:#ff0000;'>%1</span>").arg(">>>");
JarMan
  • 7,589
  • 1
  • 10
  • 25
  • Thanks, now what if i use this QString redPart in the item of QListWIdget, will it be coloured in red ?? – the_learnist Aug 19 '20 at 22:18
  • The QListWidgetItem should be able to handle rich text. Even if it can't, a QListWidgetItem supports setting a foreground color. – JarMan Aug 19 '20 at 22:26
  • actually it was not able to, i used this `QString redPart = QString("%1").arg(">>>"); ` and when i tried to print it in a textedit, it was working, i got `>>>` in red color, However when i tried to print it in a item it came in as `span style= " color:#ff0000;">>>` as text. – the_learnist Aug 19 '20 at 22:31
3

your error message means that you are missing a ) because you have to escape the quotes when using them in a string literal (\" instead of ") i.e. do something like

QString redPart = QString("<span style=\" color:#ff0000;\">%1</span>").arg(">>>"); 

on the other hand, Qstring is a class that has nothing to do with widgets or guis at all, so properties like color, bold format etc arent defined there, so you need to use a widget or something that you can display to the user then if widget: you can set the stylesheet, if a QLabel: you can set the text using richtext.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97