문제

I'm trying to get like a checkerboard pattern into a QListWidget. for example, have the first item in the list a dark grey background, and the second item in the list would have a light grey background and keep alternating like that as it goes down. Is there a simple way to accomplish this?

I know I can do it by creating a class, extending a QListWidget and then just set the styles in it each time the list updates but I'm looking for the simplest way possible. Any answers would be helpful.

도움이 되었습니까?

해결책

 listWidget->setAlternatingRowColors(true);

It's a built-in property of Qt.

If you use designer:

enter image description here

다른 팁

I believe this should work

myList->setAlternatingRowColors(true);
myList->setStyleSheet("alternate-background-color: white;background-color: black;");

use pseudo-state - :alternate in stylesheet

Example:

QListView::item {
    background-color: #1C80A6;
    color: white;}
QListView::item:alternate {
    background-color: white;
    color: #1C80A6;}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top