Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGradient showing invalid color?

QGradient showing invalid color?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 593 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by
    #1

    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 and QGradient page, what am I missing here?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      alt text

      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);
      
      CJhaC 1 Reply Last reply
      4
      • mrjjM mrjj

        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.

        alt text

        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);
        
        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on last edited by
        #3

        @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)
        
        mrjjM 1 Reply Last reply
        0
        • CJhaC CJha

          @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)
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @CJha
          Hi
          Mine also says
          "QBrush(QColor(Invalid),LinearGradientPattern)"
          even it draws as expected. o.O

          Hmm. code looks ok.
          but could you try
          QRect nRect = newComboOption.rect;
          --->
          QRect nRect = widget->rect()

          just for test ?

          CJhaC 2 Replies Last reply
          0
          • mrjjM mrjj

            @CJha
            Hi
            Mine also says
            "QBrush(QColor(Invalid),LinearGradientPattern)"
            even it draws as expected. o.O

            Hmm. code looks ok.
            but could you try
            QRect nRect = newComboOption.rect;
            --->
            QRect nRect = widget->rect()

            just for test ?

            CJhaC Offline
            CJhaC Offline
            CJha
            wrote on last edited by
            #5

            @mrjj Hi, Thanks for sharing the output. I tried QRect nRect = widget->rect() the size of rect is the same and the color is still black.

            1 Reply Last reply
            0
            • mrjjM mrjj

              @CJha
              Hi
              Mine also says
              "QBrush(QColor(Invalid),LinearGradientPattern)"
              even it draws as expected. o.O

              Hmm. code looks ok.
              but could you try
              QRect nRect = newComboOption.rect;
              --->
              QRect nRect = widget->rect()

              just for test ?

              CJhaC Offline
              CJhaC Offline
              CJha
              wrote on last edited by
              #6

              @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?

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved