PDA

View Full Version : QRubberBand make background invisible



FlameFurious
29th December 2010, 22:32
I'm trying to modify the painter method of QRubberBand to fit my taste of style, and for the most part it works just like it should, but I have tried to get the background to be invisible, but I have found no method that I had any luck with.

With no painting method for the background it is a transparent white.



class CustomRubberBand : public QRubberBand
{
public:
CustomRubberBand(Shape s, QWidget * p = 0) : QRubberBand(s, p)
{
}

protected:
void paintEvent(QPaintEvent *pe)
{
Q_UNUSED(pe);

QPainter painter;
QPen pen(Qt::red, 6);
pen.setStyle(Qt::SolidLine);

painter.begin(this);
painter.setPen(pen);
painter.drawRect(pe->rect());
painter.end();
}
};


http://img707.imageshack.us/img707/7924/examplebackground.png (http://img707.imageshack.us/i/examplebackground.png/)

I've attached an example where you would be able to see what I'm talking about.
Does anyone know how to make it invisible?

high_flyer
30th December 2010, 09:45
If what you want is a non transparent rubber band, I think setting autoFillBackground will do the trick.

aamer4yu
30th December 2010, 10:29
Did you try setting brush to transparent ?
painter.setBrush(Qt::transparent)

FlameFurious
30th December 2010, 11:10
@high_flyer
I want the background to be completely invisible and the borders to fully visible. Tried autoFillBackground, but it doesn't seem to have any effect at all.

@aamer4yu
Just tried and it have no effect at all. No matter what I do the background is always visible.

Even if I do this, which have an empty paint method, it still gets drawn. I get a white semi-transparent background with no border, while the default is a blue semi-transparent background and a border.


class CustomRubberBand : public QRubberBand
{
public:
CustomRubberBand(Shape s, QRubberBand * p = 0) : QRubberBand(s, p)
{
}
protected:
void paintEvent(QPaintEvent *pe)
{
Q_UNUSED(pe);
}
};

high_flyer
30th December 2010, 11:20
What happens if in the original code you posted you set a brush (non transparent)?

EDIT:

By default a rectangular rubber band (s is Rectangle) will use a mask, so that a small border of the rectangle is all that is visible. Some styles (e.g., native Mac OS X) will change this and call QWidget::setWindowOpacity() to make a semi-transparent filled selection rectangle.
You might want try that.

FlameFurious
30th December 2010, 11:29
It will turn green, the background, if I set the brush as following:

painter.setBrush(QBrush(QColor(0, 255, 0, 255)));

So I tried doing this in the hope of getting rid of the background color, but it is still white:

painter.setBrush(QBrush(QColor(0, 0, 0, 0)));

And I just noticed that the border actually gets the same amount of transparency as the background. My plan was to have a transparent background and fully visible borders.

high_flyer
30th December 2010, 11:47
try:

painter.setBrush(QBrush(QColor(0, 0, 0, 255)));

FlameFurious
30th December 2010, 12:09
painter.setBrush(QBrush(QColor(0, 0, 0, 255)));
That makes it black instead of white.



CustomRubberBand *selection = new CustomRubberBand(QRubberBand::Rectangle, 0);
selection->setGeometry(150,150,250,250);
selection->show();
There is no effect with "selection->setWindowOpacity(0.0);" at all.

high_flyer
30th December 2010, 12:12
That makes it black instead of white.

Correct - I thought that this is what you wanted?!

I am confused - if you don't want it opaque, and not transparent, what do you want then?

FlameFurious
30th December 2010, 13:22
I want the background to be completely transparent, not semi, which is what it is stuck at right now. It should be a transparent box with red none transparent borders.

high_flyer
30th December 2010, 13:58
I see...
and what does a brush with any other color than black and full transparency do?
for example:

painter.setBrush(QBrush(QColor(0, 255, 0, 0)));

FlameFurious
30th December 2010, 13:58
They do nothing, the white semi-transparent background is still there :(

high_flyer
30th December 2010, 13:59
it seems to be the semi transparent part is a child of the rubber band.
This would explain why an empty paintEvent() still draws it like that.
Try getting the children list, see if its empty, and if not, set the transparency on the children.
Better yet - look at the QRubberBand source.

FlameFurious
30th December 2010, 14:11
Sorry, didn't see that last part, my mistake.

How would I iterate through the QRubberBand's children?

I'll give the source a try.

If this won't result in a positive result I'll just use four QWidgets to make a box instead.

high_flyer
30th December 2010, 14:16
How would I iterate through the QRubberBand's children?
http://doc.trolltech.com/4.7/qobject.html#children


If this won't result in a positive result I'll just use four QWidgets to make a box instead.
Wouldn't a one QFrame be better?

FlameFurious
30th December 2010, 14:27
Thanks, I'll try it at some point in the new year.

I made the function, for testing purpose, and used QWidgets without give a thought to QFrame, but indeed they are better suited for the task. For now this is the method I will use, since it is much simpler to use.

Thanks for your time. :)