2012-04-13 88 views
0

我有一组按钮,确定和取消如何自定义pyQT中的对话框信号/插槽?

buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok| 
            QtGui.QDialogButtonBox.Cancel) 

我想要一个对话提示,当我们点击Cancel

self.connect(buttonBox, SIGNAL("rejected()"), 
          self, SLOT("reject()")) 
    def reject(self): 
     print 'hello' 
     self.emit(SIGNAL("reject()")) 

我不知道发出什么。我不想只关闭这个东西。当我按X时,我知道如何创建QMessageBox。我想在reject中做提示并关闭。

我希望它是有道理的。谢谢。


为了您的信息,当我按下X关闭整个应用程序,我有一个覆盖方法

def closeEvent(self, event): 
    reply = QtGui.QMessageBox.question(self, 'Message', 'Are you sure to quit?', QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No) 
    if reply == QtGui.QMessageBox.Yes: 
     event.accept() 
    else: 
     event.ignore() 

这种覆盖self.close()方法。

回答

1

你不会发射任何东西。 QDialog有一个reject()插槽,它将返回码设置为Rejected并关闭对话框。你需要调用它。您也将自定义插槽命名为reject,因此覆盖它。你还可以把它想:

super(NameOfClass, self).reject() 

或更改插槽名称到别的和使用:在那里

self.reject() 

0

你可能想覆盖你的QDialog类的accept()函数。

例如:

def accept(self): 
    if your_validation_userconfirmation_fct(): 
     self.done(QtWidgets.QDialog.Accepted)