Qt World Summit: Register Today!

QListWidget::setItemWidget The Selection area behind the label image

    1. i use the QListWidget to 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.
    2. 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);
    1. 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...

    2. My Problem image The Images about my problem.

    3. My Code. My Zip code, You can click and download My code Demo

  • Moderators

    @joeQ Try setSelectionRectVisible(false)

  • @p3c0 i want to have the selection area. but the selection area behind the label image. i want the selection area before the label image. Thank u.

  • Moderators

    @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;
        ui->listWidget->insertItem(row, item);

  • @p3c0 Yes, i used the other way is ok. but, i used the setItemWidget to QListWidget, it is wrong. i don't know why. you can download my demo. step 4. Please help me. Thank u.

  • Moderators

    @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:

        for(int i=0; i<10; i++) {
            QPushButton *label = new QPushButton;
            QListWidgetItem *item = new QListWidgetItem;
            listWidget->insertItem(i, item);
            listWidget->setItemWidget(item, label);

    For eg. following works:

    Use QLabel instead and set only the text. But if we set pixmap for label then its the same behavior.
    Is there any other setting ? Or only subclassing might solve this problem ?

  • @p3c0 Thank u very much, I do not give it up. when i solved it , i will share my way for u.

  • Lifetime Qt Champion


    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 setItemData function, but Data type mismatch. so, i subclass the QWidget.

  • Lifetime Qt Champion

    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 ?

  • @SGaist I think i was wrong, i chosed the wrong solution. Thank u, and , i found some demo about delegate, i know , i should to use the QStyledItemDelegate class. Thank u very much. and i am new bird in Qt. so ...

Log in to reply