[SOLVED] How to change the background color of selected text in QGraphicsTextItem?
-
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)
{
QStyleOptionGraphicsItem style = const_cast<QStyleOptionGraphicsItem>(option);// Remove the HasFocus style state, to prevent the dotted line from being drawn. style->state &= ~QStyle::State_HasFocus; // 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; QGraphicsTextItem::paint(painter, style, widget);
}
@I'm trying to do this for quite some time already, so I'm happy about all suggestions.
Thanks,
Conny -
Hi,
Instead of setting the QPalette you can use QPainter->fillRect(), Try the following code:.h
@class MyGraphicsTextItem : public QGraphicsTextItem
{
public:
MyGraphicsTextItem();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);};@
.cpp
@QRectF MyGraphicsTextItem::boundingRect() const
{
return QRectF(0,0,60,30); //set the bounding rect for your text item
}@
@void MyGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{QStyleOptionGraphicsItem* style = const_cast<QStyleOptionGraphicsItem*>(option); // Remove the HasFocus style state, to prevent the dotted line from being drawn. style->state &= ~QStyle::State_HasFocus; // 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; */ painter->fillRect(option->rect,Qt::red); QGraphicsTextItem::paint(painter, style, widget);
}@
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":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 :)
-
-
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? -
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] -
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] -
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] -
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. -
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 :)
-
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.