Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to change the background color of selected text in QGraphicsTextItem?
Qt 6.11 is out! See what's new in the release blog

[SOLVED] How to change the background color of selected text in QGraphicsTextItem?

Scheduled Pinned Locked Moved General and Desktop
18 Posts 3 Posters 25.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sam
    wrote on last edited by
    #2

    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 :)

    1 Reply Last reply
    0
    • C Offline
      C Offline
      conny
      wrote on last edited by
      #3

      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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #4

        Sorry I misunderstood the question. It will be helpful if you can attach an image that describes your requirement. Thanks :)

        1 Reply Last reply
        0
        • C Offline
          C Offline
          conny
          wrote on last edited by
          #5

          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 :)

          1 Reply Last reply
          0
          • C Offline
            C Offline
            conny
            wrote on last edited by
            #6

            I've still found no solution. Does anyone have some more hints?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sam
              wrote on last edited by
              #7

              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

              1 Reply Last reply
              0
              • W Offline
                W Offline
                Wilk
                wrote on last edited by
                #8

                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?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #9

                  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]

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    conny
                    wrote on last edited by
                    #10

                    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]

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      conny
                      wrote on last edited by
                      #11

                      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]

                      1 Reply Last reply
                      0
                      • W Offline
                        W Offline
                        Wilk
                        wrote on last edited by
                        #12

                        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.

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          conny
                          wrote on last edited by
                          #13

                          Thank you very much for that code. I'll try it and report back here.

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            conny
                            wrote on last edited by
                            #14

                            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 :)

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              Sam
                              wrote on last edited by
                              #15

                              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 :)

                              1 Reply Last reply
                              0
                              • C Offline
                                C Offline
                                conny
                                wrote on last edited by
                                #16

                                Already done. Thanks!

                                1 Reply Last reply
                                0
                                • W Offline
                                  W Offline
                                  Wilk
                                  wrote on last edited by
                                  #17

                                  Hello
                                  Thanks for a proper solution.

                                  1 Reply Last reply
                                  0
                                  • C Offline
                                    C Offline
                                    conny
                                    wrote on last edited by
                                    #18

                                    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.

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved