Hi,
as already discussed here and here 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
) :{
_border_color
= QColor(0,
83,
235);
_background_color
= QColor(51,
122,
255);
}
SelectionFrame::~SelectionFrame()
{
}
{
Q_UNUSED(event);
pen.setWidth(5);
pen.setStyle(Qt::DashLine);
painter.begin(this);
painter.setPen(pen);
painter.setOpacity(0.5);
painter.setBrush(brush);
painter.drawRect(event->rect());
painter.end();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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?
Bookmarks