dsyleixa123
Posts: 1436
Joined: Mon Jun 11, 2018 11:22 am

qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sat May 15, 2021 5:02 pm

I don't see thorgh that messy QtGraphics syntax:
I have
QtGraphicsTextItem* text = scene->addText("Hello Wold!", QFont("Arial", 20) );

Now how can I change the color of this text to e.g. red?
sth like a simple
text->setTextColor(Qt::red)? But there is no such a thng to find! :evil:

Heater
Posts: 18202
Joined: Tue Jul 17, 2012 3:02 pm

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sat May 15, 2021 5:23 pm

If you look at the documentation here: https://doc.qt.io/qt-5/qgraphicstextitem.html you find a list of all the methods implemented by QGraphicsTextItem.

You won't find any colour setting methods there. But you will find a link to : "List of all members, including inherited members" https://doc.qt.io/qt-5/qgraphicstextitem-members.html

There you will find the method: setDefaultTextColor().

Which I believe does what you want.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1436
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sat May 15, 2021 5:28 pm

thank you very much,
but tbh: no, I actually don't want to change the default, but instead just set an optional different arbitrary color to a single arbitrary text.

But if that's the only way to do it... :roll:

and how to change then back to the standard default? :?

Heater
Posts: 18202
Joined: Tue Jul 17, 2012 3:02 pm

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sat May 15, 2021 5:53 pm

Somehow we have to get you to read the documentation yourself.

For example here: https://doc.qt.io/qt-5/qgraphicssimpletextitem.html we see a description of QGraphicsSimpleTextItem.

and we find:
The simple text item can have both a fill and an outline; setBrush() will set the text fill (i.e., text color), and setPen() sets the pen that will be used to draw the text outline. (The latter can be slow, especially for complex pens, and items with long text content.) If all you want is to draw a simple line of text, you should call setBrush() only, and leave the pen unset; QGraphicsSimpleTextItem's pen is by default Qt::NoPen.
So, setBrush() seems to be the thing you need to set the text color.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1436
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sat May 15, 2021 6:04 pm

unfortunately no,
neither
setBrush(Qt::red); // (compiler error: setBrush was not declared)
nor
text->setBrush(Qt::red); // (compiler error: has no member setBrush)
works
:shock: :?

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sat May 15, 2021 10:13 pm

It's a rich text graphics item, you style it using CSS.

auto myItem = new QGraphicsTextItem;

myItem->setHtml("<span style="color: red">Hello World</span>");

Or you use QGraphicsSimpleTextItem which you can use setBrush to select a colour.

dsyleixa123
Posts: 1436
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sun May 16, 2021 8:40 am

hmm, the html or CSS stuff is too cumbersome and too weird....
but do you mean then sth like
QGraphicsSimpleTextItem * text = scene->addText("Hello Wold!", QFont("Arial", 20) );
and then
text->setBrush(Qt::red);

update, tested:

that does neither work:
error: cannot convert QGraphicsTextItem* to QGraphicsSimpleTextItem* in initialization
but then I had a sudden inspiration about
QGraphicsSimpleTextItem * text = scene->addSimpleText("Hello Wold!", QFont("Arial", 20) );

and that worked! Thanks a lot! 8-)

dsyleixa123
Posts: 1436
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sun May 16, 2021 8:47 am

another question related to text formatting by color:

why do I have to use this odd color name syntax using a :: like
(a) Qt::red
eg., like above in
text->setBrush(Qt::red);
?

why not
(b) Qt.red
or
(c) Qt->red
?

IIRC, the :: is used for declarations or definitions of classes/members, and if "red" had been declared already as a Qt class member,
then I would expect (b) or (c) when calling this member method or value instead...? :shock: :?

Heater
Posts: 18202
Joined: Tue Jul 17, 2012 3:02 pm

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sun May 16, 2021 10:36 am

If you were writing Qt.red then "Qt" would have to be an object of the "Qt" class and "red" would have to be a property of that class.

If you were writing "Qt->red" the "Qt" would have to be a pointer to an object the "Qt" class.

What we have though is an enum "GlobalColor" in the "Qt" namespace. And "red" is an allowed value of that enum. Which happens to be 7. So we use "::" for namespace resolution.
https://doc.qt.io/qt-5/qt.html

You can see how it is defined in the Qt core lib sources here: https://code.woboq.org/qt5/qtbase/src/c ... ace.h.html
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1436
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sun May 16, 2021 11:28 am

aaah, a namespace, not a class - thank you!

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Sun May 16, 2021 8:22 pm

The CSS (QSS in reality) stylesheets are one of the most powerful features of Qt, they can make it considerably easier to modify the look of widgets than using other, more traditional methods.

My one major gripe with them is that the documentation for them is unbelievably poor, Qt's documentation is normally amongst the best out there but it's severely lacking detail with regards to stylesheets.

I make heavy use of them in my Ribbon(like-ish) control that I use in pingnoo, although there is a mix of custom painting and stylesheets, it's mostly stylesheets wherever possible, falling back to custom drawing when it's not possible to achieve the desired look with just a stylesheet.
l.jpg
l.jpg (179.7 KiB) Viewed 804 times
d.jpg
d.jpg (197.78 KiB) Viewed 804 times
Specifically with your case of a QGraphicsItem subclass, using stylesheets will allow you to format text much more easily.

dsyleixa123
Posts: 1436
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Mon May 17, 2021 8:01 am

I agree, that looks really awesome, absolutely professional design!
Last edited by dsyleixa123 on Mon May 17, 2021 8:21 am, edited 2 times in total.

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Mon May 17, 2021 8:19 am

dsyleixa123 wrote:
Mon May 17, 2021 8:01 am
I agree, that looks really awesome, absolutely professional design!
Thanks, Qt is so powerful. Although widgets have not received as much love as QML has, I still rarely find things that I need are missing. It can also be overwhelming for new users, I've been writing commercial software in it for 12+ years.

dsyleixa123
Posts: 1436
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: set the text color of QtGraphicsTextItem* text...?

Mon May 17, 2021 8:23 am

ah, ok, a professional programmer.. 8-)

I am just programming on an Arduino hobbyist's level ;-)

Return to “C/C++”