QListWidget::setItemWidget The Selection area behind the label image
-
- i use the
QListWidgetto preview some images files. I did not use the setIcon. I did inherit QWidget, QWidget include two QLabel to load imags and to show some text. - i use the following code to add the item;
pItemWidget = new ListItemWidget(this); // My Widget pItemWidget->SetPixmap( pixmap ); // My customize function: Qlabel load pixmap pItemWidget->SetImgPath( qsFilePath );// My customize function: show some text pItem = new QListWidgetItem(this); pItem->setSizeHint( pItemWidget->size() ); pItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); setItemWidget(pItem, pItemWidget);-
when i use the mouse to choose the list widget item, i find the selection area behind the label image. i do not know how to do ? help me...
-
My Problem image

-
My Code. My Zip code, You can click and download My code Demo
- i use the
-
@joeQ The selection rect is already at foreground. There might be some other problem in your code.
Here a simple test I did:for(int i=0; i<10; i++) { QListWidgetItem *item = new QListWidgetItem; item->setText("Item"); item->setIcon(QIcon("image.png")); ui->listWidget->insertItem(row, item); } -
@joeQ The selection rect is already at foreground. There might be some other problem in your code.
Here a simple test I did:for(int i=0; i<10; i++) { QListWidgetItem *item = new QListWidgetItem; item->setText("Item"); item->setIcon(QIcon("image.png")); ui->listWidget->insertItem(row, item); } -
@joeQ Unfortunately I too am not aware of this kind of behaviour. I have reduced the problem to the following for anyone else to test:
listWidget->setViewMode(QListView::IconMode); listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); for(int i=0; i<10; i++) { QPushButton *label = new QPushButton; label->resize(50,50); label->setText("Button"+QString::number(i)); QListWidgetItem *item = new QListWidgetItem; item->setSizeHint(label->size()); listWidget->insertItem(i, item); listWidget->setItemWidget(item, label); }For eg. following works:
Use
QLabelinstead and set only thetext. But if we set pixmap for label then its the same behavior.
Is there any other setting ? Or only subclassing might solve this problem ? -
@joeQ Unfortunately I too am not aware of this kind of behaviour. I have reduced the problem to the following for anyone else to test:
listWidget->setViewMode(QListView::IconMode); listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); for(int i=0; i<10; i++) { QPushButton *label = new QPushButton; label->resize(50,50); label->setText("Button"+QString::number(i)); QListWidgetItem *item = new QListWidgetItem; item->setSizeHint(label->size()); listWidget->insertItem(i, item); listWidget->setItemWidget(item, label); }For eg. following works:
Use
QLabelinstead and set only thetext. But if we set pixmap for label then its the same behavior.
Is there any other setting ? Or only subclassing might solve this problem ? -
Hi,
It's because you are setting a widget on a cell, the widget is "above" the cell.
If you only want to show an image then you should use a custom QStyledItemDelegate.
-
Hi,
It's because you are setting a widget on a cell, the widget is "above" the cell.
If you only want to show an image then you should use a custom QStyledItemDelegate.
@SGaist Hi, Thank u. and if I don't want to use a custom QStyledItemDelegate. What should I do ?
My Widget has image label and text label, The important thing is that some of my structure data is stored in the Widget class members, eg: structure pointer.
I also used try
setItemDatafunction, but Data type mismatch. so, i subclass the QWidget. -
From the setItemWidget documentation: it's to display static content. It's not meant to handle D&D or editing.
Why do you need to embed custom data in that widget ?
-
From the setItemWidget documentation: it's to display static content. It's not meant to handle D&D or editing.
Why do you need to embed custom data in that widget ?