Put and animate a progress-bar in QTableView



  • I am using a QTableView that displays a simple 1-column QAbstractItemModel-based model (QAbstractTableModel can be used as well). The values in the model represent progress - from 0% to 100%.

    Aim: display the progress using a progress-bar.
    There are several options:

    1. Set a QItemDelegate-based delegate to the QTableView using QTableView::setItemDelegateForColumn(). Make the delegate print the progress-bar using a similar code:
      @
      void ProgressBarDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
      {
      QStyleOptionProgressBar progressBarStyleOption;
      progressBarStyleOption.rect = option.rect;

      const int progress = <get-the-progress-from-the-model>;

      progressBarStyleOption.minimum = 0;
      progressBarStyleOption.maximum = 100;
      progressBarStyleOption.textAlignment = Qt::AlignCenter;
      progressBarStyleOption.progress = progress ;
      progressBarStyleOption.text = QString( "%1%" ).arg( progress );
      progressBarStyleOption.textVisible = true;

      qApp->style()->drawControl( QStyle::CE_ProgressBar, &progressBarStyleOption, painter );
      }
      @

    2. Use the QTableView's setIndexWidget member add a living QProgressBar over what is printed by the default QTableWidget delegate in the given index.

    3. Use a QTableWidget instead of QTableView and use QTableWidget::setCellWidget() again to put a living QProgressBar.

    Solution #3: Forget about it - QTableWidget doesn't allow setting a custom QAbstractItemModel.
    Solution #2: setIndexWidget works correctly in C++ Qt but when using PySide Qt (not PyQt), the progressbars get deleted from time to time due to the garbage collection. Also, one needs to set QWidget::setAutoFillBackground() for the QProgressBar being inserted at a given index, because otherwise if the QProgressBar doesn't paint the whole cell, the cell will display the contents painted by the default cell's delegate and will paint the QProgressBar over them. This is a problem in the case of running Mac OS X, since the progressbars there are not resizable vertically. The default QTableView delegate could be overridden by setting a one that doesn't paint anything.

    Solution #1: This is the preferred solution to the case since it is the most MVC compliant one, the most lightweight one and works perfectly well using PySide Qt.

    The problem: Solution #1: Progress-bars are not animated (Windows 7 Aero draws a green progress-bar that flashes. Mac OS X Lion draws a blue progress-bar that has an animation that resembles flowing water). So when the progress bar is at 50% percent, the filled part of the progress-bar should be animated using Windows 7 Aero flash animation or Mac OS X Lion's water animation.

    Please provide a solution how to add an animate the progress-bar in Solution #1 or a better way to display a progress-bar in QTableView's cells.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.