Why does the qlabel background in qlistwidget not display?



  • hi.

    I created a custom qlistwidget.

    code as follows:

    QListWidgetItem * item = new QListWidgetItem(ui->listWidget);
    ui->listWidget->addItem(item);
    
    QLabel * lItem = new QLabel();
    lItem->setAutoFillBackground(true);
    
    QPalette p = lItem->palette();
    p.setBrush(QPalette::Window, QBrush(QColor(Qt::blue)));
    lItem->setPalette(p);
    
    ui->listWidget->setItemWidget(item, lItem);
    
    item->setSizeHint (QSize(lItem->rect().width(), lItem->rect().height()));
    

    But its blue background does not display,why?

    Thanks!


  • Lifetime Qt Champion

    Hi,

    Why do you need QLabels in your QListWidget ?



  • @SGaist Hi

    I want to implement a button with background pictures and foreground strings.

    QLabel is found to be the simplest.And QLabel implements the background fillet is also very simple.

    Outside of QListWidget,It works well.


  • Lifetime Qt Champion

    Then why do you need a QListWidget for ?



  • @SGaist Thank you

    Listwidget is not the point of the problem.

    Actually, I have many components in QListWidgetItem.

    I've just chosen one of the small parts as an example.

    In this QListWidgetItem part, there is a local area that uses qlabel.The qlabel also uses foreground and background features.

    But the background of qlabel is invisible.


  • Lifetime Qt Champion

    Well, in fact, it might be. Many people are using QListWidget has a holder for a list of widget rather than using e.g. QStyledItemDelegate to do the drawing of a special column or cell.



  • Instead of using a QLabel in a QListWidgetItem just call item ->setData(Qt::BackgroundRole,QBrush(QColor(Qt::blue));


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.