Question:
I am new to PyQT4. I develop a program with it, and I use web search to get data in my program. While the information loads my graphical interface. I would like to call this function in a separate background thread, possibly using QThread, but it's hard for me to wrap my head around QThread, Qt as a whole and the way the slots / signals are transmitted.
I read about creating a generic workflow that will call any function passed to it. I do not know how to implement it in my main file so that I can perform my functions as a background process. If any sample code can be shown, please explain each line in detail, as I do not understand this process.
Questions:
- How can I prevent my GUI from freezing while the function is running?
- How to use background thread to run functions from my class?
code:
My ui is loaded from an external file created by Qt 4 Designer.
Complete Files on Github
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
main.py (main file)
def connections():
ui.btnRefreshSummary.clicked.connect(lambda: summary())
def refresh_ui():
if summary_data != []:
ui.valWatching.setText(summary_data[0])
ui.valBidding.setText(summary_data[1])
ui.valWon.setText(summary_data[2])
ui.valNotWon.setText(summary_data[3])
ui.valPurchases.setText(summary_data[4])
ui.valInvoices.setText(summary_data[5])
def login():
time.sleep(5)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
connections()
with open('login.txt') as f:
credentials = f.readline().strip().split(':')
f.closed
b = Biddergy()
b.login(credentials[0],credentials[1])
summary_data = b.summary()
b.logout()
refresh_ui()
sys.exit(app.exec_())
source
share