PDA

View Full Version : Using QBrush(const QGradient & gradient) constructor causes a problem



ajo
12th April 2013, 11:41
I'm trying to draw an image like the radial gradient one in the Qt gradients example, working with:
Qt 5.0.1 (64 bit) Qt Creator 2.6.2
on a Mac with OS X 10.8.3 (Mountain Lion)

It gives an memory access error and points to the line where the brush is constructed, whether brush is a pointer or not.


ballItem::ballItem()
{
QGradient gradient;
gradient.setSpread(QGradient::PadSpread);

// brush = new QBrush(gradient);
QBrush brush2(gradient);
}

This is the header:


#include <QGraphicsItem>

class ballItem : public QGraphicsItem
{
public:
ballItem();
protected:
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
QBrush* brush;
};

I was trying to use this QBrush constructor:
QBrush::QBrush(const QGradient & gradient)

I would appreciate it if somebody can show me my error.

Added after 40 minutes:

It seems that QConicalGradient, QLinearGradient or QRadialGradient needs to be used, not QGradient, because the QGradient type isn't initialized.
http://lists.trolltech.com/qt-jambi-interest/2007-05/thread00025-0.html

wysota
16th April 2013, 19:31
So is your problem solved or not?

ajo
17th April 2013, 08:10
The "Elastic Nodes Example" provided me with a simpler way to draw a ball with a light spot and a smooth transition to the darker parts.

In that example - Node:: paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) - QRadialGradient is used:


QRadialGradient gradient(-3, -3, 10);

My problem is solved, and I think to use QRadialGradient would also work to draw an image like the one in the "Gradients" example. Interestingly though, I see in that example - in void GradientRenderer::paint(QPainter *p) - a QGradient variable is declared and used:


void GradientRenderer:: paint(QPainter *p)
{
QPolygonF pts = m_hoverPoints->points();

QGradient g;

// and then farther down in the function:

p->setBrush(g);

When I tried to do the same, it crashed, because I left out this part:


if (m_gradientType == Qt::LinearGradientPattern) {
g = QLinearGradient(pts.at(0), pts.at(1));

} else if (m_gradientType == Qt::RadialGradientPattern) {
g = QRadialGradient(pts.at(0), qMin(width(), height()) / 3.0, pts.at(1));

} else {
QLineF l(pts.at(0), pts.at(1));
qreal angle = l.angle(QLineF(0, 0, 1, 0));
if (l.dy() > 0)
angle = 360 - angle;
g = QConicalGradient(pts.at(0), angle);
}


g is changed to a QLinearGradient or a QRadialGradient or a QConicalGradient.