其他分享
首页 > 其他分享> > QListWidget自定义item实现

QListWidget自定义item实现

作者:互联网

效果如下:
在这里插入图片描述
关键代码:

//添加item
void CListTestWgt::AddItem(MarkType type, int serialNum, const QString &content) 
{
    CItemWidget *pItemWidget = new CItemWidget(this);
    pItemWidget->SetData(type, serialNum, content);
    
    QListWidgetItem *pItem = new QListWidgetItem();
    
    if (type == ellipse)
      pItem->setSizeHint(QSize(350, 110));
    else
      pItem->setSizeHint(QSize(350, 55));
    
    ui->listWidget->addItem(pItem);
    ui->listWidget->setItemWidget(pItem, pItemWidget);
    
    connect(pItemWidget, SIGNAL(sgnDeleteItem()), this, SLOT(sltDeleteItem()));
}
//删除item
void CListTestWgt::sltDeleteItem() 
{
    QPoint pPoint = ui->listWidget->mapFromGlobal(QCursor::pos());
    
    auto item = ui->listWidget->itemAt(pPoint);
    
    ui->listWidget->removeItemWidget(item);
    
    delete item;
}

标签:pItem,QListWidget,pItemWidget,自定义,item,ui,listWidget,type
来源: https://blog.csdn.net/oTianLe1234/article/details/121054845