- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将图片加载到QGraphicsScene
中,然后当您单击照片时,它会在单击的区域中放置圆圈。然后我添加一个自定义小部件,它将保留点列表。这个小部件有几个按钮。其中一个可以移动圆圈,另一个可以删除它。我目前卡在删除部分。
问题
我可以很好地删除圆圈,也可以从列表中删除小部件。问题是列表中小部件曾经所在的位置仍然有一个空格,并且由于我使用小部件中的按钮并且没有选择该项目,所以我不确定如何删除该位置。另外,如果我删除一堆然后尝试添加一些Python,它的自身就会崩溃,我不知道为什么。
我不确定是否可以完成我想要的操作,因为确实没有引用,或者我是否最好将按钮移动到主窗口并从自定义小部件中删除它们。如果可能的话,我想保持原样。
有几个文件,所以我会将其放在 GitHub 上,这样我就不会占用大量空间。非常感谢任何帮助。
GitHub 链接
相关代码(来自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)
图片
最佳答案
您必须删除项目小部件和项目本身。为此,需要一种方法来从项目小部件(或其子小部件之一)获取项目。一个干净的方法是使用列表小部件的 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/
我正在通过单击 ui 上的按钮更改 QListWidgetItem 标题(QListWidgetItem 文本),在此之下我还调用了 repaint() 和 update(),但标题不会更改(刷新),
我有课 class MyClass { public: QString name; int age; }; ... MyClass* itm1= new MyClass(); itm1
我的应用程序 (Qt 4.6) 要求我在列表中显示某些消息,该列表在添加新行时总是向下滚动(因此最近的行始终可见)。 由于我在包括显示这些单行消息在内的整个过程中遇到性能问题,所以我运行了一些测试并发
我有一个 QListWidget,里面有一些小部件: QListWidgetItem* w = new QListWidgetItem(ui->listWidget); ui->listWidget-
是否可以根据其内容调整 QListWidget 的高度和宽度? sizeHint() 始终返回 256, 192,无论其内容是什么。 QListWidgetItem 的 sizeHint() 返回 -
我用一些项目创建了 QListWidget。 QHBoxLayout* h_layout = new QHBoxLayout; h_layout->setMargin( 0 ); me
我正在 Qt 中创建一个可排序列表。该代码对于向下滚动效果非常好,但是当我在向下滚动列表后获取可拖动项目时遇到一些问题。我添加了一些测试用例屏幕截图以便更好地理解 这是测试用例代码 #incl
我是 Qt 框架的新手。在新应用程序中,我想创建包含自定义项目的列表。这些项目非常简单,必须包含标题标签、缩略图和描述标签 (picture here) 现在我不想玩自定义绘图和所有这些东西,因为我认
我创建了一个自定义 QListWidget,其中添加了自定义列表小部件(在本例中只是 QPushButton 小部件),所有设置都添加到它的 QListWidgetItems 中,以便我可以从 QPu
我有一个带有一系列 QListWidget 的 GUI,第一个 QListWidget 应该有文本项和图标项,图标的标签位于侧面。其他 QListWidget 的项目应与第一个 QListWidget
我在 PySide2 中有一个 QListWidget,我希望所选项目具有不同的颜色。这可能吗?我将附上两张图片来演示。 我目前拥有的: 这就是我想要的: 最佳答案 您必须使用委托(delegate)
我想创建一个包含许多这些项目的 QListView 或 QListWidget。我研究过创建委托(delegate)并使用 setItemWidget 来设置小部件。我在这两方面都遇到困难。 下面是在
我需要在“.ui”文件中的 QListWidget 提出的不同选项中包含文本和希腊符号(用于简单的数学方程)(我以前使用 Qt Designer,如果重要的话我喜欢 python)。我希望选项显示如下
删除QListWidget中的一项后,我在更新它时遇到了一些问题。 这是代码的一部分: class Window(QMainWindow): list_1 = [] #The items are
我想写一个“无限滚动”的列表小部件。滚动应该根据计时器自动发生。不会有任何用户交互。例如,该列表包含项目 0-100。如果滚动区域达到第 100 项,则显示的下一个项目应该是第 1 项,依此类推(如滚
在 Qt 4.6.1 上,当我连接来自 QListWidget 的 itemChanged 信号时,我没有看到每次编辑时的连续发射行为,如中所述 Qt - signal for when QListW
我问这个问题是因为我不知道如何用谷歌搜索它,我找不到合适的关键字。 我有一个包含字符串的QListWidget。字符串很长,如果我禁用水平滚动,这正是我想要的,那么文本以...结尾,因为它太长了。 我
在我的 QMainWindow 构造函数中,我读取了一个数据库并用项目填充了我的 QListWidget。显然没有选择任何项目,所以我必须自己做。我还有一个插槽,当我单击列表中的项目时将调用它。 我试
我有一个包含多个项目的 QListWidget,每个项目都像一个按钮,在单击时做出响应。我遇到了一个问题,如果您单击一个项目并将鼠标拖动到屏幕上除您单击的项目之外的任何其他位置,那么程序将导致段错误并
如何使我的 QListWidget 高度与我的主窗口相同? 因此,如果我更改主窗口,它必须调整大小,如果无法在小部件中查看所有列表,则使滚动条可见,等等。 最佳答案 Qt 有一个叫做 layouts
我是一名优秀的程序员,十分优秀!