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. How to create a QLinearGradient?

How to create a QLinearGradient?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 493 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.
  • K Offline
    K Offline
    Kattia
    wrote on last edited by
    #1

    What's wrong in the way i'm creating the QLinearGradient?
    At qDebug() << gradient; it output: QBrush(QColor(Invalid),LinearGradientPattern)

    int main(int argc, char* argv[]) {
    	QApplication app(argc, argv);
    
    	QLinearGradient gradient;
    	QString gradientString = "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(15, 21, 255, 155), stop:1 rgba(126, 22, 224, 155));";
    	QWidget* widget = new QWidget;
    	widget->setStyleSheet(gradientString);
    	widget->setWindowTitle("StyleSheet");
    	widget->show();
    	
            // Parse the string to create a QLinearGradient
    
    	// Remove the "qlineargradient(" and ")" parts
    	gradientString.replace("background-color: qlineargradient(", "");
    	gradientString.chop(2);
    
    	// Extract the start and end points
    	QRegularExpression rx("x1:([\\d.]+), y1:([\\d.]+), x2:([\\d.]+), y2:([\\d.]+)");
    	QRegularExpressionMatch match = rx.match(gradientString);
    	if (match.hasMatch())
    	{
    		float x1 = match.captured(1).toFloat();
    		float y1 = match.captured(2).toFloat();
    		float x2 = match.captured(3).toFloat();
    		float y2 = match.captured(4).toFloat();
    		qDebug() << "x1:" << x1 << "y1:" << y1 << "\nx2:" << x2 << "y2:" << y2 << "\n";
    
    		gradient = QLinearGradient(QPointF(x1, y1), QPointF(x2, y2));
    
    		// Extract the color stops
    		QRegularExpression rxStop("stop:([\\d.]+) rgba\\((\\d+), (\\d+), (\\d+), (\\d+)\\)");
    		int pos = 0;
    		while ((match = rxStop.match(gradientString, pos)).hasMatch())
    		{
    			float stop = match.captured(1).toFloat();
    			int r = match.captured(2).toInt();
    			int g = match.captured(3).toInt();
    			int b = match.captured(4).toInt();
    			int a = match.captured(5).toInt();
    
    			qDebug().nospace() << "stop: " << stop << " rgba(" << r << "," << g << "," << b << "," << a << ")\n";
    
    			// Check if the color values are valid
    			if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255 && a >= 0 && a <= 255)
    				gradient.setColorAt(stop, QColor(r, g, b, a));
    			else
    				qDebug() << "Invalid color values";
    
    			pos = match.capturedEnd();
    		}
    		qDebug() << gradient;
    	}
    
    	widget = new QWidget;
    	QPalette palette;
    	palette.setBrush(widget->backgroundRole(), QBrush(gradient));
    	widget->setPalette(palette);
    	widget->setWindowTitle("Palette");
    	widget->show();
    
    	return app.exec();
    }
    
    KenAppleby 0K 1 Reply Last reply
    0
    • K Kattia

      What's wrong in the way i'm creating the QLinearGradient?
      At qDebug() << gradient; it output: QBrush(QColor(Invalid),LinearGradientPattern)

      int main(int argc, char* argv[]) {
      	QApplication app(argc, argv);
      
      	QLinearGradient gradient;
      	QString gradientString = "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(15, 21, 255, 155), stop:1 rgba(126, 22, 224, 155));";
      	QWidget* widget = new QWidget;
      	widget->setStyleSheet(gradientString);
      	widget->setWindowTitle("StyleSheet");
      	widget->show();
      	
              // Parse the string to create a QLinearGradient
      
      	// Remove the "qlineargradient(" and ")" parts
      	gradientString.replace("background-color: qlineargradient(", "");
      	gradientString.chop(2);
      
      	// Extract the start and end points
      	QRegularExpression rx("x1:([\\d.]+), y1:([\\d.]+), x2:([\\d.]+), y2:([\\d.]+)");
      	QRegularExpressionMatch match = rx.match(gradientString);
      	if (match.hasMatch())
      	{
      		float x1 = match.captured(1).toFloat();
      		float y1 = match.captured(2).toFloat();
      		float x2 = match.captured(3).toFloat();
      		float y2 = match.captured(4).toFloat();
      		qDebug() << "x1:" << x1 << "y1:" << y1 << "\nx2:" << x2 << "y2:" << y2 << "\n";
      
      		gradient = QLinearGradient(QPointF(x1, y1), QPointF(x2, y2));
      
      		// Extract the color stops
      		QRegularExpression rxStop("stop:([\\d.]+) rgba\\((\\d+), (\\d+), (\\d+), (\\d+)\\)");
      		int pos = 0;
      		while ((match = rxStop.match(gradientString, pos)).hasMatch())
      		{
      			float stop = match.captured(1).toFloat();
      			int r = match.captured(2).toInt();
      			int g = match.captured(3).toInt();
      			int b = match.captured(4).toInt();
      			int a = match.captured(5).toInt();
      
      			qDebug().nospace() << "stop: " << stop << " rgba(" << r << "," << g << "," << b << "," << a << ")\n";
      
      			// Check if the color values are valid
      			if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255 && a >= 0 && a <= 255)
      				gradient.setColorAt(stop, QColor(r, g, b, a));
      			else
      				qDebug() << "Invalid color values";
      
      			pos = match.capturedEnd();
      		}
      		qDebug() << gradient;
      	}
      
      	widget = new QWidget;
      	QPalette palette;
      	palette.setBrush(widget->backgroundRole(), QBrush(gradient));
      	widget->setPalette(palette);
      	widget->setWindowTitle("Palette");
      	widget->show();
      
      	return app.exec();
      }
      
      KenAppleby 0K Offline
      KenAppleby 0K Offline
      KenAppleby 0
      wrote on last edited by
      #2

      @Kattia said in How to create a QLinearGradient?:

      At qDebug() << gradient; it output: QBrush(QColor(Invalid),LinearGradientPattern)

      I think this just means that the brush has been set with a gradient and no a fill colour. You can ignore this.

      The constructor for QLinearGradient takes pixel dimensions. So you are creating one that is just 1 pixel wide and 0 pixels high. Try something like:

      gradient = QLinearGradient(QPointF(0.0, 0.0), widget->rect().bottomRight());
      
      1 Reply Last reply
      0
      • Ketan__Patel__0011K Offline
        Ketan__Patel__0011K Offline
        Ketan__Patel__0011
        wrote on last edited by
        #3

        You can see this video about QLinearGradient

        https://youtu.be/hK5dP2Onw9E?si=W19327qJOEzO8Aw5

        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