簡體   English   中英

PyQt:從QListWidget刪除自定義窗口小部件

[英]PyQt: removing custom widgets from a QListWidget

我正在將圖片加載到QGraphicsScene然后當您單擊照片時,它將在所單擊的區域中放置圓圈。 然后,我添加一個自定義小部件,該小部件將保留要點列表。 這個小部件有幾個按鈕。 其中一個可以移動圓,另一個可以刪除圓。 我目前停留在刪除部分。

問題

我可以刪除圓圈,也可以從列表中刪除小部件。 問題在於列表中仍然有一個小部件所在的位置,並且由於我使用的是小部件中的按鈕並且未選擇該項目,因此我不確定如何刪除該位置。 另外,如果我刪除一堆然后嘗試添加一些Python,它的自身將會崩潰,我也不知道為什么。

我不確定是否可以做我想做的事情,因為實際上沒有參考,或者我最好將按鈕移至主窗口並將其從自定義小部件中刪除。 如果可能的話,我想保持原樣。

有一些文件,所以我將其放在GitHub上,因此我不會占用大量空間。 任何幫助深表感謝。

GitHub鏈接

鏈接到GitHub Project

相關代碼(來自Main.py ):

class MainWindow(QMainWindow, Ui_MainWindow):
    ...

    def Add_History(self,pos):
        self.HistoryWidget = HistoryList()
        self.HistoryWidget.setObjectName("HistoryWidget_"+ str(Constents.analysisCount))
        myQListWidgetItem = QListWidgetItem(self.History_List)
        myQListWidgetItem.setSizeHint(self.HistoryWidget.sizeHint())
        self.History_List.addItem(myQListWidgetItem)
        self.History_List.setItemWidget(myQListWidgetItem, self.HistoryWidget)
        self.HistoryWidget.buttonPushed.connect(self.deleteObject)
        self.HistoryWidget.setXpoint(str(pos.x()))
        self.HistoryWidget.setYpoint(str(pos.y()))
        self.HistoryWidget.setHistoryName("Point "+ str(Constents.analysisCount))
        Constents.analysisCount = Constents.analysisCount + 1

    def deleteObject(self):
        sender = self.sender() #var of object that sent the request
        row_number = sender.objectName().split("_")
        number = row_number[1]
        x,y = Constents.analysisDetails[str(number)]# getting xy for object
        self.loadPicture.findAndRemoveAnalysisPoint(x,y) #calling the class with the scense to delete it
        Constents.analysisDetails.pop(str(number)) # get rid of the object in the variables
        HistoryWidget = self.findChildren(HistoryList, "HistoryWidget_"+number)[0] #find the actual object

        HistoryWidget.deleteLater() #delete that object
        #Simport pdb; pdb.set_trace()
        #self.History_List.takeItem(HistoryWidget)

圖片

python崩潰的圖片

在此處輸入圖片說明

您必須同時刪除item-widget和item本身。 為此,需要一種從item-widget(或其子小部件之一)中獲取項目的方法。 一種干凈的方法是使用列表小部件的itemAt方法,該方法可以從屏幕上的某個點獲取項目。 這樣做的主要好處是它不需要了解項目的索引,而當刪除其他項目時,當然可以改變。 這也意味着item-widget不需要了解與它們關聯的特定list-widget項的任何信息。

這是您的deleteObject方法的deleteObject ,該方法實現了:

def deleteObject(self):
    sender = self.sender() #var of object that sent the request
    row_number = sender.objectName().split("_")
    number = row_number[1]
    x,y = Constents.analysisDetails[str(number)]# getting xy for object
    self.loadPicture.findAndRemoveAnalysisPoint(x,y) #calling the class with the scense to delete it
    Constents.analysisDetails.pop(str(number)) # get rid of the object in the variables

    # get the viewport coords of the item-widget
    pos = self.History_List.viewport().mapFromGlobal(
        sender.mapToGlobal(QtCore.QPoint(1, 1)))
    if not pos.isNull():
        # get the item from the coords
        item = self.History_List.itemAt(pos)
        if item is not None:
            # delete both the widget and the item
            widget = self.History_List.itemWidget(item)
            self.History_List.removeItemWidget(item)
            self.History_List.takeItem(self.History_List.row(item))
            widget.deleteLater()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM