Jay
Jay

Reputation: 14471

Animation of list items/redrawing Qt QListView

What I'd like to achieve:

A visual indication to draw attention to newly added items in a QListView. I had in mind having the background color 'throb' once (fading from a color to the background).

The setup

I have a model/view using QListView displaying QStandardItems. Qt version 4.7

What I've tried:

I've created a new class derived from QStyledItemDelegate. I have my own paint method to render the item. That part works. I created a QTimeLine object and set it up to create events to redraw the items.

I can't figure out how to trigger redraws of the QListView item.

In the item delegate constructor:

   timeLine = new QTimeLine( 3000, this );
   timeLine->setFrameRange( 100, 0 );
   connect( timeLine, SIGNAL( frameChanged( int ) ), this, SLOT( update() ) );
   timeLine->start();

I tried connecting to the sizehintChanged event but this does not work

void myDelegate::update()
{
   const QModelIndex index;
   emit QStyledItemDelegate::sizeHintChanged( index );
}

Any suggestions? Can this be done with style sheets?

Upvotes: 3

Views: 3084

Answers (1)

Abhijith
Abhijith

Reputation: 2642

The standard practice to include animations into code is to use state machines. Animations in Qt cannot be achieved using QtStylesheets. Either use QML or use QStyledItemDelegate and a state machine.

 /*CustomItemDelegate*/

    int state;
    enum states{
        animating,
        normal
     }

    void setstate(int state){
        this->state = state;
        /*Start animation depending on state ,by starting a QTimer and calling 
        repaint when the timer expires,also change animation variables like opacity ,         
        angle etc etc*/

    }

     void paint(QPainter *painter, const QStyleOptionViewItem &option,
            const QModelIndex &index) const{

          switch(state){
               case animating:
                     break;
               case normal;
                     break;
          }
      }
    ....

 /*CustomListView*/

    slots:
       void dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight ){
              ( (CustomItemDelegate)itemDelegate(topleft) )->setState(animating);
       } 
    ....


 /*Mainwindow*/
      connect(model,SIGNAL(datachanged(QModelIndex,QModelindex)),view,SLOTS(QModelindex,QModelindex));

Upvotes: 5

Related Questions