Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to edit the Qlistwidget item on the contextmenu



  • I want to make three action in the listwidget contextmenu.
    when I right click on the action that is named "rename"

            #右键菜单
            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):
            test =QListWidgetItem("test")
            test.setTextAlignment(Qt.AlignCenter)
            self.leftWidget.addItem(test)
    
    
        #删除分组
        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.openPersistentEditor(item)
    
    

  • Qt Champions 2019

    @Triomphe Please explain clearly what you want to do. In what way do you want to "edit the Qlistwidget item"?


Log in to reply