PyQt:如何使用QMessageBox.open()和连接回调?

huangapple go评论66阅读模式
英文:

PyQt: How to use QMessageBox.open() and connect callback?

问题

import sys
from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QPushButton, QMessageBox
)

def show_dialog(app, window):
    mbox = QMessageBox(window)
    mbox.setIcon(QMessageBox.Icon.Information)
    mbox.setText("Message box pop up window")
    mbox.setWindowTitle("QMessageBox Example")
    mbox.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
    def button_clicked():
        print("message box button clicked")
        mbox.done(0)
        app.quit()
    # How to connect button_clicked() to the standard buttons of the message box?
    mbox.open(button_clicked, button_clicked) # Use button_clicked for both receiver and member

def main():
    app = QApplication(sys.argv)
    window = QMainWindow()
    window.resize(330, 200)
    window.setWindowTitle("Testing")
    button = QPushButton(window)
    button.setText("Button1")
    button.setGeometry(100,100,100,30)
    def button_clicked():
        print("button clicked")
        show_dialog(app, window)
    button.clicked.connect(button_clicked)
    window.show()
    app.exec()

if __name__ == '__main__':
    main()
英文:

How can I connect a callback when using QMessageBox.open() ? In the documentation it says:

> QMessageBox.open(receiver, member)
Opens the dialog and connects its finished() or buttonClicked() signal to the slot specified by receiver and member . If the slot in member has a pointer for its first parameter the connection is to buttonClicked() , otherwise the connection is to finished() .

What should I use for receiver and member in the below example:

import sys
from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QPushButton, QMessageBox
)

def show_dialog(app, window):
   mbox = QMessageBox(window)
   mbox.setIcon(QMessageBox.Icon.Information)
   mbox.setText("Message box pop up window")
   mbox.setWindowTitle("QMessageBox Example")
   mbox.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   def button_clicked():
       print("message box button clicked")
       mbox.done(0)
       app.quit()
   # How to connect button_clicked() to the standard buttons of the message box?
   mbox.open(receiver, member) # What to use for receiver and member?

def main():
    app = QApplication(sys.argv)
    window = QMainWindow()
    window.resize(330, 200)
    window.setWindowTitle("Testing")
    button = QPushButton(window)
    button.setText("Button1")
    button.setGeometry(100,100,100,30)
    def button_clicked():
        print("button clicked")
        show_dialog(app, window)
    button.clicked.connect(button_clicked)
    window.show()
    app.exec()

if __name__ == '__main__':
    main()

答案1

得分: 0

After some trial and error I found that I could simply use mbox.open(button_clicked) but I am still not sure how I can determine from within the callback which of the two buttons was clicked. Also one should not call QMessageBox.done() from within the callback since that would lead to infinite recursion. Here is how I call QMessageBox.open() which works, but I am still missing the last piece of the puzzle: How to determine which button was clicked?

def show_dialog(app, window):
   mbox = QMessageBox(window)
   mbox.setIcon(QMessageBox.Icon.Information)
   mbox.setText("Message box pop up window")
   mbox.setWindowTitle("QMessageBox Example")
   mbox.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)

   #@pyqtSlot() # It is not necessary to use this decorator?
   def button_clicked():
       print("message box button clicked") # But which button?
       #mbox.done(0)  # Do not call this method as it will lead to infinite recursion
       app.quit()
   mbox.open(button_clicked)
   print("Dialog opened")

Update:

How to determine which button was clicked?

You can use mbox.clickedButton().text(), for example like this:

def button_clicked():
    if mbox.clickedButton().text() == "&OK":
        print("OK clicked")
    else:
        print("Cancel clicked")    
    app.quit()
英文:

After some trial and error I found that I could simply use mbox.open(button_clicked) but I am still not sure how I can determine from within the callback which of the two buttons was clicked. Also one should not call QMessageBox.done() from within the callback since that would lead to infinite recursion. Here is how I call QMessageBox.open() which works, but I am still missing the last piece of the puzzle: How to determine which button was clicked?

def show_dialog(app, window):
   mbox = QMessageBox(window)
   mbox.setIcon(QMessageBox.Icon.Information)
   mbox.setText("Message box pop up window")
   mbox.setWindowTitle("QMessageBox Example")
   mbox.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   
   #@pyqtSlot() # It is not necessary to use this decorator?
   def button_clicked():
       print("message box button clicked") # But which button?
       #mbox.done(0)  # Do not call this method as it will lead to infinite recursion
       app.quit()
   mbox.open(button_clicked)
   print("Dialog opened")

Update:

> How to determine which button was clicked?

You can use mbox.clickedButton().text(), for example like this:

def button_clicked():
    if mbox.clickedButton().text() == "&OK":
        print("OK clicked")
    else:
        print("Cancel clicked")    
    app.quit()

huangapple
  • 本文由 发表于 2023年7月23日 16:26:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定