How to increase the padding (or margins) for an item/row in a QListWidget?
Asked Answered
L

3

5

We're looking for a way to increase the padding (or margins) for a QListWidget we are using in our application. We'd like to increase this for all four directions to give the text in our list some extra space

I've looked at the documentation for both QListWidget and QListWidgetItem and can't find anything. For QListWidget there's setContentsMargins which is inherited from QWidget but that is for the widget as a whole (rather than individual entries).

What can we do to solve this? Grateful for help!

Ledge answered 21/3, 2018 at 14:55 Comment(4)
How are you creating the QListWidgetItem list for the QListWidget?Shaven
What about setContentsMargins and/or setSpacing?Halimeda
@Halimeda setSpacing did the trick, thanks!Ledge
Cant believe that it is 2024 and we still have to hack this using style sheets.Michaelmas
L
12

setSpacing(int) will increase the padding in all directions

(Thank you G.M. for your help!)

Ledge answered 23/4, 2018 at 11:24 Comment(1)
is it possible to increase only the bottom spacing (margin ?)Chairwoman
B
7

how about this

ui->listWidget->setStyleSheet("QListWidget {padding: 10px;} QListWidget::item { margin: 10px; }");
Berth answered 22/3, 2018 at 6:59 Comment(0)
S
2

we use css selector for thise

list = QListWidget()
list.addItem("item 1")
list.addItem("item 2")
list.addItem("item 3")

list.setStyleSheet("""
    QListWidget {
        background-color: red;
        padding:20px;

    }
    QListWidget::item {
        margin:20px;
        background-color: blue;

    }
""")
Squall answered 24/2, 2022 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.