2011-06-27 23 views
2

我想什麼來實現動畫列表項/重繪的Qt而QListView

視覺指示提請注意一個而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); 
} 

有什麼建議?這可以用樣式表完成嗎?

回答

4

將動畫包含到代碼中的標準做法是使用狀態機。 使用QtStylesheets無法實現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)); 
+0

謝謝Abhijith,我會嘗試狀態機方法。 – Jay

+0

項目委託沒有重繪方法。你必須在視圖上調用repaint嗎?如果是這樣,該觀點是代表的父母嗎? – Jay

+0

我在管理項目列表的QListView上重複調用repaint()。它不更新屏幕。如果我在點擊動畫時用鼠標點擊列表項目,那麼我會得到一個視覺更新。 – Jay