gpt4 book ai didi

python - PyQt:从 QListWidget 中删除自定义小部件

转载 作者:行者123 更新时间:2023-12-01 02:28:04 25 4
gpt4 key购买 nike

我正在将图片加载到QGraphicsScene中,然后当您单击照片时,它会在单击的区域中放置圆圈。然后我添加一个自定义小部件,它将保留点列表。这个小部件有几个按钮。其中一个可以移动圆圈,另一个可以删除它。我目前卡在删除部分。

问题

我可以很好地删除圆圈,也可以从列表中删除小部件。问题是列表中小部件曾经所在的位置仍然有一个空格,并且由于我使用小部件中的按钮并且没有选择该项目,所以我不确定如何删除该位置。另外,如果我删除一堆然后尝试添加一些Python,它的自身就会崩溃,我不知道为什么。

我不确定是否可以完成我想要的操作,因为确实没有引用,或者我是否最好将按钮移动到主窗口并从自定义小部件中删除它们。如果可能的话,我想保持原样。

有几个文件,所以我会将其放在 GitHub 上,这样我就不会占用大量空间。非常感谢任何帮助。

GitHub 链接

Link to 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)

图片

Picture of python crashing

enter image description here

最佳答案

您必须删除项目小部件和项目本身。为此,需要一种方法来从项目小部件(或其子小部件之一)获取项目。一个干净的方法是使用列表小部件的 itemAt方法,它可以从屏幕上的某个点获取项目。这样做的主要好处是它不需要了解项目的索引,当其他项目被删除时,索引当然会发生变化。这也意味着项目小部件不需要了解与它们关联的特定列表小部件项目的任何信息。

这是对 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()

关于python - PyQt:从 QListWidget 中删除自定义小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47165402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com