Problem with QLinearGradient
-
Hi,
I followed the Qt example "painterpaths", but in my app the gradients won't work.
I run out of idea about what I could have done wrong ...
-
Hi,
Please explain what you did, the issue you have, post a picture of the result. Add informations such as Qt version, OS, etc.
Just saying you have an issue and expecting people to download some random file and read through a maybe large project is not the best way to request for help.
-
The idea is to create an app, where the user can interactively assemble an image from various subpictures/elements.
The sample project from first post contains only one subelement:This is definition of QLinearGradient from painterpath:
void RenderArea::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.scale(width() / 100.0, height() / 100.0); painter.translate(50.0, 50.0); painter.rotate(-rotationAngle); painter.translate(-50.0, -50.0); painter.setPen(QPen(penColor, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); QLinearGradient gradient(0, 0, 0, 100); gradient.setColorAt(0.0, fillColor1); gradient.setColorAt(1.0, fillColor2); qDebug() << "gradient:" << fillColor1 << " -> " << fillColor2; painter.setBrush(gradient); painter.drawPath(path); }
and this is my variant:
void MainWindow::assembleParts() { QImage scratch = QImage(width, height, QImage::Format_ARGB32); QPainter p(&scratch); p.setRenderHint(QPainter::Antialiasing); p.setCompositionMode(QPainter::CompositionMode_Source); p.fillRect(scratch.rect(), Qt::transparent); p.fillRect(scratch.rect(), Qt::transparent); p.setCompositionMode(QPainter::CompositionMode_SourceOver); for (int i=0; i < 2; ++i) { if (!i || cfg[i].x || cfg[i].y) { p.translate(cfg[i].x, cfg[i].y); p.rotate(-cfg[i].rotation); QLinearGradient gradient(0, 0, 0, 100); gradient.setColorAt(0, cfg[i].fgCol0); // white gradient.setColorAt(1, cfg[i].fgCol1); // red qDebug() << "gradient:" << cfg[i].fgCol0 << " -> " << cfg[i].fgCol1; p.setPen(QPen(cfg[i].fgCol1, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); p.setBrush(gradient); p.drawPath(cfg[i].pp); p.rotate(cfg[i].rotation); p.translate(-cfg[i].x, -cfg[i].y); } } ui->label->setPixmap(QPixmap::fromImage(scratch)); }
-
This behaviour might be related to the "known issue" where Qt is not able to generate linear gradients if they are vertically aligned. The "known issue" talks about predefined gradients, but it seems as if the issue appears without predefined gradients too.
Changing the gradient to horizontal works fine.
I tested the behaviour with Qt5.12, Qt6.3 and Qt 6.4