insertItem
void QListWidget::insertItem(int row, QListWidgetItem *item)
Inserts the item at the position in the list given by row.
在QListWidget 指定位置(row参数)插入一项。
在这种方法中,需要注意一点:
在新建项的时候,项的父控件不能是QListWidget,否则还是添加到QListWidget的最后。
问题1:
错误的写法:
QListWidgetItem * listItem = new QListWidgetItem(QListWidget);
正确的方法:
QListWidgetItem * listItem = new QListWidgetItem();
问题2:
重置代理解决列表虚线问题鼠标焦点问题,重写sizeHint会导致QListWidgetItem::setSizeHint设置的行高失效
问题3:
假设QListWidget有以下Item:
Item1
Item2
Item3
Item4
item5
如果被删除的是最后一个Item时,也就是Item5,那么该信号的参数currentRow会自动减1,也就是3;
如果被删除的不是最后一个Item时,currentRow竟然会+1!也就是如果删除的时Item4,那么currentRow会是4!但是这时候QListWidget::count也只有4!
所以,如果你有使用QListWidget的删除业务时,最好不要使用currentRowChanged这个信号!
如果你要删除item3 item4 item5, 实际上使用的行号是不变的
QListWidgetItem* pItem = this->takeItem(2);
this->removeItemWidget(pItem);
if (pItem != nullptr)
{
delete pItem;
pItem = nullptr;
}