Skip to content

Instantly share code, notes, and snippets.

@cassolmc
Last active April 25, 2016 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassolmc/01931db16fcbe6f5694a8e5610893303 to your computer and use it in GitHub Desktop.
Save cassolmc/01931db16fcbe6f5694a8e5610893303 to your computer and use it in GitHub Desktop.
QMessagebox with Checkbox
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class DialogWithCheckBox(QMessageBox):
def __init__(self, parent= None):
super(DialogWithCheckBox, self).__init__(parent)
self.checkbox = QCheckBox()
#Access the Layout of the MessageBox to add the Checkbox
layout = self.layout()
layout.addWidget(self.checkbox, 1,2)
def exec_(self, *args, **kwargs):
"""
Override the exec_ method so you can return the value of the checkbox
"""
return QMessageBox.exec_(self, *args, **kwargs), self.checkbox.isChecked()
class ExampleCheckbox(DialogWithCheckBox):
def __init__(self, parent=None):
super(ExampleCheckbox, self).__init__()
self.setWindowTitle("Dialog with CheckBox")
self.setText("Isn't this checkbox beautiful?")
self.checkbox.setText("Do not ask again")
self.setStandardButtons(QMessageBox.Cancel |QMessageBox.Ok)
self.setDefaultButton(QMessageBox.Cancel)
self.setIcon(QMessageBox.Warning)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = ExampleCheckbox()
answer = dialog.exec_()
# Example how to handle the response
if answer[1] == True:
if answer[0] == int(QMessageBox.Ok):
print ("Ok button pressed")
print ("handle checkbox has been activated")
else:
print ("handle checkbox not activated")
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment