Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWidget is not updated at the right time



  • Hi!
    I have the code running. In certain parts of the code, I need to update the widget. But the widget is updated only when all the code is completed. I tried to output a part of the code to a separate thread and wait for it to finish, but while the thread is waiting for the end of the flow, the widget is not redrawn.


  • Lifetime Qt Champion

    @Mikeeeeee said in QWidget is not updated at the right time:

    but while the thread is waiting for the end of the flow, the widget is not redrawn

    Of course not - you're blocking the event loop.
    You will need to provide more information and code if you want to get a meaningful answer.



  • @jsulm it is my code

    self.parent.waitServer.showWindow()  # need draw this window
    self.parent.updateWindowAtStart()
    self.parent.showWindow()
    self.parent.waitServer.hideWindow()
    

    this toonot works:

    elf.parent.waitServer.showWindow()
     thread = QThreadPool()
     thread.start(self.parent.updateWindowAtStart())
    thtred = Thread(target=self.parent.updateWindowAtStart)
    thtred.start()
    QCoreApplication.processEvents()
    thtred.join()
    self.parent.updateWindowAtStart()
    self.parent.showWindow()
     self.parent.waitServer.hideWindow()
    

  • Lifetime Qt Champion

    @Mikeeeeee said in QWidget is not updated at the right time:

    thtred.join()

    You're blocking the event loop again.


  • Qt Champions 2017

    I don't believe python allows preemptive multitasking to begin with, so I am not really convinced this can ever work.

    (And that is leaving aside the question of touching GUI stuff from other threads, which you may or may not be doing, can't tell).


  • Lifetime Qt Champion

    @Mikeeeeee As @SGaist told you you are blocking the event loop. Please take time to learn event driven/asynchronous programming!
    The way you should do your task:

    • Start your thread
    • Emit signals from that thread every time you need to update UI and pass needed information as signal parameters
    • In UI thread connect a slot to the above signal and update UI in that slot
    • Do NOT wait for the thread (remove thtred.join())
    • No need for QCoreApplication.processEvents() with this approach

Log in to reply