View Full Version : Change the color of QRubberBand
franco.amato
1st December 2009, 01:37
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
class WfRubberBand : public QRubberBand
{
public:
WfRubberBand( Shape s, QWidget * p = 0 ) : QRubberBand( s, p )
{
QPalette palette;
//palette.setBrush( QPalette::WindowText, QBrush( Qt::red ) );
palette.setBrush( QPalette::Foreground, QBrush( Qt::red ) );
setPalette(palette);
}
protected:
virtual void paintEvent( QPaintEvent * )
{
QStylePainter painter(this);
QStyleOptionFocusRect option;
option.initFrom( this );
painter.drawControl(QStyle::CE_FocusFrame, option);
}
};
I tested it and effectively it draw a red rectangle but filled and not transparent so I added this line
setWindowOpacity( 0.0 ); without any effects.
How can I do to have a transparency?
May be should I use composition instead of rubberband?
Best Regards,
Franco
aamer4yu
1st December 2009, 04:37
Try this -
painter.setOpacity(0.5)... in the paintEvent
franco.amato
1st December 2009, 16:00
Try this -
painter.setOpacity(0.5)... in the paintEvent
It doesn't work.
Here the new code:
class WfRubberBand : public QRubberBand
{
public:
WfRubberBand( Shape s, QWidget * p = 0 ) : QRubberBand( s, p )
{
QPalette palette;
//palette.setBrush( QPalette::WindowText, QBrush( Qt::red ) );
palette.setBrush( QPalette::Foreground, QBrush( Qt::red ) );
setPalette(palette);
}
protected:
virtual void paintEvent( QPaintEvent * )
{
QStylePainter painter(this);
QStyleOptionFocusRect option;
option.initFrom( this );
painter.drawControl(QStyle::CE_FocusFrame, option);
painter.setOpacity(0.5); // from aamer4yu
}
};
Which can be the problem?
franco.amato
1st December 2009, 17:19
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.
Lykurg
1st December 2009, 17:46
Maybe you wanna call setOpacity() before you paint the rect.
franco.amato
1st December 2009, 19:18
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
Lykurg
1st December 2009, 21:49
Is possible to give to the selection a border of a specified color?
Of course: QPainter::setPen().
aamer4yu
2nd December 2009, 07:07
Quote:
Originally Posted by aamer4yu View Post
Try this -
painter.setOpacity(0.5)... in the paintEvent
It doesn't work.
Hope it does now :cool:
franco.amato
2nd December 2009, 21:35
Hope it does now :cool:
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
Lykurg
2nd December 2009, 22:21
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.
franco.amato
3rd December 2009, 05:20
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
3rd December 2009, 05:31
I changed the code so but it doesn't work well:
class WfRubberBand : public QRubberBand
{
public:
WfRubberBand( Shape s, QWidget * p = 0 ) : QRubberBand( s, p )
{
QPalette palette;
//palette.setBrush( QPalette::WindowText, QBrush( Qt::red ) );
palette.setBrush( QPalette::Foreground, QBrush( Qt::red ) );
setPalette(palette);
}
protected:
virtual void paintEvent( QPaintEvent *pe )
{
QStylePainter painter( this );
QRect rect = pe->rect();
QPen pen( Qt::blue, 1 ); // blue solid line, 1 pixels wide
painter.setPen( pen );
// QStyleOptionFocusRect option;
// option.initFrom( this );
painter.setOpacity( 0.3 );
// painter.drawControl(QStyle::CE_FocusFrame, option);
painter.drawRect( rect );
}
};
The rect is not filled with the red color, but it's empty.
Where my code is wrong?
Lykurg
3rd December 2009, 07:28
Use QPainter instead of QStylePainter since you don't use it's possibilities. And then use QPainter::setBrush() to define the fill color.
franco.amato
3rd December 2009, 07:47
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
clement
4th August 2011, 16:30
Hi
just for those not wanting to re-implement a paint event for changing colors:
calling
setStyleSheet("selection-background-color: yellow"); on a QRubberBand changes the background color of the selected area to yellow as expected.
Hope this will help someone.
Clément.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.