pyqt-todolist/test/QListWidgetTest.py

54 lines
1.8 KiB
Python

self.leftWidget =QListWidget()
#禁止双击可编辑
self.leftWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
#右键菜单
self.leftWidget.setContextMenuPolicy(Qt.CustomContextMenu)
self.leftWidget.customContextMenuRequested.connect(self.myListWidgetContext)
def myListWidgetContext(self,position):
#弹出菜单
popMenu = QMenu()
creAct =QAction("新建分组",self)
delAct =QAction("删除分组",self)
renameAct =QAction(u'重命名', self)
#查看右键时是否在item上面,如果不在.就不显示删除和修改.
popMenu.addAction(creAct)
if self.leftWidget.itemAt(position):
popMenu.addAction(delAct)
popMenu.addAction(renameAct)
creAct.triggered.connect(self.CreateNewItem)
renameAct.triggered.connect(self.RenameItem)
delAct.triggered.connect(self.DeleteItem)
popMenu.exec_(self.leftWidget.mapToGlobal(position))
#创建新的分组
def CreateNewItem(self):
#创建一个没有名字的item
item =QListWidgetItem("")
item.setTextAlignment(Qt.AlignCenter)
#使得item是可以编辑的.
item.setFlags(item.flags() | Qt.ItemIsEditable)
self.leftWidget.addItem(item)
#创建后就可以编辑item,用户自己起名字.
self.leftWidget.editItem(item)
#删除分组
def DeleteItem(self):
self.leftWidget.takeItem(self.leftWidget.currentRow())
#重命名分组
def RenameItem(self):
curRow =self.leftWidget.currentRow()
item=self.leftWidget.item(curRow)
item.setFlags(item.flags() | Qt.ItemIsEditable)
self.leftWidget.editItem(item)
self.leftWidget.itemChanged.connect(lambda :self.ChangeItem(item))
def ChangeItem(self,item):
print("test")