Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom button to a QMessageBox in PyQt4

Tags:

python

pyqt

I am coding a application which needs a custom buttons in QMessageBox. i managed to create an example in QT designer which is given below.

enter image description here

i wanted to do this in a QMessageBox.

I am using python 2.6.4 and PyQt4. please, can any one help.

like image 807
Sudeepth Patinjarayil Avatar asked Mar 28 '13 13:03

Sudeepth Patinjarayil


1 Answers

manuel-gutierrez, why do you inherit from QDilaog? You can inherit from QMessageBox. It's much simpler and less code

import sys
from PyQt4.QtGui import QMessageBox, QPushButton, QApplication
from PyQt4.QtCore import Qt

class ErrorWindow(QMessageBox):
    def __init__(self, parent=None):
        QMessageBox.__init__(self, parent)
        self.setWindowTitle("Example")

        self.addButton(QPushButton("Yes"), QMessageBox.YesRole )
        self.addButton(QPushButton("No"), QMessageBox.NoRole)
        self.addButton(QPushButton("Cancel"), QMessageBox.RejectRole)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = ErrorWindow()
    ex.setText("some error")
    ex.show()

    sys.exit(app.exec_())
like image 84
Дмитрий Моцик Avatar answered Oct 26 '22 19:10

Дмитрий Моцик