Naruto
Naruto

Reputation: 9634

Qt Qbrush issue

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.

Upvotes: 3

Views: 5410

Answers (2)

goug
goug

Reputation: 2444

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.

Upvotes: 0

Matthieu
Matthieu

Reputation: 2761

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 !

Upvotes: 8

Related Questions