PySide:如何触发当前点击的 QPushbutton,而不是其他后来添加的

编程入门 行业动态 更新时间:2024-10-07 23:23:49
本文介绍了PySide:如何触发当前点击的 QPushbutton,而不是其他后来添加的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 PySide 的新手.在我的程序中,遇到了一个问题,当我点击一个按钮时,它会触发后来添加的另一个按钮.谢谢!

I am new to PySide. In my program, I encountered a problem that when I click one button, it triggers other button later added. Thanks!

self.addContentButton = QtGui.QPushButton('Add') self.addContentButton.clicked.connect(self.addContent) def addContent(self): ''' slot to add a row that include a lineedit, combobox, two buttons ''' self.contentTabHBoxWdgt = QtGui.QWidget() self.contentName = QtGui.QLineEdit('line edit') self.conetentTypeBox = QtGui.QComboBox() self.conetentTypeBox.addItem('elem1') self.conetentTypeBox.addItem('elem2') self.contentSave = QtGui.QPushButton('save',parent = self.contentTabHBoxWdgt) self.contentSave.clicked.connect(self.contntSaveAct) self.contentDelete = QtGui.QPushButton('delete',parent=self.contentTabHBoxWdgt) self.contentDelete.clicked.connect(self.contntDel) self.contentTabHBox = QtGui.QHBoxLayout() self.contentTabHBox.addWidget(self.contentName) self.contentTabHBox.addWidget(self.conetentTypeBox) self.contentTabHBox.addWidget(self.contentSave) self.contentTabHBox.addWidget(self.contentDelete) self.contentTabHBoxWdgt.setLayout(self.contentTabHBox) self.contentTabVBox.addWidget(self.contentTabHBoxWdgt) def contntDel(self): ''' slot to delete a row ''' msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Warning, '', 'Be sure to delete') okBttn = msgBox.addButton('Yes', QtGui.QMessageBox.AcceptRole) noBttn = msgBox.addButton('Cancel', QtGui.QMessageBox.RejectRole) ret = msgBox.exec_() if msgBox.clickedButton() == okBttn: self.contentTabVBox.removeWidget(self.contentDelete.parentWidget());

当我添加一行并单击其删除按钮时,它没有按预期工作.当我添加两行或三行时,我单击一个删除按钮,它删除了不是单击的删除按钮所属的一行.我怎么能实现这个功能.这!

When I Add one row and click its delete button, it does not work as expected.While I add two or three row , I click one delete button , it remove one row that is not the clicked delete button belong to. How could I achieve this function. Ths!

推荐答案

你的问题是因为你没有真正正确地利用面向对象编程.

Your problem is because you aren't really taking advantage of object oriented programming properly.

示例中的所有行都调用方法 contntDel 的相同实例.此方法使用 self.contentDelete,它始终包含对添加的最后一行的引用.

All rows in your example call the same instance of the method contntDel. This method uses self.contentDelete which always contains a reference to the last row added.

您需要做的是将与行相关的所有内容分离到一个新类中.添加行时,创建此类的新实例并传入contentTabVBox.这样每一行(或您将编写的新类的实例)都有自己的删除方法.

What you need to do is separate out everything related to a row to a new class. When you add a row, create a new instance of this class and pass in the contentTabVBox. That way each row (or instance of the new class you will write) will have it's own delete method.

没有完整的代码示例,我无法提供完整的解决方案,但这应该可以让您大致了解:

Without a complete code example, I can't provide a complete solution, but this should give you a rough idea:

class MyRow(object): def __init__(self,contentTabVBox, rows): self.contentTabVBox = contentTabVBox self.my_list_of_rows = rows self.addContent() def addContent(self): # The code for your existing addContent method here def contntDel(self): # code from your existing contntDel function here # also add (if Ok button clicked): self.my_list_of_rows.remove(self) class MyExistingClass(??whatever you have here normally??): def __init__(....): self.addContentButton = QtGui.QPushButton('Add') self.addContentButton.clicked.connect(self.addContent) self.my_list_of_rows = [] def addContent(self): my_new_row = MyRow(self.contentTabVBox,self.my_list_of_rows) # You mustsave a reference to my_new_row in a list or else it will get garbage collected. self.my_list_of_rows.append(my_new_row)

希望有帮助!

更多推荐

PySide:如何触发当前点击的 QPushbutton,而不是其他后来添加的

本文发布于:2023-11-28 06:26:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641292.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   PySide   QPushbutton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!