How to create a QLinearGradient?
Unsolved
General and Desktop
-
What's wrong in the way i'm creating the
QLinearGradient
?
AtqDebug() << 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(); }
-
What's wrong in the way i'm creating the
QLinearGradient
?
AtqDebug() << 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(); }
@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());
-
You can see this video about QLinearGradient