Is there a way to make a resizable QMessageBox in PyQt? How can I do this?
If I write this code:
mbox = QMessageBox()
mbox.setText('tesdt')
mbox.setTitle('test')
mbox.exec()
The messagebox cannot be resized. How can I make it resizable?
Is there a way to make a resizable QMessageBox in PyQt? How can I do this?
If I write this code:
mbox = QMessageBox()
mbox.setText('tesdt')
mbox.setTitle('test')
mbox.exec()
The messagebox cannot be resized. How can I make it resizable?
You can create an inherited class from QMessageBox() and modify it to be resizable like I do in this code:
import sys
from PyQt5.QtWidgets import (
QApplication,
QPushButton,
QWidget,
QMessageBox,
QVBoxLayout,
QTextEdit,
QSizePolicy
)
class MyMessageBox(QMessageBox):
def __init__(self):
QMessageBox.__init__(self)
self.setSizeGripEnabled(True)
def event(self, e):
result = QMessageBox.event(self, e)
self.setMinimumHeight(0)
self.setMaximumHeight(16777215)
self.setMinimumWidth(0)
self.setMaximumWidth(16777215)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
textEdit = self.findChild(QTextEdit)
if textEdit != None :
textEdit.setMinimumHeight(0)
textEdit.setMaximumHeight(16777215)
textEdit.setMinimumWidth(0)
textEdit.setMaximumWidth(16777215)
textEdit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
return result
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QGridLayout Example")
self.button = QPushButton("Show message")
self.layout2 = QVBoxLayout()
self.layout2.addWidget(self.button)
self.setLayout(self.layout2)
self.button.clicked.connect(self.show_message)
def show_message(self):
self.mbox = MyMessageBox()
self.mbox.setIcon(MyMessageBox.Information)
self.mbox.setStandardButtons(MyMessageBox.Ok)
self.mbox.setText('test')
self.mbox.setWindowTitle('test')
self.mbox.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())