Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error on creating QPropertyAnimation object
Forum Updated to NodeBB v4.3 + New Features

Error on creating QPropertyAnimation object

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • behruz montazeriB Offline
    behruz montazeriB Offline
    behruz montazeri
    wrote on last edited by behruz montazeri
    #1

    Header file :

    #ifndef MYRECT_H
    #define MYRECT_H
    #include <QGraphicsItem>
    #include <QPropertyAnimation>
    
    class MyRect:public QGraphicsItem
    {
        Q_PROPERTY(QRect nrect READ nRect WRITE setNrect)
    
    public:
        MyRect();
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
        QRectF boundingRect() const;
        QRect nRect()const;
        void setNrect(const QRect &rect);
    protected:
    private:
        QRect mRect;
        QPropertyAnimation *anime;
    
    };
    
    #endif // MYRECT_H
    

    In cpp file :

        anime = new QPropertyAnimation(this,"nrect");   //error
        anime->setDuration(2000);
        anime->setStartValue(QRect(0,0,10,10));
        anime->setEndValue(QRect(0,0,50,50));
        anime->start();
    
        QObject::connect(anime,&QPropertyAnimation::valueChanged,[=](){update();});
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      QGraphicsItem is not a QObject. It cannot have Qt properties.

      So, either use multiple inheritance to inherit from QGraphicsItem and QObject, or use Q_GADGET macro (and don't pass any parent to QPropertyAnimation).

      (Z(:^

      behruz montazeriB 1 Reply Last reply
      3
      • sierdzioS sierdzio

        QGraphicsItem is not a QObject. It cannot have Qt properties.

        So, either use multiple inheritance to inherit from QGraphicsItem and QObject, or use Q_GADGET macro (and don't pass any parent to QPropertyAnimation).

        behruz montazeriB Offline
        behruz montazeriB Offline
        behruz montazeri
        wrote on last edited by behruz montazeri
        #3

        @sierdzio
        Thanks.How should i create QPropertyAnimation object ?

        1 Reply Last reply
        0
        • behruz montazeriB Offline
          behruz montazeriB Offline
          behruz montazeri
          wrote on last edited by
          #4
          class MyRect:public QGraphicsItem,public QObject
          {
              Q_PROPERTY(QRect nrect READ nRect WRITE setNrect)
          
          

          It seems QPropertyAnimation doen't know nrect
          QPropertyAnimation: you're trying to animate a non-existing property nrect of your QObject

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Do you have Q_OBJECT macro in your header file?

            (Z(:^

            behruz montazeriB 2 Replies Last reply
            0
            • sierdzioS sierdzio

              Do you have Q_OBJECT macro in your header file?

              behruz montazeriB Offline
              behruz montazeriB Offline
              behruz montazeri
              wrote on last edited by behruz montazeri
              #6

              @sierdzio
              If i add Q_OBJECT it generates errors.
              I made a new class from QObject and it solves the problem.

              sierdzioS 1 Reply Last reply
              0
              • behruz montazeriB behruz montazeri

                @sierdzio
                If i add Q_OBJECT it generates errors.
                I made a new class from QObject and it solves the problem.

                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                @behruz-montazeri said in Error on creating QPropertyAnimation object:

                @sierdzio
                If i add Q_OBJECT it generates errors.

                What errors? You need to fix them.

                There is no way a QObject with properties will work without the macro, you have to have it.

                (Z(:^

                1 Reply Last reply
                0
                • sierdzioS sierdzio

                  Do you have Q_OBJECT macro in your header file?

                  behruz montazeriB Offline
                  behruz montazeriB Offline
                  behruz montazeri
                  wrote on last edited by
                  #8

                  @sierdzio
                  Also instead of QGraphicsItem We can use QGraphicsObject

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved