PDA

View Full Version : How to draw QRubberBand with red color



lni
27th May 2009, 19:16
Hi,

I am using QRubberBand to zoom the scene when user clicks and drags a rectangle area. How can I set the QRubberBand to different color, such as red?

Thanks

lni
27th May 2009, 22:54
Never mind, soloved!

aamer4yu
28th May 2009, 07:26
Can you post the solution too :rolleyes:

lni
28th May 2009, 20:45
Can you post the solution too :rolleyes:

You won't like it. Codes were developed in Linux, and after moved to Windows, I got rubber band filled with red, so now I have to use #ifdef... too bad...



class LinuxRubberBand : public QRubberBand
{
public:

LinuxRubberBand( Shape s, QWidget * p = 0 ) : QRubberBand( s, p ) {
QPalette palette;
palette.setBrush( QPalette::WindowText, QBrush( Qt::red ) );
setPalette(palette);
}

protected:

virtual void paintEvent( QPaintEvent * ) {
QStylePainter painter(this);

QStyleOptionFocusRect option;
option.initFrom( this );

painter.drawControl(QStyle::CE_FocusFrame, option);
}

};

QRubberBand* createRubberBand()
{
QRubberBand* rubberBand( 0 );

#ifdef Q_WS_WIN
rubberBand = new QRubberBand( QRubberBand::Rectangle, 0 ) ),
#else
rubberBand = new LinuxRubberBand( QRubberBand::Rectangle, 0 ) ),
#endif

return rubberBand;
}

aamer4yu
29th May 2009, 06:32
One question ...
why did you inherit ? you could have very well set the palette on the object of QRubberBand, isnt it ?

lni
29th May 2009, 17:04
One question ...
why did you inherit ? you could have very well set the palette on the object of QRubberBand, isnt it ?

I did that but it didn't work...

I switch to QStyleOptionFocusRect and it works (on Linux)...

zgulser
26th February 2010, 10:49
Hi,

As aamer pointed out, is there a way to draw QRubberBand object with another color without inheriting the QRubberBand?

I've tried to solve it using style sheet and palette but neither of them worked.

zgulser
9th March 2010, 14:42
Hi,


Regarding the solution posted above, is it possible to make the rubberband dashed line also by using QPalette or something?

aamer4yu
9th March 2010, 14:43
You may try subclassing QRubberBand and setting palette while drawing.

zgulser
9th March 2010, 15:31
Hi,

That's what I exactly did..but I couldn't find to how to set "dashed line" for QPalette.

aamer4yu
9th March 2010, 18:12
Check Qt::PenStyle.
QPainter::setPen

zgulser
10th March 2010, 08:03
I did it as well in the paintEvent method of my rubberband class but it didn't make any difference. I guess QPalette overwrites QPainter or something.

aamer4yu
10th March 2010, 08:12
May be if you show some code, or give compilable code we can see whats wrong

zgulser
10th March 2010, 08:46
I love to..


class CustomRubberBand : QRubberBand
{


CustomRubberBand( Shape s) : QRubberBand (s)
{
QPalette palette;
palette.setBrush(QPalette::WindowText, QBrush(Qt::red));
setPalette(palette);

repaint();
}

void paintEvent(QPaintEvent *)
{
QStylePainter painter(this);

QStyleOptionFocusRect option;

option.initFrom(this);

painter.begin(this);

QPen pen;
pen.setStyle(Qt::DashLine);
pen.setWidth(2);
pen.setColor(QColor(Qt::red));
painter.setPen(pen);

painter.drawControl(QStyle::CE_FocusFrame, option);
}

friend class MyGraphView;

};

aamer4yu
10th March 2010, 10:12
What if you simply draw a rectangle painter.drawRect(...)
instead of the painter.drawControl(QStyle::CE_FocusFrame, option);

??

zgulser
10th March 2010, 10:49
Simply worked beautifully. Thanks