3

I'm using MessageBox to get user input if data is to be added to database, but I don't know how to place my variables inside the actual message. MessageBox function looks like this:

def message(self, par_1, par_2):
    odp = QMessageBox.question(self, 'Information', "No entry. Would you like to add it to DB?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
    if odp == QMessageBox.Yes:
        return True
    else:
        return False

Function is called like this:

self.message(autor_firstname, autor_lastname)

I tried adding:

odp.setDetailText(par_1, par_2)

But it didn't work as expected. Additionally I have problem when user clicks "No". Program crashes instead of returning to main window.

HyperQBE
  • 67
  • 10

2 Answers2

1

As per the docs on QMessageBox::question, the return value is the button that was clicked. Using the static method QMessageBox::question limits how you can customize the QMessageBox. Instead, create an instance of QMessageBox explicitly, call setText, setInformativeText, and setDetailedText as needed. Note that your arguments also do not match what is needed for setDetailedText. Those docs are here. I think your code should look something like this.

def message(self, par_1, par_2):

    # Create the dialog without running it yet
    msgBox = QMessageBox()

    # Set the various texts
    msgBox.setWindowTitle("Information")
    msgBox.setText("No entry. Would you like to add it to the database")
    detail = "%s\n%s" % (par_1, par_2) # formats the string with one par_ per line.
    msgBox.setDetailedText(detail) 

    # Add buttons and set default
    msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
    msgBox.setDefaultButton(QMessageBox.No)     

    # Run the dialog, and check results   
    bttn = msgBox.exec_()
    if bttn == QMessageBox.Yes:
        return True
    else:
        return False
buck54321
  • 847
  • 9
  • 21
  • That makes sense, I've added this code but program crashes. message function is called but program exits with Process finished with exit code -1073740791 (0xC0000409) – HyperQBE May 27 '18 at 09:05
  • 1
    I found that program was crashing at: `msgBox.addButton(QMessageBox.Yes | QMessageBox.No)` I've changed it to: `msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)` and it started working. – HyperQBE May 27 '18 at 09:29
  • @HyperQBE Glad it worked. I'll update the answer to reflect your comment. Sorry about the error. I should have mentioned that it wasn't tested. – buck54321 May 27 '18 at 20:13
0

This can also solve your problem:

def message(self, par_1, par_2):
    # Create the dialog without running it yet
    msgBox = QMessageBox()

    # Set the various texts
    msgBox.setWindowTitle("Information")
    msgBox.setText("No entry for '"+str(par_1)+" "+str(par_2)+"'.Would you like to add it to the database")
    

   # Add buttons and set default
   msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
   msgBox.setDefaultButton(QMessageBox.No)     

   # Run the dialog, and check results   
   bttn = msgBox.exec_()
   if bttn == QMessageBox.Yes:
      return True
   else:
      return False

Not the use of string concatenation on line 6.

kelvinmacharia254
  • 117
  • 1
  • 3
  • 12