Hello,

I find myself creating a lot of QBrush and QPen objects when I draw things on the screen or create items for a graphics scene. I decided to create a class for easy access to QBrushes like so:

Qt Code:
  1. class ColorUtil
  2. {
  3. public:
  4. ColorUtil();
  5. ~ColorUtil(){}
  6.  
  7. QBrush brushYellow;
  8. QBrush brushRed;
  9. QBrush brushBlue;
  10. };
  11.  
  12. ColorUtil::ColorUtil()
  13. {
  14. brushYellow.setColor(QColor::fromRgb(255,255,0,125));
  15. brushRed.setColor(QColor::fromRgb(255,125,125,255));
  16. brushBlue.setColor(QColor::fromRgb(0,0,255,125));
  17. }
To copy to clipboard, switch view to plain text mode 

Now when I try to use the brushes from the ColorUtil class above for example in the constructor of a graphics scene:

Qt Code:
  1. class PocScene : public QGraphicsScene
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6.  
  7.  
  8. public:
  9. PocScene(QObject *parent = 0);
  10. ~PocScene(){}
  11. };
  12.  
  13. PocScene::PocScene(QObject *parent) : QGraphicsScene(parent)
  14. {
  15. setSceneRect(-1000, -1000, 2000, 2000);
  16.  
  17. double wheelRadius = 0.05;
  18. double carHeight = 0.15;
  19. double carWidth = 0.3;
  20.  
  21. QPen pen;
  22. pen.setCosmetic(true);
  23. pen.setWidth(2);
  24.  
  25. QBrush yellowBrush(QColor::fromRgb(255,255,0,125));
  26. QBrush redBrush(QColor::fromRgb(255,125,125,255));
  27. QBrush blueBrush(QColor::fromRgb(0,0,255,125));
  28.  
  29. ColorUtil colorUtil;
  30.  
  31. car = addRect(-0.5*carWidth, -0.5*carHeight, carWidth, carHeight, pen, yellowBrush);
  32. lWheel = addEllipse(-wheelRadius, -wheelRadius, 2.0*wheelRadius, 2.0*wheelRadius, pen, colorUtil.brushRed); // ColorUtil used here!
  33. rWheel = addEllipse(-wheelRadius, -wheelRadius, 2.0*wheelRadius, 2.0*wheelRadius, pen, blueBrush);
  34. }
To copy to clipboard, switch view to plain text mode 

...it turns out that it does not work and the left wheel of the car is not colored while the right one is. I don't understand why this does not work. Printing of color values inside the ColorUtil class shows me that the QBrushes have been instantiated correctly, so what is the difference between the QBrushes inside the ColorUtil class and the locally instantiated QBrush objects that do work?