QListWidget使用问题记录

1、内存泄漏问题

在Qt docs中,它表示如下:

If you need to insert a new item into the list at a particular
position, then it should be constructed without a parent widget. The
insertItem() function should then be used to place it within the list.
The list widget will take ownership of the item.

可以了解到Item不需要父控件,会自动释放,不用考虑内存泄漏问题。

2、滚动条显示问题

ui->listWidgetDesktop->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->listWidgetDesktop->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

3、不要重复使用Item,会出现显示位置重叠问题,解决办法是只通过new新的item并insert,经过测试不会出现两个图标重叠现象。

ui->listWidget->clear();
for(int i = (mPageIndex -1)*ITEM_COUNT_OF_PAGE;i < mDesktopList.count();++i){
    QListWidgetItem *item = new QListWidgetItem();
    QWidget *form = new QWidget(this);
    item->setSizeHint(QSize(ITEM_WIDTH, ITEM_HEIGHT));
    ui->listWidge->addItem(item);
    form->setSizeIncrement(ITEM_WIDTH, ITEM_HEIGHT);
    ui->listWidgetDesktop->setItemWidget(item, form);
    form->setModelInfo(mCloudDesktopList.at(i));
}

猜你喜欢

转载自blog.csdn.net/cai742925624/article/details/127093318