'How to start a QDialog in a separate process in PyQt5? [duplicate]
I have QDialog designed by PyQt5 designer, I basically need to execute this dialog box from QMainWindow app in a separate process. Although I don't get any errors with below code but the Dialog box never show up. Can anyone tell me what am I doing wrong?
Main Window App on button click:
def alertWindow(self, alertInfo):
p = Process(name="alertwindow", target=alertWindowCustom, args=(alertInfo,))
Dialog Box Class:
class alertWindowCustom(QDialog, Ui_alertwindow):
def __init__(self, alertwindowData):
QDialog.__init__(self)
self.setupUi(self)
self.alertwindowData = alertwindowData
self.run()
self.exec_()
def run(self):
print("brah", self.alertwindowData)
If I just call alertWindowCustom class as a = alertWindowCustom(alertInfo) without a process, the dialog box is created but the MainWindow become unresponsive.
If using QThread is a better option to use over multiprocessing, I would rather use that.
Solution 1:[1]
Credit to @musicamante and @ekhumoro.
You don't have to use a thread or a process to create a dialog box that does not block main thread.
To keep your MainWindow responsive while dialog box is open, avoid using exec_() but rather use show(). This will create a non-modeled dialog box and store the dialog box in memory which you will always have to keep a reference of. To do that make a reference of it in your MainWindow as below:
self.alertwindowclass = alertWindowCustom(alertInfo)
Make sure to use self, otherwise as soon as the function is done, dialog box data will be moved to python recycle bin which will destroy you dialog box.
My new full code is below:
class mainWindow(QMainWindow):
# Below function is called to create the dialog box
def alertWindow(self, alertInfo):
self.alertwindowclass = alertWindowCustom(alertInfo)
class alertWindowCustom(QDialog, Ui_alertwindow):
def __init__(self, alertwindowData):
QDialog.__init__(self)
self.setupUi(self)
self.alertwindowData = alertwindowData
self.run()
self.show()
def run(self):
print("brah", self.alertwindowData)
I had to read many forms to gather this information. I wrote an article here if you want to learn more about it. Hope that help people in the future.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pefile |
