class MessageCheckBox(QMessageBox): """ A QMessageBox derived widget that includes a QCheckBox aligned to the right under the message and on top of the buttons. """ def __init__(self, *args, **kwargs): super(MessageCheckBox, self).__init__(*args, **kwargs) self.setWindowModality(Qt.NonModal) self._checkbox = QCheckBox(self) # Set layout to include checkbox size = 9 check_layout = QVBoxLayout() check_layout.addItem(QSpacerItem(size, size)) check_layout.addWidget(self._checkbox, 0, Qt.AlignRight) check_layout.addItem(QSpacerItem(size, size)) # Access the Layout of the MessageBox to add the Checkbox layout = self.layout() if PYQT5: layout.addLayout(check_layout, 1, 2) else: layout.addLayout(check_layout, 1, 1) # --- Public API # Methods to access the checkbox def is_checked(self): return self._checkbox.isChecked() def set_checked(self, value): return self._checkbox.setChecked(value) def set_check_visible(self, value): self._checkbox.setVisible(value) def is_check_visible(self): self._checkbox.isVisible() def checkbox_text(self): self._checkbox.text() def set_checkbox_text(self, text): self._checkbox.setText(text)
class MessageCheckBox(QMessageBox): """ A QMessageBox derived widget that includes a QCheckBox aligned to the right under the message and on top of the buttons. """ def __init__(self, *args, **kwargs): super(MessageCheckBox, self).__init__(*args, **kwargs) self._checkbox = QCheckBox() # Set layout to include checkbox size = 9 check_layout = QVBoxLayout() check_layout.addItem(QSpacerItem(size, size)) check_layout.addWidget(self._checkbox, 0, Qt.AlignRight) check_layout.addItem(QSpacerItem(size, size)) # Access the Layout of the MessageBox to add the Checkbox layout = self.layout() if PYQT5: layout.addLayout(check_layout, 1, 2) else: layout.addLayout(check_layout, 1, 1) # --- Public API # Methods to access the checkbox def is_checked(self): return self._checkbox.isChecked() def set_checked(self, value): return self._checkbox.setChecked(value) def set_check_visible(self, value): self._checkbox.setVisible(value) def is_check_visible(self): self._checkbox.isVisible() def checkbox_text(self): self._checkbox.text() def set_checkbox_text(self, text): self._checkbox.setText(text)
class MessageCheckBox(QMessageBox): """ A QMessageBox derived widget that includes a QCheckBox aligned to the right under the message and on top of the buttons. """ def __init__(self, *args, **kwargs): super(MessageCheckBox, self).__init__(*args, **kwargs) self._checkbox = QCheckBox() # Set layout to include checkbox size = 9 check_layout = QVBoxLayout() check_layout_h = QHBoxLayout() check_layout.addItem(QSpacerItem(size, size)) check_layout_h.addStretch() check_layout_h.addWidget(self._checkbox, 1, Qt.AlignRight) check_layout.addLayout(check_layout_h) check_layout.addItem(QSpacerItem(size, size)) # Access the Layout of the MessageBox to add the Checkbox layout = self.layout() if PYQT4: grid_position = 1 else: grid_position = 2 layout.addLayout(check_layout, 1, grid_position) # --- Public API # Methods to access the checkbox def is_checked(self): return self._checkbox.isChecked() def set_checked(self, value): return self._checkbox.setChecked(value) def set_check_visible(self, value): self._checkbox.setVisible(value) def is_check_visible(self): self._checkbox.isVisible() def checkbox_text(self): self._checkbox.text() def set_checkbox_text(self, text): self._checkbox.setText(text) @staticmethod def _boxcreator(parent, title, text, buttons=QMessageBox.Ok, defaultButton=QMessageBox.NoButton, checkbox_text='', icon=None): widget = MessageCheckBox(icon, title, text, buttons=buttons, parent=parent) widget.set_checkbox_text(checkbox_text) return widget @staticmethod def warning(*args, **kwargs): kwargs['icon'] = QMessageBox.Warning return MessageCheckBox._boxcreator(*args, **kwargs) @staticmethod def critical(*args, **kwargs): kwargs['icon'] = QMessageBox.Critical return MessageCheckBox._boxcreator(*args, **kwargs) @staticmethod def information(*args, **kwargs): kwargs['icon'] = QMessageBox.Information return MessageCheckBox._boxcreator(*args, **kwargs) @staticmethod def question(*args, **kwargs): kwargs['icon'] = QMessageBox.Question return MessageCheckBox._boxcreator(*args, **kwargs)