先上代码
QListWidget *listWidget = new QListWidget();
QListWidgetItem *wi = new QListWidgetItem;
wi->setText("greenbg");
wi->setBackgroundColor(Qt::green);
listWidget->addItem(wi);
//1.QSS修改QListwidget的item
listWidget->setStyleSheet( "QListWidget::item {border-style: solid; border-width:1px; border-color:black;}");
QListWidgetItem *wi2 = new QListWidgetItem;
wi2->setText("redbg");
//2.成员函数修改QListwidget的item
wi2->setBackgroundColor(Qt::red);
listWidget->addItem(wi2);
listWidget->show;
解决办法:
法1.去掉QSS完全用成员函数设置item;
把第二处的设置改为如下:
listWidget->setStyleSheet( "QListWidget::item
{"
"border-style: solid;"
"border-width:1px;"
"border-color:black;"
"background-color: green;"
"}"
"QListWidget::item:selected
{"
"background-color: red;"
"}"
);
法2.完全用QSS在两处设置item;
其实QT 的QSS是一个强大而又方便的特性!