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. Problem with QLinearGradient
Forum Updated to NodeBB v4.3 + New Features

Problem with QLinearGradient

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 551 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    django013
    wrote on last edited by
    #1

    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 ...

    simplified test app

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

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        django013
        wrote on last edited by
        #3

        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:

        No-Gradient.png

        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));
          }
        
        1 Reply Last reply
        0
        • D Offline
          D Offline
          django013
          wrote on last edited by
          #4

          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

          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