简体   繁体   English

单击 PyQt5 对话框按钮框中的特定按钮后如何执行 function?

[英]How to execute a function after click particular button in PyQt5 Dialog Button Box?

Here is my Dialog Button Box design on Python:这是我在 Python 上设计的对话框按钮框:

    self.buttonBox = QtWidgets.QDialogButtonBox(Frame)
    self.buttonBox.setGeometry(QtCore.QRect(200, 216, 144, 27))
    self.buttonBox.setFont(font)
    self.buttonBox.setAcceptDrops(False)
    self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Discard|QtWidgets.QDialogButtonBox.Save)
    self.buttonBox.setObjectName("buttonBox")

There are two buttons as shown above, which are Discard and Save .如上图有两个按钮,分别是DiscardSave How to execute a particular unique function when I click Save or Discard .单击SaveDiscard时如何执行特定的唯一 function 。 What I have tried is:我试过的是:

    self.buttonBox.accepted.connect(self.save)
    self.buttonBox.rejected.connect(self.discard)

However, the function execute only when I clicked Save and did not work when I clicked Discard .但是,function 仅在我单击Save时执行,而在我单击Discard时不起作用。 How to handle this?如何处理?

Discard is a destructive button, not a reject button. Discarddestructive按钮,而不是reject按钮。 You could either:你可以:

  • Use Cancel , Close , or Abort and link to the rejected signal as you are already doing使用CancelCloseAbort并链接到被rejected的信号,就像您已经在做的那样
  • If you need to use Discard , connect to the clicked signal and just verify in your self.discard function that the clicked button (passed with the signal) was the Discard button before doing anything如果您需要使用Discard ,请连接到clicked信号,然后在您的self.discard function 中验证 clicked 按钮(随信号传递)是Discard按钮,然后再做任何事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM