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

推荐订阅源

人人都是产品经理
人人都是产品经理
博客园_首页
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
小众软件
小众软件
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Announcements
Recent Announcements
M
MIT News - Artificial intelligence
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub 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.