Results 1 to 15 of 15

Thread: Change the color of QRubberBand

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Change the color of QRubberBand

    Hi to all I need to select a region over my waveform display so someone suggested to me to use QRubberBand. I tried it and it works but I would change the selection color to transparent red so I found in internet this class

    Qt Code:
    1. class WfRubberBand : public QRubberBand
    2. {
    3. public:
    4.  
    5. WfRubberBand( Shape s, QWidget * p = 0 ) : QRubberBand( s, p )
    6. {
    7. QPalette palette;
    8. //palette.setBrush( QPalette::WindowText, QBrush( Qt::red ) );
    9. palette.setBrush( QPalette::Foreground, QBrush( Qt::red ) );
    10. setPalette(palette);
    11. }
    12.  
    13. protected:
    14.  
    15. virtual void paintEvent( QPaintEvent * )
    16. {
    17. QStylePainter painter(this);
    18.  
    19. option.initFrom( this );
    20.  
    21. painter.drawControl(QStyle::CE_FocusFrame, option);
    22. }
    23. };
    To copy to clipboard, switch view to plain text mode 

    I tested it and effectively it draw a red rectangle but filled and not transparent so I added this line
    Qt Code:
    1. setWindowOpacity( 0.0 );
    To copy to clipboard, switch view to plain text mode 
    without any effects.

    How can I do to have a transparency?
    May be should I use composition instead of rubberband?
    Best Regards,
    Franco
    Franco Amato

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change the color of QRubberBand

    Try this -
    painter.setOpacity(0.5)... in the paintEvent

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change the color of QRubberBand

    Quote Originally Posted by aamer4yu View Post
    Try this -
    painter.setOpacity(0.5)... in the paintEvent
    It doesn't work.
    Here the new code:

    Qt Code:
    1. class WfRubberBand : public QRubberBand
    2. {
    3. public:
    4.  
    5. WfRubberBand( Shape s, QWidget * p = 0 ) : QRubberBand( s, p )
    6. {
    7. QPalette palette;
    8. //palette.setBrush( QPalette::WindowText, QBrush( Qt::red ) );
    9. palette.setBrush( QPalette::Foreground, QBrush( Qt::red ) );
    10. setPalette(palette);
    11. }
    12.  
    13. protected:
    14.  
    15. virtual void paintEvent( QPaintEvent * )
    16. {
    17. QStylePainter painter(this);
    18.  
    19. option.initFrom( this );
    20.  
    21. painter.drawControl(QStyle::CE_FocusFrame, option);
    22. painter.setOpacity(0.5); // from aamer4yu
    23. }
    24. };
    To copy to clipboard, switch view to plain text mode 

    Which can be the problem?
    Franco Amato

  4. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change the color of QRubberBand

    Well seems that this problem has no solutions. So I definitively have to use composition to select a region with a color and transparency different from the default in case of rubberBand.
    Franco Amato

  5. #5
    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: Change the color of QRubberBand

    Maybe you wanna call setOpacity() before you paint the rect.

  6. #6
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change the color of QRubberBand

    Quote Originally Posted by Lykurg View Post
    Maybe you wanna call setOpacity() before you paint the rect.
    Yes this solved the problem thank you very much.
    Another interesting question. Is possible to give to the selection a border of a specified color?

    Regards
    Franco Amato

  7. #7
    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: Change the color of QRubberBand

    Quote Originally Posted by franco.amato View Post
    Is possible to give to the selection a border of a specified color?
    Of course: QPainter::setPen().

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change the color of QRubberBand

    Quote:
    Originally Posted by aamer4yu View Post
    Try this -
    painter.setOpacity(0.5)... in the paintEvent
    It doesn't work.
    Hope it does now

  9. #9
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change the color of QRubberBand

    Quote Originally Posted by aamer4yu View Post
    Hope it does now
    Sure, thank you.
    I would also change the border color.
    Lykurg suggested to me to use QPainter::setPen() but how? I only would
    draw the border of a specified color and not all.

    Best
    Franco Amato

  10. #10
    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: Change the color of QRubberBand

    Quote Originally Posted by franco.amato View Post
    Lykurg suggested to me to use QPainter::setPen() but how?
    Come on, that's not a big deal, it like you set the opacity. And since you obviously don't need a style aware rubber band, get rid of that QStyleOptionFocusRect stuff and draw that rubber rect yourself using QPainter::drawRect(). This will also increase your speed.

  11. #11
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change the color of QRubberBand

    Quote Originally Posted by Lykurg View Post
    Come on, that's not a big deal, it like you set the opacity. And since you obviously don't need a style aware rubber band, get rid of that QStyleOptionFocusRect stuff and draw that rubber rect yourself using QPainter::drawRect(). This will also increase your speed.
    But wich QRect must I pass to the drawRect method?
    Franco Amato

  12. #12
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change the color of QRubberBand

    I changed the code so but it doesn't work well:
    Qt Code:
    1. class WfRubberBand : public QRubberBand
    2. {
    3. public:
    4.  
    5. WfRubberBand( Shape s, QWidget * p = 0 ) : QRubberBand( s, p )
    6. {
    7. QPalette palette;
    8. //palette.setBrush( QPalette::WindowText, QBrush( Qt::red ) );
    9. palette.setBrush( QPalette::Foreground, QBrush( Qt::red ) );
    10. setPalette(palette);
    11. }
    12.  
    13. protected:
    14.  
    15. virtual void paintEvent( QPaintEvent *pe )
    16. {
    17. QStylePainter painter( this );
    18. QRect rect = pe->rect();
    19.  
    20. QPen pen( Qt::blue, 1 ); // blue solid line, 1 pixels wide
    21. painter.setPen( pen );
    22. // QStyleOptionFocusRect option;
    23. // option.initFrom( this );
    24. painter.setOpacity( 0.3 );
    25. // painter.drawControl(QStyle::CE_FocusFrame, option);
    26. painter.drawRect( rect );
    27. }
    28. };
    To copy to clipboard, switch view to plain text mode 

    The rect is not filled with the red color, but it's empty.
    Where my code is wrong?
    Franco Amato

  13. #13
    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: Change the color of QRubberBand

    Use QPainter instead of QStylePainter since you don't use it's possibilities. And then use QPainter::setBrush() to define the fill color.

  14. #14
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change the color of QRubberBand

    Quote Originally Posted by Lykurg View Post
    Use QPainter instead of QStylePainter since you don't use it's possibilities. And then use QPainter::setBrush() to define the fill color.
    It works,
    thank you
    Franco Amato

  15. #15
    Join Date
    Aug 2011
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Change the color of QRubberBand

    Hi

    just for those not wanting to re-implement a paint event for changing colors:

    calling
    Qt Code:
    1. setStyleSheet("selection-background-color: yellow");
    To copy to clipboard, switch view to plain text mode 
    on a QRubberBand changes the background color of the selected area to yellow as expected.

    Hope this will help someone.

    Clément.

  16. The following user says thank you to clement for this useful post:

    mvuori (4th August 2011)

Similar Threads

  1. Replies: 2
    Last Post: 30th June 2009, 17:08
  2. Change database data in the QTabelView
    By sophister in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2009, 16:40
  3. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 16:56
  4. statusBar() message color change
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 23:20
  5. Can't change the text display of a QTreeview item.
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2006, 01:03

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.