tags:

views:

173

answers:

5

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?

A: 

There is a slot in QProgressBar called setValue(int),
You can update it sending a signal to this progress bar from Your file manager.
This one should be designed in a way it could check or monitor download state and sends those signals periodically.

Good approach to manage up/down/finish images would be to have additional column in table with the image item.
It would be quite easy to update your image if the socket/connection/file corrupt state would change.

Writing any example would be actually writing you a program, so i suggest to post parts of the problems (if any) with code performed by yourself.

bua
Yep I know that, the question is how to display the progress bar *inside* the TableView. I'll write the question clearer.
Marco
A: 

QTableView is not for displaying widgets in a layout. Use QGridLayout or some other suitable layout and put the widgets in that layout.

teukkam
why not use table? if the downloader has 100** files to manage this is good solution... Like in Opera download manager for instance or any other browser. Many features for free, sorting, filtering...
bua
You are right, I read the question hastily and though he was going to use a QTableView to lay out UI items à la HTML... it's probably fine for this kind of use.
teukkam
+2  A: 

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);
}
Cătălin Pitiș
I already looked at the Star Delegate example, but their they just paint polygons. Can you provide more information how to paint a widget inside the `QItemDelegate::paint` method?
Marco
Code sketch added.
Cătălin Pitiș
+3  A: 

You can give a look at the Qt Torrent client sample: it shows a progress bar inside a table.

tibur
A: 

Look at the source code of the Arora browser, which is written with Qt. The download manager is similar to Opera.