PDA

View Full Version : Custom QRubberBand with transparent background



mdomke
10th June 2010, 17:46
Hi,

as already discussed here (http://www.qtcentre.org/threads/21324-How-to-draw-QRubberBand-with-red-color?highlight=qrubberband) and here (http://www.qtcentre.org/threads/26140-Change-the-color-of-QRubberBand?highlight=qrubberband) it should be possible to paint modify the appearance of the QRubberBand by making a subclass and overwriting the paintEvent. The rubberband should be displayed on QGraphicsView to select some items on a QGraphicsScene. I'm able to modify the frame color but the background of the rubber band rect remains completely transparent (white). Here is my implementation of the QRubberBand subclass



#include "SelectionFrame.h"
#include <QPaintEvent>
#include <QPainter>
#include <QDebug>

SelectionFrame::SelectionFrame(Shape shape, QWidget *parent) :
QRubberBand(shape, parent)
{
_border_color = QColor(0, 83, 235);
_background_color = QColor(51, 122, 255);
}

SelectionFrame::~SelectionFrame()
{
}

void SelectionFrame::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);

QPainter painter;
QPen pen = QPen(_border_color);
pen.setWidth(5);
pen.setStyle(Qt::DashLine);

QBrush brush = QBrush(_background_color);

painter.begin(this);
painter.setPen(pen);
painter.setOpacity(0.5);
painter.setBrush(brush);
painter.drawRect(event->rect());
painter.end();
}


As the painter's pen has been set to dashed, I think I can see, that there is an additional border painted on top of the dashed line with the correctly set transparency. The background is however not painter. Does anyone have a suggestion how this can be done right?

mdomke
11th June 2010, 11:34
I thin I remained a little bit unclear about what I'm trying to achieve. The rubber bands background (the content of the selection rectangle) should be semi-transparent and have a special color. In my understanding the rubber bands default behavior, at least on my system, is to have a completely transparent content area with only the borders painted.