Results 1 to 3 of 3

Thread: Using QBrush(const QGradient & gradient) constructor causes a problem

  1. #1
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QBrush(const QGradient & gradient) constructor causes a problem

    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.

    Qt Code:
    1. ballItem::ballItem()
    2. {
    3. QGradient gradient;
    4. gradient.setSpread(QGradient::PadSpread);
    5.  
    6. // brush = new QBrush(gradient);
    7. QBrush brush2(gradient);
    8. }
    To copy to clipboard, switch view to plain text mode 

    This is the header:

    Qt Code:
    1. #include <QGraphicsItem>
    2.  
    3. class ballItem : public QGraphicsItem
    4. {
    5. public:
    6. ballItem();
    7. protected:
    8. QRectF boundingRect() const;
    9. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    10. private:
    11. QBrush* brush;
    12. };
    To copy to clipboard, switch view to plain text mode 

    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-...ad00025-0.html
    Last edited by ajo; 12th April 2013 at 11:41.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QBrush(const QGradient & gradient) constructor causes a problem

    So is your problem solved or not?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QBrush(const QGradient & gradient) constructor causes a problem

    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:

    Qt Code:
    1. QRadialGradient gradient(-3, -3, 10);
    To copy to clipboard, switch view to plain text mode 

    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:aint(QPainter *p) - a QGradient variable is declared and used:

    Qt Code:
    1. void GradientRenderer:: paint(QPainter *p)
    2. {
    3. QPolygonF pts = m_hoverPoints->points();
    4.  
    5.  
    6. // and then farther down in the function:
    7.  
    8. p->setBrush(g);
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. if (m_gradientType == Qt::LinearGradientPattern) {
    2. g = QLinearGradient(pts.at(0), pts.at(1));
    3.  
    4. } else if (m_gradientType == Qt::RadialGradientPattern) {
    5. g = QRadialGradient(pts.at(0), qMin(width(), height()) / 3.0, pts.at(1));
    6.  
    7. } else {
    8. QLineF l(pts.at(0), pts.at(1));
    9. qreal angle = l.angle(QLineF(0, 0, 1, 0));
    10. if (l.dy() > 0)
    11. angle = 360 - angle;
    12. g = QConicalGradient(pts.at(0), angle);
    13. }
    To copy to clipboard, switch view to plain text mode 

    g is changed to a QLinearGradient or a QRadialGradient or a QConicalGradient.
    Last edited by ajo; 17th April 2013 at 08:18. Reason: updated contents

Similar Threads

  1. Linear gradient problem
    By K4ELO in forum Newbie
    Replies: 1
    Last Post: 10th April 2013, 08:57
  2. QBrush pattern problem
    By ilovethisgame in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2012, 22:07
  3. Gradient fill problem
    By benzolo in forum Qt Programming
    Replies: 0
    Last Post: 6th September 2010, 08:29
  4. Problem with the const
    By aekilic in forum Qt Programming
    Replies: 13
    Last Post: 9th April 2010, 08:10
  5. Qpen, QBrush or QPainterpath problem
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 14th July 2009, 11:53

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.