Say I have a liner gradient as shown:
QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, 100));
linearGrad.setColorAt(1, Qt::red);
linearGrad.setColorAt(0.5, Qt::yellow);
linearGrad.setColorAt(0, Qt::green);
How to get the color of the point QPointF(0, 28.5) in this gradient?
Indeed I want to have this kind of color distribution to be able to choose intermediate colors. I don’t care if it is done by using QLinearGradient or something else.
There is only way to make it:
There is a static member in QPixmap class
QPixmap QPixmap::grabWindow( WId window, int x = 0, int y = 0, int width = -1, int height = -1 )1) draw your gradient on your widget;
2) grab you widget’s surface into pixmap using that function;
WIdcan be received fromQWidget::effectiveWinId ();3) convert token pixmap into
QImage(there is an constructor available);4)
int QImage::pixelIndex( int x, int y )returns the pixel index at (x, y) inQImage‘s color table. In your case you must calculate percentage value from widget’s height (pWidget->height() / 100 * 28.5).5)
QRgb QImage::color( int i )returns the color in the color table at index i.So returned Color is the color you were seeking.