Search code examples
c++qtbackground-imageqlistwidget

How to set image and size of QListWidget in qt


How to insert a background image in QListWidget and How to set the size of QListWidget?

QListWidget *list = new QListWidget();
list->addItem("Hello");
list->addItem("Hi");

Solution

  • You have to set the background image through stylesheets using setStyleSheet.

    list->setStyleSheet("background-image: url(image_url.png)");
    

    If you want it to be in the center, just add:

    list->setStyleSheet("background-image: url(image_url.png); background-position: center;");
    

    If you want to set the size, you can do it through the stylesheet too as a fixed size:

    list->setStyleSheet("background-image: url(image_url.png); max-width:100px;min-width:100px; max-height:100px; min-height:100px;");
    

    Setting max and min of them is the same as doing this by code:

    list->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    list->setFixedSize(100,100);
    

    Hope it helps.