Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved QGradient showing invalid color?

    General and Desktop
    2
    6
    98
    Loading More Posts
    • 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.
    • CJha
      CJha last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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);
        
        CJha 1 Reply Last reply Reply Quote 3
        • CJha
          CJha @mrjj last edited by

          @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 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @CJha last edited by

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

            CJha 2 Replies Last reply Reply Quote 0
            • CJha
              CJha @mrjj last edited by

              @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 Reply Quote 0
              • CJha
                CJha @mrjj last edited by

                @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 Reply Quote 0
                • First post
                  Last post