Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Repeated triggering of the closeEvent phenomenon
Qt 6.11 is out! See what's new in the release blog

Repeated triggering of the closeEvent phenomenon

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 469 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TSummerDay
    wrote on last edited by
    #1
    import sys
    
    from PyQt6.QtCore import QRect
    from PyQt6 import  QtCore
    from PyQt6.QtGui import QIcon
    from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget
    from PyQt6.QtCore import Qt
    from PyQt6.QtWidgets import QMessageBox
    from PyQt6 import QtGui
    
    class Utils:
        @staticmethod
        def popup_message(title, message, buttons=None, icon=QMessageBox.Icon.Critical, left_btn=None, right_btn=None,
                          parent_window=None):
            msg = QMessageBox()
            msg.setWindowTitle(title)
            msg.setText(message)
            msg.setStyleSheet("QLabel { color: rgba(34, 34, 34, 255);}")
            msg.setIcon(icon)
            msg.setWindowFlag(QtCore.Qt.WindowType.Dialog)
            if parent_window is not None:
                width = len(message) * 20 // 2 if len(message) < 30 else (30 * 20 // 2)
                pos_x = parent_window.x() + parent_window.width() // 2 - width
                pos_y = parent_window.y() + parent_window.height() // 2 - 100
                msg.move(pos_x, pos_y)
            if left_btn is not None and right_btn is not None:
                msg.addButton(left_btn, QMessageBox.ButtonRole.AcceptRole)
                msg.addButton(right_btn, QMessageBox.ButtonRole.AcceptRole)
                ret = msg.exec()
                return ret
            else:
                msg.setStandardButtons(buttons)
                ret = msg.exec()
                return QMessageBox.StandardButton(ret)
    
    class NavigationWindow(QMainWindow):
        def __init__(self):
            super(NavigationWindow, self).__init__()
            self.setup_window()
    
        def setup_window(self):
            # 创建一个1*1,任务栏不可见的窗口
            self.setFixedSize(1, 1)
            self.setWindowFlags(Qt.WindowType.Tool)
    
        def closeEvent(self, ev: QtGui.QCloseEvent) -> None:
            print("closeEvent")
            ev.ignore()
            ret = Utils.popup_message(title="退出", message='@NavigationWindow.exit_prompt',
                                      buttons=QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.No,
                                      icon=QMessageBox.Icon.Question,
                                      parent_window=self)
            if ret == QMessageBox.StandardButton.No:
                return None
            ev.accept()
            super().closeEvent(ev)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
    
        window = NavigationWindow()
        window.show()
        msg = QMessageBox()
        title = "提示"
        message = "是否唤起完整功能界面?"
        buttons = QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.No
        icon = QMessageBox.Icon.Question
        msg.setWindowTitle(title)
        msg.setText(message)
        msg.setStyleSheet("QLabel { color: rgba(34, 34, 34, 255);}")
        msg.setIcon(icon)
        msg.setWindowFlag(QtCore.Qt.WindowType.Dialog)
        msg.setStandardButtons(buttons)
        ret = msg.exec()
        # Start the event loop.
        window_flags = window.windowFlags()
        window_flags &= ~Qt.WindowType.Tool
        window.setWindowFlags(window_flags)
        WINDOW_WIDTH = 1440
        WINDOW_HEIGHT = 906
        window.setFixedSize(1440, 906)
        window.show()
        app.exec()
    
    

    1.Create a subclass NavigationWindow of QMainWindow: In the initialization, set it as a tool window.
    2.Create a NavigationWindow window: Instantiate the NavigationWindow class.
    3.Display a QMessageBox: Prompt whether to display the window.
    4.Set NavigationWindow as a non-tool window: Change the window flags of the NavigationWindow instance so that it is no longer a tool window.
    5.Close the NavigationWindow and display a QMessageBox: Show a message box when the NavigationWindow is closed.
    6.Choose 'No', the popup disappears, and the closeEvent of NavigationWindow is triggered again: If 'No' is selected in the message box, the closeEvent of NavigationWindow continues to be triggered.
    7.Repeat Step 6 in a loop: Continuously repeat the action in Step 6.

    jsulmJ 1 Reply Last reply
    0
    • T TSummerDay
      import sys
      
      from PyQt6.QtCore import QRect
      from PyQt6 import  QtCore
      from PyQt6.QtGui import QIcon
      from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget
      from PyQt6.QtCore import Qt
      from PyQt6.QtWidgets import QMessageBox
      from PyQt6 import QtGui
      
      class Utils:
          @staticmethod
          def popup_message(title, message, buttons=None, icon=QMessageBox.Icon.Critical, left_btn=None, right_btn=None,
                            parent_window=None):
              msg = QMessageBox()
              msg.setWindowTitle(title)
              msg.setText(message)
              msg.setStyleSheet("QLabel { color: rgba(34, 34, 34, 255);}")
              msg.setIcon(icon)
              msg.setWindowFlag(QtCore.Qt.WindowType.Dialog)
              if parent_window is not None:
                  width = len(message) * 20 // 2 if len(message) < 30 else (30 * 20 // 2)
                  pos_x = parent_window.x() + parent_window.width() // 2 - width
                  pos_y = parent_window.y() + parent_window.height() // 2 - 100
                  msg.move(pos_x, pos_y)
              if left_btn is not None and right_btn is not None:
                  msg.addButton(left_btn, QMessageBox.ButtonRole.AcceptRole)
                  msg.addButton(right_btn, QMessageBox.ButtonRole.AcceptRole)
                  ret = msg.exec()
                  return ret
              else:
                  msg.setStandardButtons(buttons)
                  ret = msg.exec()
                  return QMessageBox.StandardButton(ret)
      
      class NavigationWindow(QMainWindow):
          def __init__(self):
              super(NavigationWindow, self).__init__()
              self.setup_window()
      
          def setup_window(self):
              # 创建一个1*1,任务栏不可见的窗口
              self.setFixedSize(1, 1)
              self.setWindowFlags(Qt.WindowType.Tool)
      
          def closeEvent(self, ev: QtGui.QCloseEvent) -> None:
              print("closeEvent")
              ev.ignore()
              ret = Utils.popup_message(title="退出", message='@NavigationWindow.exit_prompt',
                                        buttons=QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.No,
                                        icon=QMessageBox.Icon.Question,
                                        parent_window=self)
              if ret == QMessageBox.StandardButton.No:
                  return None
              ev.accept()
              super().closeEvent(ev)
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
      
          window = NavigationWindow()
          window.show()
          msg = QMessageBox()
          title = "提示"
          message = "是否唤起完整功能界面?"
          buttons = QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.No
          icon = QMessageBox.Icon.Question
          msg.setWindowTitle(title)
          msg.setText(message)
          msg.setStyleSheet("QLabel { color: rgba(34, 34, 34, 255);}")
          msg.setIcon(icon)
          msg.setWindowFlag(QtCore.Qt.WindowType.Dialog)
          msg.setStandardButtons(buttons)
          ret = msg.exec()
          # Start the event loop.
          window_flags = window.windowFlags()
          window_flags &= ~Qt.WindowType.Tool
          window.setWindowFlags(window_flags)
          WINDOW_WIDTH = 1440
          WINDOW_HEIGHT = 906
          window.setFixedSize(1440, 906)
          window.show()
          app.exec()
      
      

      1.Create a subclass NavigationWindow of QMainWindow: In the initialization, set it as a tool window.
      2.Create a NavigationWindow window: Instantiate the NavigationWindow class.
      3.Display a QMessageBox: Prompt whether to display the window.
      4.Set NavigationWindow as a non-tool window: Change the window flags of the NavigationWindow instance so that it is no longer a tool window.
      5.Close the NavigationWindow and display a QMessageBox: Show a message box when the NavigationWindow is closed.
      6.Choose 'No', the popup disappears, and the closeEvent of NavigationWindow is triggered again: If 'No' is selected in the message box, the closeEvent of NavigationWindow continues to be triggered.
      7.Repeat Step 6 in a loop: Continuously repeat the action in Step 6.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @TSummerDay said in Repeated triggering of the closeEvent phenomenon:

      super().closeEvent(ev)

      You do not call this if No is selected

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved