3

以下代码有什么区别,

  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();

给出黑色背景

  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();

它什么也没给。

4

2 回答 2

8

正如 Qt 文档所说:

QBrush::QBrush ()
构造一个默认的黑色画笔,样式为 Qt::NoBrush(即这个画笔不会填充形状)。

在第二个示例中,您必须通过setStyle () 设置 QBrush 对象的样式,例如使用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();

希望能帮助到你 !

于 2010-03-08T12:43:51.870 回答
0

实现相同结果的另一种方法是将颜色放在画笔构造函数中,并应用默认样式的纯色:

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

采用颜色的构造函数有一个样式的可选参数,默认为 Qt::SolidPattern。两种方法产生相同的结果,但这种方法使用的代码少了两行。

于 2017-10-31T23:14:43.473 回答