Results 1 to 5 of 5

Thread: Random crashes when using QPropertyAnimation on custom property

  1. #1
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Random crashes when using QPropertyAnimation on custom property

    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).

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Random crashes when using QPropertyAnimation on custom property

    Why does your class inherit QImage?
    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
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Random crashes when using QPropertyAnimation on custom property

    Even though there is a "getImage" function, it is not used. What is done is: in the function draw, a painter is created to paint on itself, and it paints several parts of m_out on differents parts of itself (being itself a QImage). Then in the widget class I directly pass the borderImage to drawImage as a QImage. Do you think it might be a problem with inheriting both QObject and QImage? If it is so, I can simply replace the getImage function to create a new QImage, paint on it from m_out, and finally return it, or create another private member which actually is the final image, and do the painting on that image instead of itself.

    Thanks for the reply!
    Last edited by NaOH; 21st October 2012 at 15:36.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Random crashes when using QPropertyAnimation on custom property

    Quote Originally Posted by NaOH View Post
    Do you think it might be a problem with inheriting both QObject and QImage?
    Yes. QImage is usually copied and QObject can't be copied. If you pass your object as QImage somewhere, this might break QObject internals.
    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.


  5. The following user says thank you to wysota for this useful post:

    NaOH (21st October 2012)

  6. #5
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Random crashes when using QPropertyAnimation on custom property

    Thanks! No longer inheriting QImage did the trick, thanks for the quick reply!

Similar Threads

  1. Custom Property On Custom Widget
    By Ashutosh2k1 in forum Qt Programming
    Replies: 29
    Last Post: 28th October 2014, 12:16
  2. How to debug random crashes?
    By Gunnar in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2011, 21:25
  3. [SOLVED] QPropertyAnimation "text" property in QLabel
    By AlekseyK in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2010, 18:46
  4. Custom property in designer
    By avgust in forum Newbie
    Replies: 1
    Last Post: 9th September 2010, 10:14
  5. Replies: 1
    Last Post: 7th April 2010, 16:26

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.