I want to give background color in my QGraphicsScene. For that, I have override drawBackground() method, and tried to set color but it is not working. Background color is not changing.
Here is my drawBackground() method.
Widget.h
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QGraphicsView
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected:
void drawBackground(QPainter *painter, const QRectF &rect);
};
Widget.cpp
Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
view = new QGraphicsView(this);
view->setScene(scene);
view->setDragMode(QGraphicsView::RubberBandDrag);
ui->verticalLayout_2->addWidget(view);
}
void Widget::drawBackground(QPainter *painter, const QRectF &rect)
{
painter->save();
painter->setBrush(QBrush(Qt::yellow));
painter->restore();
}
Can anyone help me ?