QListWiget item scroll off behaviour
-
I have a QListWidget with multiple items with QLables with images set as the itemWidgets when the list is in scrollperpixel mode the items when they are scrolled off the top of the list do not leave the visible area as expected when the code is run on iOS while they do leave the visible area as expected on macOS. Is there some setting I need to set to change the behaviour for items leaving the screen?
list = new QListWidget(this); list->setGeometry(50,50,300,500); list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); for(int i=0;i<15;i++){ QListWidgetItem *temp = new QListWidgetItem(); QLabel *widget = new QLabel(this); widget->setGeometry(0,0,250,100); widget->setPixmap(QPixmap(":/waveofthefuture.jpg")); //random jpg to make the issue evident temp->setSizeHint(QSize(250,100)); list->addItem(temp); list->setItemWidget(temp,widget); }
(using Qt version 5.12.11 but same issue is present of 6.2.0) -
Hi
Super with video.
I have not seen any settings like that.It seems the QLabel gets compressed. (maybe)
You could try set Fixed-size matching the image and see if that alters it.That said, a Delegate would be much more efficient than using QLabels.
Update:
Tested your code on Win 10, Qt 5.15.2 and it didn't do like that. (Looked like Ios)Update 2:
Could you try a delegate ?
Also if you test please do (also)
// ui->listWidget->setItemDelegate(new ImageDelegate);
and see how close the result are just using the DecorationRole :)#include <QStyledItemDelegate> #include <qpainter.h> class ImageDelegate : public QStyledItemDelegate { public: ImageDelegate(QObject *poParent = nullptr) : QStyledItemDelegate(poParent) {} public: virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QPixmap img = index.data(Qt::DecorationRole).value<QPixmap>(); if ( ! img.isNull()) { painter->drawPixmap(option.rect, img); } } };using it:
auto list = ui->listWidget; list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); for (int i = 0; i < 15; i++) { QListWidgetItem *item = new QListWidgetItem(); item->setData(Qt::DecorationRole, QPixmap(":/test.png")); list->addItem(item); } ui->listWidget->setItemDelegate(new ImageDelegate); -
Hi
Super with video.
I have not seen any settings like that.It seems the QLabel gets compressed. (maybe)
You could try set Fixed-size matching the image and see if that alters it.That said, a Delegate would be much more efficient than using QLabels.
Update:
Tested your code on Win 10, Qt 5.15.2 and it didn't do like that. (Looked like Ios)Update 2:
Could you try a delegate ?
Also if you test please do (also)
// ui->listWidget->setItemDelegate(new ImageDelegate);
and see how close the result are just using the DecorationRole :)#include <QStyledItemDelegate> #include <qpainter.h> class ImageDelegate : public QStyledItemDelegate { public: ImageDelegate(QObject *poParent = nullptr) : QStyledItemDelegate(poParent) {} public: virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QPixmap img = index.data(Qt::DecorationRole).value<QPixmap>(); if ( ! img.isNull()) { painter->drawPixmap(option.rect, img); } } };using it:
auto list = ui->listWidget; list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); for (int i = 0; i < 15; i++) { QListWidgetItem *item = new QListWidgetItem(); item->setData(Qt::DecorationRole, QPixmap(":/test.png")); list->addItem(item); } ui->listWidget->setItemDelegate(new ImageDelegate);@mrjj Thank you the setData and using the DecorationRole was sufficient to solve the problem on initial testing with the Delegate also working but making it so I didn't need to set the correct size of my pixmap. Never knew that the data on a list widget item could be a pixmap and have it render.
-
Note for if somebody else runs into this problem in the future and finds this thread you may need "Qt::BackgroundRole" instead of Qt::DecorationRole if you still need to have sub widgets to the item to have other behaviour.