PyQt学习随笔:ListView控件删除一项列表项的方法

ListView控件可以通过控件对应数据存储删除列表项,具体使用:
数据存储.removeRow(元素索引位置)
删除指定位置的一个列表项。

数据存储如果不知道程序定义的数据存储名,可以通过model()函数获取对应ListView控件的数据存储。

案例:删除当前选择列表项的两种方式(m_ListView为listView控件名)

案例1:已知数据存储名为当前对象的itemmodel变量的实现代码

    def DelListItem(self):
         selected = self.m_ListView.selectedIndexes()
         selectindex = self.m_ListView.currentIndex()
         for i in selected:
             self.itemmodel.removeRow(i.row())

案例2:未知数据存储名的实现代码

 def DelListItem(self):
         selected = self.m_ListView.selectedIndexes()
         selectindex = self.m_ListView.currentIndex()
         itemmodel = self.m_ListView.model()
         for i in selected:
              itemmodel.removeRow(i.row())

上面两个案例定义的selectindex 没有使用,如果在控件是单选模式下,可以直接使用该变量去进行操作。对应代码:

 def DelListItem(self):
         selectindex = self.m_ListView.currentIndex()
         itemmodel = self.m_ListView.model()
         itemmodel.removeRow(selectindex.row())

老猿Python,跟老猿学Python!
博客地址:https://blog.csdn.net/LaoYuanPython

老猿Python博客文章目录:https://blog.csdn.net/LaoYuanPython/article/details/98245036
请大家多多支持,点赞、评论和加关注!谢谢!

原文地址:https://www.cnblogs.com/LaoYuanPython/p/11931728.html