Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put an image and a QProgressBar inside a QTableView?

Tags:

c++

qt

I'm developing some kind of download manager and display the file name, it's size and the remaining bytes in a QTableView. Now I want to visualize the progress with a QProgressBar and display an image (to indicate whether it's an down- or upload). How can I add or display a QProgressBar and an image inside the QTableView?

like image 918
Marco Avatar asked Feb 26 '23 07:02

Marco


1 Answers

If you are using QTableView, I presume you use a model linked to this view.

One solution would be to use delegates (see QItemDelegate) to paint the progress, In QItemDelegate::paint method you have to define, use QStyle of the widget (widget->style()) to paint the progress (use QStyle::drawControl with QStyle::CE_ProgressBarContents as control identifier).

Check the documentation from the example Star Delegate, to see how to define the delegate for the column you need.

Later edit: Example of defining the delegate paint method (code sketch, not really tested, take it as a principle, not fully working).

void MyDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
    QStyleOptionProgressBar progressStyle;
    progressStyle.rect = option.rect; // Maybe some other initialization from option would be needed

    // For the sake of the example, I assume that the index indicates the progress, and the next two siblings indicate the min and max of the progress.
    QModelIndex minIndex = index.sibling( index.row(), index.column() + 1);
    QModelIndex maxIndex = index.sibling( index.row(), index.column() + 2);

    progressStyle.minimum = qvariant_cast< int>( minIndex.data( Qt::UserRole));
    progressStyle.maximum = qvariant_cast< int>( maxIndex.data( Qt::UserRole));

    progressStyle.progress = qvariant_cast< int>( index.data( Qt::UserRole));
    progressStyle.textVisible = false;
    qApp->style()->drawControl( QStyle::CE_ProgressBarContents, progressStyleOption, painter);
}
like image 70
Cătălin Pitiș Avatar answered Mar 08 '23 11:03

Cătălin Pitiș