主要用这个函数:

void QListWidget::setItemWidget(QListWidgetItem * item, QWidget * widget)

然后应用css样式美化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
ui->alarmListWidget->setResizeMode(QListView::Adjust);
ui->alarmListWidget->setAutoScroll(true);

QWidget *wContainer = new QWidget(ui->alarmListWidget);
QHBoxLayout *hLayout = new QHBoxLayout(wContainer);

QLabel *alarmIcon = new QLabel(tr("beih"));
QLabel *placeLabel = new QLabel(tr("北京"));
QLabel *videoNumLabel = new QLabel(tr("8"));
QLabel *dateLabel = new QLabel(tr("2013-4-16"));
QLabel *alarmMsgLabel = new QLabel(tr("违章搭建"));
//QPushButton *pDeleteBtn = new QPushButton(QIcon(),tr("delete"));

hLayout->addWidget(alarmIcon);
hLayout->addStretch(1);//将空白没有widget的地方分成了若干份,按比例分配
hLayout->addWidget(placeLabel);
hLayout->addStretch(1);
hLayout->addWidget(videoNumLabel);
hLayout->addStretch(1);
hLayout->addWidget(dateLabel);
hLayout->addStretch(1);//将空白没有widget的地方分成了若干份,按比例分配
hLayout->addWidget(alarmMsgLabel);
hLayout->setContentsMargins(5,0,0,5);//关键代码,如果没有很可能显示不出来
// wContainer->setLayout(hLayout);//如果layout在创建时就已经将父窗口指针当参数,那就不用setlayout
wContainer->resize(350,50);
// wContainer->show();

QListWidgetItem *alarmItem = new QListWidgetItem(ui->alarmListWidget);
ui->alarmListWidget->setItemWidget(alarmItem,wContainer);

参考:https://blog.csdn.net/xzh_blue/article/details/49178395