Question

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");
Was it helpful?

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.

OTHER TIPS

list->setStyleSheet(QString("QListWidget { background-image: url(imagename.png) }"));

See documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top