Question

What is the difference in the following code,

  QGraphicsScene * scence = new QGraphicsScene();

   QBrush *brush = new QBrush((QColor(60,20,20)));
   scence->setBackgroundBrush(*brush);

   QGraphicsView *view = new QGraphicsView();
   view->setScene(scence);
   //view->setBackgroundBrush(*brush);
   //view->setCacheMode(QGraphicsView::CacheBackground);
   view->showFullScreen();

gives black color background

  QGraphicsScene * scence = new QGraphicsScene();

   QBrush *brush = new QBrush();
   brush->setColor(QColor(60,20,20));
   scence->setBackgroundBrush(*brush);

   QGraphicsView *view = new QGraphicsView();
   view->setScene(scence);
   //view->setBackgroundBrush(*brush);
   //view->setCacheMode(QGraphicsView::CacheBackground);
   view->showFullScreen();

it gives nothing.

Was it helpful?

Solution

As the Qt doc says :

QBrush::QBrush ()
Constructs a default black brush with the style Qt::NoBrush (i.e. this brush will not fill shapes).

In your second example, you have to set the style of the QBrush object by setStyle(), for example with Qt::SolidPattern.

   QGraphicsScene * scence = new QGraphicsScene();
   QBrush *brush = new QBrush();
   brush->setStyle(Qt::SolidPattern); // Fix your problem !
   brush->setColor(QColor(60,20,20));
   scence->setBackgroundBrush(*brush);

   QGraphicsView *view = new QGraphicsView();
   view->setScene(scence);
   //view->setBackgroundBrush(*brush);
   //view->setCacheMode(QGraphicsView::CacheBackground);
   view->showFullScreen();

Hope it helps !

OTHER TIPS

An alternate approach that achieves the same result is to put the color in the brush constructor, and that applies a default style of solid:

 QBrush *brush = new QBrush (QColor (60, 20, 20));

The constructors that take a color have an optional parameter for the style, which defaults to Qt::SolidPattern. Both approaches produce the same result, but this one uses two fewer lines of code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top