How can I prevent Qt Modality from interacting with external application windows?

I seem to be running into an issue where the modality of Qt dialogs is affecting the z-index of other grandparents.

A minimal reproducible example is the following,

from PyQt6.QtCore import pyqtSignal, Qt
from PyQt6.QtWidgets import QApplication, QDialog, QLabel, QPushButton, QVBoxLayout


class DialogA(QDialog):
    def __init__(self, parent):
        super().__init__(parent)
        self.setWindowModality(Qt.WindowModality.ApplicationModal)
        self.setModal(True)
        self.setWindowTitle("Dialog A")
        self.setLayout(QVBoxLayout())
        self.layout().addWidget(QLabel("This is Dialog A"))

        open_dialog_b_button = QPushButton("Open Dialog B")
        open_dialog_b_button.clicked.connect(self.open_dialog_b)

        self.layout().addWidget(open_dialog_b_button)

    def open_dialog_b(self):
        dialog_b = DialogB(self)
        dialog_b.closed.connect(self.close_dialog_a)
        dialog_b.exec()

    def close_dialog_a(self):
        self.deleteLater()


class DialogB(QDialog):
    closed = pyqtSignal()

    def __init__(self, parent):
        super().__init__(parent)
        self.setWindowModality(Qt.WindowModality.ApplicationModal)
        self.setModal(True)
        self.setWindowTitle("Dialog B")
        self.setLayout(QVBoxLayout())
        self.layout().addWidget(QLabel("This is Dialog B"))
        close_button = QPushButton("Close")
        close_button.clicked.connect(self.on_close_button_clicked)
        self.layout().addWidget(close_button)

    def on_close_button_clicked(self):
        self.closed.emit()
        self.deleteLater()

    def closeEvent(self, event):
        self.closed.emit()
        super().closeEvent(event)


if __name__ == "__main__":
    app = QApplication([])
    main_window = QDialog()
    main_window.setWindowTitle("Main Window")
    main_window.setLayout(QVBoxLayout())

    open_dialog_a_button = QPushButton("Open Dialog A")
    open_dialog_a_button.clicked.connect(lambda: DialogA(main_window).show())

    main_window.layout().addWidget(open_dialog_a_button)
    main_window.show()
    app.exec()

Running the following code will create one MainWindow dialog which creates DialogA and DialogA will create DialogB. When DialogB is closed via the button being closed, it seems to push the MainWindow one window backwards. Therefore, any Window behind the MainWindow will become infront of the MainWindow. The last focused window seems to be important, as I am running PyCharm that is my last focused window. It is then easy to run via PyCharm and have PyCharm as my example window that comes into the foreground when closing DialogB.

To reproduce it, run the code above and interact close DialogB with the last focused window on the OS behind the MainWindow. When closing, the MainWindow will go behind the last focused window.

The structure here is mimicking the code structure in my much larger codebase, i.e. I need deleteLater and have noticed that this is resolved using .close instead.

My goal here is to understand, given the current code structure, why the MainWindow would be going behind the last focused window when closing DialogB.

9

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật