Results 1 to 3 of 3

Thread: Changing the background color of a QGraphicsSimpleTextItem

  1. #1
    Join Date
    May 2011
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Changing the background color of a QGraphicsSimpleTextItem

    Hi, I have a class that inherits from the QGraphicsSimpleTextItem class. I cannot figure out how to make the background change when this happens. Right now, I figured out that by changing the pen I can make the text outlines change:

    Qt Code:
    1. text_item->setPen(QPen(QColor(Qt::black)));
    To copy to clipboard, switch view to plain text mode 

    Running this code will change the outlines of the text in text_item, but I want a more dramatic change. I am looking for a way to replace that line with something that will change the background of my text_item when I choose. I tried overwriting the paint() function inherited from QGraphicsItem like so:

    Qt Code:
    1. void MyTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. QBrush brush(Qt::white);
    4. painter->setBrush(brush);
    5. painter->drawRect(0,0,50,50);
    6. this->setPen(QPen(Qt::black));
    7. }
    To copy to clipboard, switch view to plain text mode 

    The first three lines of the function paint a white rectangle as the background (which is what I am aiming for), but setting the pen does nothing to display the text. So, I can either have the text with no change in background, or when I try to change the background the text doesn't show up. The fourth line in that function seems to do nothing. Is there any way I can have this class still display the text but also be able to change the background?

    Thanks in advance,
    psychocowtipper

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Changing the background color of a QGraphicsSimpleTextItem

    subclass the paint method as you have done and inside just draw the background as you like. Afterwards just call the paint method of the base class, the simple text item in your case and all should work as you want it to.

    And may be you need to change the pen outside the paint method, but I am not sure about it.

  3. The following user says thank you to Lykurg for this useful post:

    psychocowtipper (6th May 2011)

  4. #3
    Join Date
    May 2011
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing the background color of a QGraphicsSimpleTextItem

    Thank you so much! I was able to get it working just by changing my overloaded paint() function to:
    Qt Code:
    1. void MyTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3.  
    4. QBrush brush(Qt::white);
    5. painter->setBrush(brush);
    6. painter->drawRect(0,0,70,35);
    7.  
    8. QGraphicsSimpleTextItem::paint(painter,option,widget);
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. changing background color in qvfb
    By John Douma in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 31st March 2011, 19:30
  2. Changing color of only one tab
    By Tiansen in forum Qt Programming
    Replies: 1
    Last Post: 7th January 2011, 04:34
  3. changing button background color
    By Tomasz in forum Newbie
    Replies: 4
    Last Post: 27th November 2010, 04:09
  4. Changing the background color of a cell in a QTableView
    By scarleton in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2010, 13:23
  5. prob with changing QLineEdit background color
    By Ahmad in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2007, 12:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.