簡體   English   中英

單個 window 模式能夠在 PyQt5 中一次打開多個對話框?

[英]Single window mode with the ability to open multiple dialogs at once in PyQt5?

當我打開一個新對話框時,我在 PyQt5/PySide2 中使用以下代碼來創建單模式 window 應用程序:

dialog.setWindowModality(QtCore.Qt.ApplicationModal)
dialog.setWindowFlags(QtCore.Qt.Tool)

但是我需要另一種模式,我沒有通過閱讀文檔來解決它,假設我們有一個 QMainWindow 並且我們在主 window 上有 4 個按鈕,我想在單擊按鈕時打開相應的對話框,但是這個想法是這樣的:

  1. 通過使用上面的代碼,父 window (Main) 將被阻止,因此無法單擊其他按鈕來打開對話框。

  2. 防止應用程序打開已經打開的對話框。

好的,在主 window 中你可以創建打開自己的 window 的函數,例如:

主 window 文件:

def open_add_user_window(self):
    # import the class of window dialog that you create, example:
    from new_user_dialog import NewUserDialog # the class you create
    # Create a variable to store the object of the NewUserDialog class
    # in this case self means the instances of the main window
    window = NewUserDialog(self)  
    # then call the method show() of the object window
    window.show()

新用戶 window 文件(子窗口):

# in the constructor:
# parent is the parameter that stored the main window instance 
def __init__(self, parent=None):
    # with super call the constructor of the QDialog class an passed like argument the 
    #parent
    super().__init__(parent)
    # This is important if you don't put this code the child window will appear like 
    #sticked on the main window.
    self.setWindowFlag(Qt.Window) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM