Say you have MainWindow in PyQt5 and as part of this app you need a custom Pop-up/Dialog box that does not block MainWindow thread.
To do this, you first design your dialog box in PyQt5 Designer and save it as a UI file and then compile using pyuic5
as follows:
pyuic5 dialog.ui -o dialog.py
This will result into a python script under dialog.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(10, 110, 371, 20))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Welcome to Neehack"))
Note: as recommended by many PyQt5 forms, never modify PyQt5 Designer class. If you need to add features that are only available through python, then create a separate class and call PyQt5 Designer class in your external class as below.
External class calling Ui_Dialog
class CustomDialog(QDialog, Ui_dialog):
def __init__(self):
QDialog.__init__(self)
self.setupUi(self)
self.run()
self.show()
def run(self):
print("Welcome to https://neehack.com/")
Make sure to avoid calling `self.exec_()` in this class, if you do so it would block the main thread until this dialog box is closed as explained in documentation:
Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result.
If you just use `self.show(), this will create a non-modeled dialog box and save it in memory which you will have to keep a reference of until you want the dialog box to exist. If you wanted to hide the dialog box, you can just use
self.hide()
Finally call your custom dialog box class in your MainWindow class as below:
def buttonClicked(self):
self.customDialog = CustomDialog()
Remember to use self while referencing your variable, otherwise as soon as the
buttonClicked function is done, the dialog box will be moved to python recycle bin, destroying dialog box.
15 responses to “How to create a QDialog without blocking main thread in PyQt5?”
Thanks for one’s marvelous posting! I certainly enjoyed reading it, you’re a great author. I will make certain to bookmark your blog and will eventually come back someday. I want to encourage one to continue your great job, have a nice morning!
I love looking through a post that can make people think. Also, many thanks for allowing me to comment!
This post is worth everyone’s attention. Where can I find out more?
Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your webpage? My blog is in the very same area of interest as yours and my users would genuinely benefit from some of the information you provide here. Please let me know if this okay with you. Thanks!
Thanks for sharing your info. I really appreciate your efforts and I am waiting for your next write ups thanks once again.
Its such as you learn my mind! You appear to know a lot approximately this, such as you wrote the book in it or something. I think that you just could do with some percent to force the message house a bit, however instead of that, that is great blog. A fantastic read. I’ll definitely be back.
My brother recommended I might like this website. He was entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this information! Thanks!
Hey there would you mind letting me know which hosting company you’re utilizing? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most. Can you recommend a good hosting provider at a fair price? Thank you, I appreciate it!
I am regular reader, how are you everybody? This piece of writing posted at this web page is in fact pleasant.
We’re a group of volunteers and opening a new scheme in our community. Your site offered us with valuable information to work on. You’ve performed a formidable job and our whole neighborhood can be thankful to you.
Excellent post. Keep writing such kind of info on your page. Im really impressed by your blog.
Hi there, You’ve performed a great job. I’ll definitely digg it and in my opinion suggest to my friends. I’m confident they’ll be benefited from this website.
An outstanding share! I have just forwarded this onto a friend who was conducting a little homework on this. And he actually bought me lunch due to the fact that I discovered it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanx for spending the time to talk about this issue here on your web page.
I blog frequently and I genuinely thank you for your information. The article has really peaked my interest. I am going to bookmark your blog and keep checking for new details about once a week. I opted in for your Feed as well.
I know this if off topic but I’m looking into starting my own weblog and was curious what all is needed to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web savvy so I’m not 100% certain. Any tips or advice would be greatly appreciated. Cheers
We are a group of volunteers and opening a brand new scheme in our community. Your web site offered us with useful information to paintings on. You’ve performed an impressive task and our entire community will likely be grateful to you.