Results 1 to 3 of 3

Thread: Create my own QGradient subclass

  1. #1
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Create my own QGradient subclass

    I want to draw a polyline using QPainter. The line should change color dynamically according to point coordinates.

    In order to achieve this, I'd like to subclass QGradientin order to obtain a QFunctionGradient where I can set a std::function<QColor(QPointF)> that gives me the color of a point given its coordinates.

    So, at first, how It's possibile to draw a polyline with QPainter and color it according to a QGradient?

    And it's possible to subclass QPainter and use this subclass inside QBrush?

  2. #2
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Create my own QGradient subclass

    The API for the actual formulae of gradients (radial, linear etc.) is not exposed publicly.
    If it were, we would probably have something like a QAbstractGradient class with a set of protected virtual functions for us to reimplement with our own functionality.

    In your situation, what I would do is first use a QPainter to render a white polyline (polished, with anti-aliasing etc.) onto a QImage, then obtain the QImage.bits() pointer and go through every non-transparent pixel in the image, changing their colour based on a custom function, and preserving the alpha values.

    The resulting QImage can then be QPainter-ed anywhere else.

    Another technique: you can also generate a Qimage "by hand" (by setting each pixel), and then QPainting that coloured image on top of your polyline with the SourceIn composition mode.
    So you have a map of your gradient and you're using the antialiased polyline as a soft mask.

  3. #3
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create my own QGradient subclass

    Ok so it's how I was thinking, I must do this by myself... I'll try, thanks for your advices.

Similar Threads

  1. Replies: 2
    Last Post: 15th April 2013, 06:33
  2. QGradient on triangle
    By oogolov in forum Qt Programming
    Replies: 3
    Last Post: 20th June 2012, 19:34
  3. drawLine with QGradient is slow
    By D.Dupuis in forum Qt Programming
    Replies: 0
    Last Post: 7th December 2011, 12:35
  4. HistogramItem and Qgradient
    By OverTheOCean in forum Qwt
    Replies: 4
    Last Post: 27th August 2010, 02:59
  5. Problems with painting a Qgradient
    By sacrif in forum Newbie
    Replies: 4
    Last Post: 20th January 2010, 16:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.