[SOLVED] How to change the background color of selected text in QGraphicsTextItem?
Page |
1 |
Hi,
I’m looking for a way to change the background color of selected text in QGraphicsTextItem. I’ve tried overriding paint() with the following code. Unfortunately it doesn’ work for me.
- void MyTextEditor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
- {
- // Remove the HasFocus style state, to prevent the dotted line from being drawn.
- // Setting color does not work
- style->palette = p;
- }
I’m trying to do this for quite some time already, so I’m happy about all suggestions.
Thanks,
Conny
17 replies
Hi,
Instead of setting the QPalette you can use QPainter->fillRect(), Try the following code:
.h
- {
- public:
- MyGraphicsTextItem();
- };
.cpp
- {
- }
- void MyGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
- {
- // Remove the HasFocus style state, to prevent the dotted line from being drawn.
- // Setting color does not work
- /* QPalette p = style->palette;
- p.setColor(QPalette::HighlightedText, QColor("green"));
- p.setColor(QPalette::Highlight, QColor("red"));
- p.setColor(QPalette::Text, QColor("yellow"));
- style->palette = p;
- */
- }
I commented few lines of your code. Check if this works :)
Thanks a lot for your reply. Unfortunately this colors the complete background of the text item. What I want is setting the color of the selection only. I’m using this code on Harmattan where the default selection color is a dark blue. Using your code the selection is still blue but the whole background is red.
If you have more tips, I’d be happy to hear them :)
Thanks!
Conny
Sure, I’ve uploaded an image here: Image [twitpic.com]
In words: I want to change the highlight/selection color that a word get marked with if I select it for copy&paste. Well, I think the image makes it clearer :)
Hello
Did you try QGraphicsTextItem::setTextInteractionFlags [qt-project.org] with Qt::TextSelectableByMouse [qt-project.org] as an argument?
Or did I misunderstand what do you want?
Hi Wilk,
The attached image specifies his requirement.
Sure, I’ve uploaded an image here: Image [twitpic.com]In words: I want to change the highlight/selection color that a word get marked with if I select it for copy&paste. Well, I think the image makes it clearer :)
Hi Wilk,
I’ve set those flags and the text is selectable. Only the selection color is always blue and I need to change that color.
Hello Did you try QGraphicsTextItem::setTextInteractionFlags [qt-project.org] with Qt::TextSelectableByMouse [qt-project.org] as an argument? Or did I misunderstand what do you want?
Hi Soumitra,
that might be an option and I’ll try it. However I fear that painting that rectangle myself will interfere with the painting of the original text selection.
Hi conny,
One solution can be to override the mousePressEvent() and mouseReleaseEvent() of yourGraphicsTextItem, then create a QRect from the event->pos() and fill the rectangle with the color.Note: This is just an idea not yet tested!!!!
Regards
Soumitra
Hello again.
If you don’t like blue background color, then look into Qt sources.
You will find out, that background color is selected automaticaly at qt_graphicsItem_highlightSelected method of QGraphicsItem, which is called from paint method of QGraphicsTextItem.
So one of the possible solutions is:
- Create class, derived from QGraphicsTextItem.
- Override paint method with something like
- //...
- QColor bgcolor;
- //...
- }
- Q_UNUSED(widget);
- if (is_selected) {
- }
- option,
- widget);
- highlight (painter,
- is_selected);
- }
- bool is_selected) {
- if (is_selected) {
- const qreal itemPenWidth = 1.0;
- const qreal pad = itemPenWidth / 2;
- const qreal penWidth = 0; // cosmetic pen
- painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad));
- painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad));
- }
- }
Try this out, but I didn’t test it.
Hi Wilk,
I found time to look at your code now. Sorry for the delay. Unfortunately all it does is changing the style of the text item itself. It does not change the text selection inside the text item.
However, I’ve followed your advice and read the Qt code until I found out where the styling actually happens. If anyone is interested have a look at:
- QTextDocumentLayout::draw() and
- QTextControl::getPaintContext()
To make a long story short, I finally found a way that works. The solution is way easier than I thought and you don’t even have to override anything. Here is the code:
- QPalette p;
Thanks everyone for the help :)
You must log in to post a reply. Not a member yet? Register here!



