Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMessageBox does not open and throws an error



  • Hi all,

    I am trying to get a dialog box to open and it will not. It throws the following error: TypeError: setWindowTitle(self, str): first argument of unbound method must have type 'QMessageBox'

    This is the code I am trying to run:

    import sys
    import pyodbc
    from datetime import datetime as dt
    from PyQt5.QtCore import Qt
    from PyQt5.QtGui import QPalette
    from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit, QFormLayout, QPushButton, QMessageBox, QLabel
    
    class StartForm(QDialog):
        def __init__(self, parent=None):
            super(StartForm,self).__init__(parent)
            self.setGeometry(50, 50, 150, 150)
            self.setWindowTitle("TERM TEMPLATE DATABASE INSERT")
    
            # Create widgets
            self.myCourseCodeInput = QLineEdit(self)
            self.QCourseCodeLabel = QLabel("Enter the Course Code for new template: ")
            self.myTermCodeInput = QLineEdit(self)
            self.QTermCodeLabel = QLabel("Enter the term code (yymmdd) for the new template: ")
            self.startTermTemplateInsertBtn = QPushButton("PROCESS NEW TERM TEMPLATE")
            self.cancelBtn = QPushButton("EXIT APPLICATION")
    
            # Create layout and add widgets
            layout = QFormLayout()
            layout.addRow(self.QCourseCodeLabel,self.myCourseCodeInput)
            layout.addRow(self.QTermCodeLabel,self.myTermCodeInput)
            layout.addRow(self.startTermTemplateInsertBtn, self.cancelBtn)
    
            # Set dialog layout
            self.setLayout(layout)
            self.myCourseCodeInput.setEnabled(True)
            self.myTermCodeInput.setEnabled(True)
            self.startTermTemplateInsertBtn.setEnabled(True)
    
            # Connect buttons and checkbox to actions
            self.startTermTemplateInsertBtn.clicked.connect(self.termInfoVerify)
    
        def templateData(self, appTerm, cursor):
            cid = self.myCourseCodeInput.text().upper()
            appTerm = self.myTermCodeInput.text()
            termDate = dt.strptime(appTerm, '%y%m%d') #Put term input in python datetime format
            tDate = termDate.strftime('%y%m%d') #Convert python format term date to UMA term code format
            xTermDate = termDate.strftime('%Y-%m-%d') #Expand term date to yyyy-mm-dd format
            termTemplate = 'TEMPLATE_' + cid + '_' + tDate
            self.termInfoVerify()
    
        def termInfoVerify(self):
            cidText = self.myCourseCodeInput.text().upper()
            termDate = self.myTermCodeInput.text()    
            QMessageBox.Question()
            QMessageBox.setWindowTitle(self, "Template Info Verification")
            QMessageBox.setText(self, "You entered the following term template information:")
            QMessageBox.setDetailedText(self, "Course Code:" + cidText)
            QMessageBox.setDetailedText(self, "Term Date: " + termDate)
            QMessageBox.setInformativeText(self, "Click 'OK' to process the information.  Click 'Cancel' to make corrections.")
            QMessageBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
    
    
    # Create the Qt Application
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    palette = QPalette()
    palette.setColor(QPalette.ButtonText, Qt.blue)
    app.setPalette(palette)
    
    # Create and show the form
    form = StartForm()
    form.show()
    
    # Run the main Qt loop
    sys.exit(app.exec())
    

    Any help is greatly appreciated!


  • Lifetime Qt Champion

    @PyGrrl_FL said in QMessageBox does not open and throws an error:

    setWindowTitle

    This is not a static method! question() is static.
    You need to create a QMessageBox instance and then show it using exec():

    msg = QMessageBox()
    msg.setWindowTitle(self, "Template Info Verification")
    msg.setText(self, "You entered the following term template information:")
    msg.setDetailedText(self, "Course Code:" + cidText)
    msg.setDetailedText(self, "Term Date: " + termDate)
    msg.setInformativeText(self, "Click 'OK' to process the information.  Click 'Cancel' to make corrections.")
    msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
    msg.exec()
    

    If you want to use static question() method then please check its documentation and pass the parameters in the correct way:

    QMessageBox.question(self, "Template Info Verification", ...)
    

  • Lifetime Qt Champion

    @PyGrrl_FL said in QMessageBox does not open and throws an error:

    setWindowTitle

    This is not a static method! question() is static.
    You need to create a QMessageBox instance and then show it using exec():

    msg = QMessageBox()
    msg.setWindowTitle(self, "Template Info Verification")
    msg.setText(self, "You entered the following term template information:")
    msg.setDetailedText(self, "Course Code:" + cidText)
    msg.setDetailedText(self, "Term Date: " + termDate)
    msg.setInformativeText(self, "Click 'OK' to process the information.  Click 'Cancel' to make corrections.")
    msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
    msg.exec()
    

    If you want to use static question() method then please check its documentation and pass the parameters in the correct way:

    QMessageBox.question(self, "Template Info Verification", ...)
    


  • Thanks, jsulm! I see now where my mistake is. I was mixing things around. I was able to get it working with your guidance.


  • Lifetime Qt Champion

    Hi @PyGrrl_FL,

    Glad you solved it. So please mark this topic SOLVED too. Thanks!


Log in to reply