惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
News | PayPal Newsroom
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
博客园 - Franky
SecWiki News
SecWiki News
Recent Announcements
Recent Announcements
T
Troy Hunt's Blog
The Register - Security
The Register - Security
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
S
Security Affairs
博客园 - 司徒正美
S
Schneier on Security
I
InfoQ
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Forbes - Security
Forbes - Security
腾讯CDC
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
V
Vulnerabilities – Threatpost
C
Check Point Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
Schneier on Security
Schneier on Security
O
OpenAI News
K
Kaspersky official blog

WMI

How to Activate MySQL for Multiple Devices - WMI ESLint Flat Config for JS, TS, React, and Prettier - WMI Yarn NPM Registry Configuration - WMI Yarn Guides - WMI Set Up ADB Wireless Debugging on Android: A Full Guide - WMI How to Fill Tax Information on Google Adsense - WMI Migrate ESLint v9 for prettier typescript javascript - WMI PySide6 autocomplete input text - WMI Mobile Legends To The Stars Event Clue - WMI [PHP] generate random proxy IP:PORT from CIDR - WMI [PHP] generate big text file for testing purpose - WMI List of Chrome Driver command line arguments - WMI Happy eid mubarak - WMI Install markdown engine on vite ESM typescript - WMI Android Activity lifecycle - WMI OkHttp cookie handling on android (webview supported) - WMI Turn git log history into markdown - WMI enable automatic memory heap resizing of android studio - WMI is defining screen density can reduce build time ? - WMI
PySide6 button click open new window - WMI
Dimas Lanjaka · 2024-08-24 · via WMI

How to create pyside6 on button click open another window class, when main window closed close other opened windows.

To achieve this in PySide6, you can connect the main window's closeEvent to a method that closes all other opened windows. Here's an example:

  1. Main Window: The primary window with the button that opens a new window.
  2. Secondary Window: The window that opens when the button is clicked.

Here's a code example to demonstrate this:

from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget, QVBoxLayout
import sys

class SecondaryWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Secondary Window")
        self.setGeometry(100, 100, 300, 200)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Main Window")
        self.setGeometry(100, 100, 400, 300)

        self.opened_windows = []

        self.button = QPushButton("Open Secondary Window", self)
        self.button.clicked.connect(self.open_secondary_window)

        layout = QVBoxLayout()
        layout.addWidget(self.button)

        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

    def open_secondary_window(self):
        new_window = SecondaryWindow()
        new_window.show()
        self.opened_windows.append(new_window)

    def closeEvent(self, event):
        # Close all opened windows when the main window is closed
        for window in self.opened_windows:
            window.close()
        event.accept()

if __name__ == "__main__":
    app = QApplication(sys.argv)

    main_window = MainWindow()
    main_window.show()

    sys.exit(app.exec())

Explanation:

  1. SecondaryWindow Class: This represents the additional windows that will be opened when the button is clicked.
  2. MainWindow Class:
    • The open_secondary_window method creates and shows a new SecondaryWindow and keeps track of it in the opened_windows list.
    • The closeEvent method is overridden to ensure that all opened windows are closed when the main window is closed.

Behavior:

  • Clicking the button in the main window opens a secondary window.
  • When the main window is closed, any secondary windows that were opened will also close automatically.

This setup ensures that secondary windows are properly managed and closed along with the main window.

Using QWidget Instead Of QMainWindow

You can change MainWindow(QMainWindow) to MainWindow(QWidget). This makes MainWindow a subclass of QWidget instead of QMainWindow. When doing this, you'll need to make a few adjustments, such as setting the layout directly on the main window widget since QWidget doesn't have the same central widget concept as QMainWindow.

Here the modified codes:

from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
from PySide6.QtGui import QCloseEvent
from typing import List
import sys

class SecondaryWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Secondary Window")
        self.setGeometry(100, 100, 300, 200)

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Main Window")
        self.setGeometry(100, 100, 400, 300)

        self.opened_windows: List[SecondaryWindow] = []

        self.button: QPushButton = QPushButton("Open Secondary Window", self)
        self.button.clicked.connect(self.open_secondary_window)

        layout: QVBoxLayout = QVBoxLayout(self)
        layout.addWidget(self.button)
        self.setLayout(layout)

    def open_secondary_window(self) -> None:
        new_window: SecondaryWindow = SecondaryWindow()
        new_window.show()
        self.opened_windows.append(new_window)

    def closeEvent(self, event: QCloseEvent) -> None:
        # Close all opened windows when the main window is closed
        for window in self.opened_windows:
            window.close()
        event.accept()

if __name__ == "__main__":
    app: QApplication = QApplication(sys.argv)

    main_window: MainWindow = MainWindow()
    main_window.show()

    sys.exit(app.exec())

Changes Made:

  1. Base Class: Changed MainWindow(QMainWindow) to MainWindow(QWidget).
  2. Layout Management: Since QWidget doesn't have a setCentralWidget method, you directly set the layout on the MainWindow widget using self.setLayout(layout).

Behavior:

  • The functionality remains the same as before. You can still open secondary windows with the button click, and closing the main window will close all the opened secondary windows.