Hi!
I'm fairly new at Qt and I'm getting really frustrated trying to make this work.
My problem is as follows:
I've created a derived class from QImage which I called "borderImage" which also inherits QObject. This class is itself a QImage but also has two QImages members "m_original" and "m_out". These members are created whithin the class constructor (passing a filename as an argument to the constructor). There is a function member of borderImage wich is "setAlpha" which changes the alpha of each pixel of "m_out" by picking first the color of the corresponding pixel at "m_original", then modifying its alpha and finally asigning this color to m_out's pixel. Also, "alpha" is defined as a property with the Q_PROPERTY macro, with alpha() as getter which simply returns the current alpha value, and setAlpha(int alpha) as the write function. Finally there is a class derived from QWidget which has a borderImage member that is drawn in every paintEvent.
The problem arises when trying to animate the "alpha" property. The problem itself is that the application crashes randomly when executing the animation.start() function. By trial and error I've determined that if the start() function is called "away" from the call to the constructor of the widget with the borderImage to be animated the application runs without any problem. Thus, I thought that the problem was in the constructor, and as a solution I tried to change the widget member borderImage to a pointer to a borderImage and initializing it in the constructor. I did the same with the QImage members of borderImage. None of these modifications worked. The applications continues to crash randomly when trying to start the animation. The only way to prevent this is, for instance, starting the animation after pressing a button, while the objects are created somewhere else in the code.

For clarification here is some parts of the code:
borderimage.h
Qt Code:
  1. #ifndef BORDERIMAGE_H
  2. #define BORDERIMAGE_H
  3.  
  4. #include <QtGui>
  5.  
  6. class borderImage : public QObject, public QImage
  7. {
  8. Q_OBJECT
  9. Q_PROPERTY(int alpha READ alpha WRITE setAlpha)
  10. public:
  11. borderImage(int width, int height, QWidget *parent = 0, const QString & filename = NULL);
  12. borderImage(QImage image, QObject *parent=0);
  13. QImage * getImage()
  14. { return m_out ;}
  15. void setDistances(int top, int right, int bottom, int left);
  16. void draw(int w, int h, float b_w);
  17. int alpha() const
  18. {return m_alpha;}
  19. void setAlpha(int alpha);
  20.  
  21. private:
  22. int m_top, m_right, m_bottom, m_left, m_w, m_h;
  23. int m_alpha;
  24. QImage *m_out;
  25. QImage *m_original;
  26. };
  27.  
  28. #endif // BORDERIMAGE_H
To copy to clipboard, switch view to plain text mode 

mainwidget.h

Qt Code:
  1. #ifndef MAINWIDGET_H
  2. #define MAINWIDGET_H
  3.  
  4. #include <QWidget>
  5. #include "borderimage.h"
  6.  
  7. class mainWidget : public QWidget
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. mainWidget(QWidget *parent = 0);
  13. ~mainWidget();
  14. borderImage * getBorde() { return borde; }
  15.  
  16. protected:
  17. void paintEvent(QPaintEvent *event);
  18.  
  19.  
  20. public slots:
  21. void buttonClicked();
  22. void timerEvent(QTimerEvent *event);
  23.  
  24. private:
  25. void loadPlugins(QGridLayout *layout);
  26. borderImage *borde;
  27. QImage background;
  28. QObjectList plugins;
  29. };
  30.  
  31. #endif // MAINWIDGET_H
To copy to clipboard, switch view to plain text mode 

parts of borderimage.cpp
Qt Code:
  1. borderImage::borderImage(int width, int height, QWidget *parent, const QString & filename ):
  2. QObject(parent), QImage(width, height, Format_ARGB32)
  3. {
  4. if (filename!=NULL)
  5. {
  6. m_original = new QImage(filename);
  7. m_out = new QImage(filename);
  8. }
  9. this->fill(Qt::transparent);
  10.  
  11. //this->setDotsPerMeterX(10);
  12. //this->setDotsPerMeterY(10);
  13. }
  14.  
  15. ...
  16.  
  17. void borderImage::setAlpha(int alpha)
  18. {
  19. int i;
  20. int j;
  21. QColor color;
  22.  
  23. for (i=0;i< (m_original->size().height());++i)
  24. {
  25. const QRgb * rgb = (QRgb *) m_original->scanLine(i);
  26. QRgb * nuevo = (QRgb *) m_out->scanLine(i);
  27. for (j=1; j<m_out->bytesPerLine();++j)
  28. {
  29. color.setRgba(*rgb);
  30. if (color.alpha()>alpha)
  31. {
  32. color.setAlpha(alpha);
  33. //color.toRgb();
  34. //color.setRedF(color.redF()*alpha/255);
  35. //color.setGreenF(color.greenF()*alpha/255);
  36. //color.setBlueF(color.blueF()*alpha/255);
  37. *nuevo = color.rgba();
  38. }
  39. ++rgb;
  40. ++nuevo;
  41. }
  42. }
  43.  
  44. m_alpha=alpha;
  45. //cout << alpha << "\n";
  46.  
  47. QWidget *papa = qobject_cast<QWidget *>(this->parent());
  48. papa->update();
  49. }
To copy to clipboard, switch view to plain text mode 

mainwidget.cpp
Qt Code:
  1. mainWidget::mainWidget(QWidget *parent)
  2. : QWidget(parent)
  3. {
  4.  
  5. ...
  6.  
  7.  
  8. borde = new borderImage(800,800,this,":/rsr/media/background.png");
  9. borde->setDistances(50,50,50,50);
  10. borde->setAlpha(200);
  11.  
  12. }
To copy to clipboard, switch view to plain text mode 

and the animation
Qt Code:
  1. QPropertyAnimation *animation = new QPropertyAnimation(pw->getBorde(),"alpha");
  2. animation->setDuration(10);
  3. animation->setStartValue(0);
  4. animation->setEndValue(200);
  5. animation->start
To copy to clipboard, switch view to plain text mode 
Where pw is a mainWidget. If that is called near the declaration of the mainWidget, it crashes randomly when run.

I can't figure out what the problem is...

Thanks in advance for your help!

PS: I've also noticed that the same problem occurs if I call setAlpha directly instead of animating the alpha property, therefore the problem must be in the setAlpha function (which is also called by the animation).