Results 1 to 3 of 3

Thread: How to update GUI from a thread?

  1. #1
    Join Date
    Feb 2021
    Posts
    4
    Thanks
    4

    Default How to update GUI from a thread?

    I'm trying to update the QLabel in the GUI with the number, generated in a thread. Can this be done?

    Qt Code:
    1. import threading, random, sys
    2. from PyQt5.QtWidgets import *
    3. from PyQt5.QtGui import *
    4. from PyQt5.QtCore import *
    5. from PyQt5 import QtCore, QtTest
    6.  
    7.  
    8. def update():
    9. while True:
    10. n = random.randint(0,22)
    11. QtTest.QTest.qWait(500)
    12.  
    13. threading.Thread(target=update, daemon = True).start()
    14.  
    15. class MainWindow(QMainWindow):
    16.  
    17. def __init__(self, *args, **kwargs):
    18. super(MainWindow, self).__init__(*args, **kwargs)
    19. self.setFixedSize(200, 200)
    20.  
    21. self.labl = QLabel(self)
    22. self.labl.setText("value of n goes here")
    23.  
    24.  
    25. app = QApplication(sys.argv)
    26. window = MainWindow()
    27. window.show()
    28. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to update GUI from a thread?

    The usual way is to have your thread emit a signal that is handled by a slot in the your GUI thread, where you pass the value as an argument in the signal.

    This also means you probably need to use QThread for your thread implementation.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    kiskivancsi (7th February 2021)

  4. #3
    Join Date
    Apr 2021
    Location
    Toronto
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to update GUI from a thread?

    Have same problem

Similar Threads

  1. Replies: 51
    Last Post: 21st April 2015, 13:12
  2. Update progress bar in another thread
    By qt_developer in forum Qt Programming
    Replies: 8
    Last Post: 19th June 2012, 19:41
  3. use Thread update many GUI
    By phuong_90 in forum Qt Programming
    Replies: 6
    Last Post: 20th November 2011, 06:23
  4. Update GUI in a thread
    By olivier1978 in forum Qt Programming
    Replies: 3
    Last Post: 8th January 2011, 22:33
  5. Update GUI from another thread
    By Anne in forum Qt Programming
    Replies: 9
    Last Post: 14th July 2010, 16:08

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.