[SOLVED] How to change the background color of selected text in QGraphicsTextItem?
-
wrote on 30 Apr 2012, 06:57 last edited by
Sure, I've uploaded an image here: "Image":http://twitpic.com/9fisl6/full
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 :)
-
wrote on 4 May 2012, 10:40 last edited by
I've still found no solution. Does anyone have some more hints?
-
wrote on 6 May 2012, 08:47 last edited by
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 -
wrote on 6 May 2012, 23:26 last edited by
Hello
Did you try "QGraphicsTextItem::setTextInteractionFlags":http://qt-project.org/doc/qt-4.8/qgraphicstextitem.html#setTextInteractionFlags with "Qt::TextSelectableByMouse":http://qt-project.org/doc/qt-4.8/qt.html#TextInteractionFlag-enum as an argument?
Or did I misunderstand what do you want? -
wrote on 7 May 2012, 05:54 last edited by
Hi Wilk,
The attached image specifies his requirement.
[quote author="conny" date="1335769034"]Sure, I've uploaded an image here: "Image":http://twitpic.com/9fisl6/full
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 :)
[/quote] -
wrote on 7 May 2012, 08:08 last edited by
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.
[quote author="Wilk" date="1336346790"]Hello
Did you try "QGraphicsTextItem::setTextInteractionFlags":http://qt-project.org/doc/qt-4.8/qgraphicstextitem.html#setTextInteractionFlags with "Qt::TextSelectableByMouse":http://qt-project.org/doc/qt-4.8/qt.html#TextInteractionFlag-enum as an argument?
Or did I misunderstand what do you want?[/quote] -
wrote on 7 May 2012, 08:09 last edited by
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.
[quote author="Soumitra" date="1336294039"]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[/quote] -
wrote on 7 May 2012, 14:16 last edited by
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 @
class YourTextItem : public QGraphicsTextItem {
//...
QColor bgcolor;
//...
}void YourTextItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget) {
Q_UNUSED(widget);
bool is_selected = option->state & (QStyle::State_Selected | QStyle::State_HasFocus);if (is_selected) {
option->state &= ~QStyle::State_Selected;
}
QGraphicsTextItem::paint(painter,
option,
widget);
highlight (painter,
is_selected);
}void YourTextItem::highlight (QPainter *painter,
bool is_selected) {
if (is_selected) {
const qreal itemPenWidth = 1.0;
const qreal pad = itemPenWidth / 2;const qreal penWidth = 0; // cosmetic pen const QColor fgcolor = option->palette.windowText().color(); painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine)); painter->setBrush(Qt::NoBrush); painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad)); painter->setPen(QPen(option->palette.windowText(), 0, Qt::DashLine)); painter->setBrush(Qt::NoBrush); painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad));
}
}
@
Try this out, but I didn't test it. -
wrote on 7 May 2012, 15:54 last edited by
Thank you very much for that code. I'll try it and report back here.
-
wrote on 11 May 2012, 09:01 last edited by
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;
p.setBrush(QPalette::Active, QPalette::Highlight, QBrush(QColor("pink")));
p.setBrush(QPalette::Inactive, QPalette::Highlight, QBrush(QColor("pink")));
p.setBrush(QPalette::Active, QPalette::HighlightedText, QBrush(QColor("green")));
p.setBrush(QPalette::Inactive, QPalette::HighlightedText, QBrush(QColor("green")));
QApplication::setPalette(p, "QTextControl");
@Thanks everyone for the help :)
-
wrote on 11 May 2012, 09:12 last edited by
Hi Conny,
Good that you got the solution, and thanks for sharing the code so that we can also use the same. You can edit your first post and add [SOLVED] to the title.Happy Coding :)
-
wrote on 11 May 2012, 09:13 last edited by
Already done. Thanks!
-
wrote on 12 May 2012, 01:13 last edited by
Hello
Thanks for a proper solution. -
wrote on 14 May 2012, 07:59 last edited by
The only disadvantage of this solution is, that it does not really work during runtime. Setting the value during application start-up works, but changing it later has no effect.
For me this is currently not a problem, but I thought I should still mention it.
14/18