After hours of work, I can draw a widget on a QListView . However, the picture is done through QPixmap . A widget will appear and I see a progress bar. However, it is a bit pixelated (due to the use of QPixmap ). Is it possible to draw directly as a regular widget? This is my question.
The following is what I am doing:
void FileQueueItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QPaintDevice* original_pdev_ptr = painter->device(); FileQueueListItem* itemWidget = reinterpret_cast<FileQueueListItem*>(index.data(Qt::UserRole).value<void*>()); itemWidget->setGeometry(option.rect); painter->end(); QPixmap pixmap(itemWidget->size()); if (option.state & QStyle::State_Selected) pixmap.fill(option.palette.highlight().color()); else pixmap.fill(option.palette.background().color()); itemWidget->render(&pixmap,QPoint(),QRegion(),QWidget::RenderFlag::DrawChildren); painter->begin(original_pdev_ptr); painter->drawPixmap(option.rect, pixmap); }
I learned how to do what I did with the tips here . There, the picture is executed directly on QListView , which I want to achieve. What am I doing wrong for the next attempt not to work:
void FileQueueItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { std::cout<<"Painting..."<<std::endl; QPaintDevice* original_pdev_ptr = painter->device(); FileQueueListItem* itemWidget = reinterpret_cast<FileQueueListItem*>(index.data(Qt::UserRole).value<void*>()); itemWidget->setGeometry(option.rect); painter->end(); if (option.state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight()); else painter->fillRect(option.rect, option.palette.background()); itemWidget->render(painter->device(), QPoint(option.rect.x(), option.rect.y()), QRegion(0, 0, option.rect.width(), option.rect.height()), QWidget::RenderFlag::DrawChildren); painter->begin(original_pdev_ptr); }
The list remains empty and nothing happens. Although the selection can be seen, the widget is not displayed.