QGradient showing invalid color?
-
Hi, I am trying to create a new
QLinearGradient
but is appearing as black. When I try to debug the output it shows invalid color:QLinearGradient gradient(QPointF(100, 100), QPointF(200, 200)); gradient.setColorAt(0, Qt::green); gradient.setColorAt(1, Qt::red); QBrush brush(gradient); qDebug() << gradient << brush;
output:
QBrush(QColor(Invalid),LinearGradientPattern) QBrush(QColor(Invalid),LinearGradientPattern)
This is the first time I am creating a gradient and I am following the examples shown on
QBrush
andQGradient
page, what am I missing here? -
Hi
100,100 - 200, 200
must match to some degree what you draw it on.so if you fill rect from 200,200 or simular it will show black or another color as the gradient is based on the coordinates.
QLinearGradient gradient(QPointF(0, 0), QPointF(100, 100)); gradient.setColorAt(0, Qt::green); gradient.setColorAt(1, Qt::red); QBrush brush(gradient); painter.setBrush(brush); painter.drawRect(0,0,100,100);
-
Hi
100,100 - 200, 200
must match to some degree what you draw it on.so if you fill rect from 200,200 or simular it will show black or another color as the gradient is based on the coordinates.
QLinearGradient gradient(QPointF(0, 0), QPointF(100, 100)); gradient.setColorAt(0, Qt::green); gradient.setColorAt(1, Qt::red); QBrush brush(gradient); painter.setBrush(brush); painter.drawRect(0,0,100,100);
@mrjj Can you show me your debug output for
qDebug() << gradient << brush;
before you set the painter's brush?For a clearer explanation, I am using it inside my own style to draw on a
QPushButton
and setting the brush of the palette, I am trying to have a gradient for a brush:void GuiStyle::drawControl(QStyle::ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const { if(element == QStyle::CE_PushButton) { const QStyleOptionButton* comboOption = qstyleoption_cast<const QStyleOptionButton*>(option); QStyleOptionButton newComboOption = *comboOption; QRect nRect = newComboOption.rect; QLinearGradient nGrad(0, 0, nRect.width(), nRect.height()); nGrad.setColorAt(0, Qt::green); nGrad.setColorAt(1, Qt::red); QBrush nBrush(nGrad); qDebug() << nRect << nGrad << nBrush; newComboOption.palette.setBrush(QPalette::Button, nBrush); QProxyStyle::drawControl(element, &newComboOption, painter, widget); } else { QProxyStyle::drawControl(element, option, painter, widget); } }
My button appears as black and the output is still invalid color while the rect is of correct size:
QRect(0,0 80x24) QBrush(QColor(Invalid),LinearGradientPattern) QBrush(QColor(Invalid),LinearGradientPattern)
-
@mrjj Can you show me your debug output for
qDebug() << gradient << brush;
before you set the painter's brush?For a clearer explanation, I am using it inside my own style to draw on a
QPushButton
and setting the brush of the palette, I am trying to have a gradient for a brush:void GuiStyle::drawControl(QStyle::ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const { if(element == QStyle::CE_PushButton) { const QStyleOptionButton* comboOption = qstyleoption_cast<const QStyleOptionButton*>(option); QStyleOptionButton newComboOption = *comboOption; QRect nRect = newComboOption.rect; QLinearGradient nGrad(0, 0, nRect.width(), nRect.height()); nGrad.setColorAt(0, Qt::green); nGrad.setColorAt(1, Qt::red); QBrush nBrush(nGrad); qDebug() << nRect << nGrad << nBrush; newComboOption.palette.setBrush(QPalette::Button, nBrush); QProxyStyle::drawControl(element, &newComboOption, painter, widget); } else { QProxyStyle::drawControl(element, option, painter, widget); } }
My button appears as black and the output is still invalid color while the rect is of correct size:
QRect(0,0 80x24) QBrush(QColor(Invalid),LinearGradientPattern) QBrush(QColor(Invalid),LinearGradientPattern)
-
@CJha
Hi
Mine also says
"QBrush(QColor(Invalid),LinearGradientPattern)"
even it draws as expected. o.OHmm. code looks ok.
but could you try
QRect nRect = newComboOption.rect;
--->
QRect nRect = widget->rect()just for test ?
-
@CJha
Hi
Mine also says
"QBrush(QColor(Invalid),LinearGradientPattern)"
even it draws as expected. o.OHmm. code looks ok.
but could you try
QRect nRect = newComboOption.rect;
--->
QRect nRect = widget->rect()just for test ?
@mrjj Hi, even if I do
void GuiStyle::drawControl(QStyle::ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const { if(element == QStyle::CE_PushButton) { const QStyleOptionButton* comboOption = qstyleoption_cast<const QStyleOptionButton*>(option); QStyleOptionButton newComboOption = *comboOption; QBrush nBrush(Qt::green, Qt::DiagCrossPattern); qDebug() << nBrush; newComboOption.palette.setBrush(QPalette::Button, nBrush); QProxyStyle::drawControl(element, &newComboOption, painter, widget); } else { QProxyStyle::drawControl(element, option, painter, widget); } }
The color of the button appears as
Qt::green
without any pattern. Can it point to something I am missing in my Qt installation?