列表项的动画/重绘Qt QListView

3

我希望实现的目标:

在QListView中添加新项时,通过视觉提示来引起注意。我想让背景颜色闪烁一次(从一种颜色渐变到背景色)。

设置

我使用QListView显示QStandardItems的模型/视图。Qt版本为4.7。

我尝试过的方法:

我创建了一个派生自QStyledItemDelegate的新类。我有自己的绘制方法来呈现该项。这部分是有效的。我创建了一个QTimeLine对象,并设置它创建事件以重新绘制项目。

我无法弄清楚如何触发QListView项目的重绘。

在项委托构造函数中:

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

我尝试连接sizehintChanged事件,但这并不起作用。

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

任何建议?这是否可以使用样式表完成?
1个回答

5

将动画包含在代码中的标准方法是使用状态机。 在Qt中,无法使用Qt样式表实现动画。要么使用QML,要么使用QStyledItemDelegate和状态机。

 /*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));

谢谢Abhijith,我会尝试状态机方法。 - Jay
项代理没有重绘方法。您必须在视图上调用repaint吗?如果是这样,那么视图是否是代理的父级? - Jay
我在管理项目列表的QListView上重复调用repaint(),但它不会更新屏幕。如果我在动画过程中用鼠标单击列表项,则会获得单个视觉更新。 - Jay
2
哦,显然不行。您可以在itemdelegate中添加一个repaintRequest()信号,并将其连接到QListview中的repaint()槽。这将进而调用列表的委托的重绘。 - Abhijith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接