How to place image and QProgressBar inside QTableView?

I am developing a download manager and showing the file name, its size and the remaining bytes in QTableView. Now I want to visualize the progress with the QProgressBar and display the image (to indicate whether it will be inaccessible or loaded). How to add or display QProgressBar and image inside QTableView?

+4
source share
5 answers

If you are using QTableView , I assume that you are using the model associated with this view.

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

Check the documentation from the Star Delegate example to find out how to determine the delegate for the desired column.

Later editing: Example of defining a delegate drawing method (sketching code that has not been tested, takes it as a principle, does not fully work).

 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); } 
+4
source
 TrackDelegate::TrackDelegate(QObject *parent) : QItemDelegate(parent) -------------------------------------------------------------------------------- void TrackDelegate::paint( QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem viewOption(option); QImage image(m_RowBackGroundImagePath); QPixmap pixmap(m_RowBackGroundImagePath); qDebug()<<"forward"<<pixmap.width()<<pixmap.height(); pixmap.scaled(option.rect.width(),option.rect.height()); qDebug()<<"back"<<pixmap.width()<<pixmap.height(); qDebug()<<option.rect.width()<<option.rect.height(); QBrush brush(pixmap); painter->save(); painter->fillRect(option.rect, brush/*QColor(238, 233, 233, 255)*/); painter->restore(); viewOption.rect = QRect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height()); // viewOption.palette.setColor(QPalette::Text, QColor(Qt::red)); // viewOption.palette.setBrush ( QPalette::ButtonText, brush1); QItemDelegate::paint(painter, viewOption,index); int progress = index.model()->data(index,Qt::DisplayRole).toInt(); QStyleOptionProgressBar progressBarOption; progressBarOption.rect = QRect(option.rect.x(), option.rect.y()+(SETHEIGHT - PROGRESSBARHEIGHT)/2, option.rect.width(), /*option.rect.height()*/PROGRESSBARHEIGHT); //qDebug()<<progressBarOption.rect.x()<<progressBarOption.rect.y()<<progressBarOption.rect.height()<<progressBarOption.rect.width(); //qDebug()<<option.rect.x()<<option.rect.y()<<option.rect.height()<<option.rect.width(); progressBarOption.state |= QStyle::State_Enabled; progressBarOption.direction = QApplication::layoutDirection(); progressBarOption.fontMetrics = QApplication::fontMetrics(); progressBarOption.minimum = 0; progressBarOption.maximum = 100; progressBarOption.textAlignment = Qt::AlignCenter; progressBarOption.textVisible = true; progressBarOption.progress = progress < 0 ? 0 : progress; progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress); QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); break; } 
+2
source

You might want to use a QTableWidget for this. It has a method that allows you to add widgets such as QProgressBar. This is the setCellWidget method.

+1
source

There is a slot in the QProgressBar called setValue (int) ,
You can update it by sending a signal to this progress bar from your file manager.
This one should be designed so that it can check or monitor the status of the download and periodically send these signals.

A good up / down / end image management approach should be to have an extra column in the table with the image element.
It would be fairly easy to update the image if the state of the socket / connection / file changed.

Writing any example will actually write you a program, so I suggest publishing parts of the problems (if any) using code that you yourself have executed.

0
source

QTableView not intended to display widgets in a layout. Use a QGridLayout or other suitable layout and put widgets in this layout.

0
source

All Articles