1

I am building a c++ app with Qt5. In the examples of QPen I see that I can set the brush as follows:

QPen* myPen = new QPen();
myPen->setBrush(Qt::cyan);

While this compiles fine, it doesn't match the documentation. QPen's setBrush method is supposed to receive a QBrush. Why is it acceptable to pass in a Qt::GlobalColor instead? From the QPen.h file it does not appear that setBrush is overloaded.

Dude
  • 13
  • 1
  • 2

1 Answers1

0

QBrush has a conversion constructor for Qt::GlobalColor, i.e. compiler can use it to convert a Qt::GlobarColor to QBrush.

Here is its definition in qbrush.h:

QBrush(Qt::GlobalColor color, Qt::BrushStyle style = Qt::SolidPattern)

This constructor is, what's been called in your myPen->setBrush(Qt::cyan); function call to create a QBrush for you and pass to setBrush().

Check this to learn more about the conversion constructor concept.

Community
  • 1
  • 1
Mehrdad
  • 1,186
  • 7
  • 15