QListWidget 使用
1、QListWidget 插入Qwidget
QListWidgetItem *widgetItem = new QListWidgetItem(ui->listWidget); widgetItem->setSizeHint(QSize(94, 48)); ui->listWidget->addItem(widgetItem); WgFriendList* wgFriend = new WgFriendList( this); ui->listWidget->setItemWidget(widgetItem, wgFriend);
WgFriendList继承字QWidget。上述将一个窗口放入QListWidget的行中。
2、双击item项
void OnItemDoubleClicked(QListWidgetItem * item) { int nIndex = ui->listWidget->currentIndex().row(); qDebug() << "OnItemDoubleClicked" << nIndex; QWidget* wg = ui->listWidget->itemWidget(item); WgPersonInfo* wgPi = (WgPersonInfo*)wg; }
3、QListWidget 鼠标划过,按下背景色改变
Qss
1 QListWidget 2 { 3 background-color: rgba(0,108,255, 0.2); 4 color:white; 5 font-family:Microsoft YaHei; 6 } 7 QListWidget::Item 8 { 9 border-bottom:1px solid rgba(0,106,255,0.5); 10 } 11 12 QListWidget::Item:hover 13 { 14 background:rgba(0,191,255,0.3); 15 font-family:Microsoft YaHei; 16 } 17 18 QListWidget::Item:selected 19 { 20 background:rgba(0,191,255,0.8); 21 color:white; 22 font-family:Microsoft YaHei; 23 }