Important: Please read the Qt Code of Conduct -

Calling widget methods form a separate thread

  • I have to call a widget method and it takes a significant amount of time and for this time the application freezes. Can I call it from a separate thread?

  • @michalt38 You could move the executed Code in the Widget to a seperate QThread Class to execute it. But you cant move a Method from a Widget to a QThread. What does the Code in the Method of the Widget? A QWidget is bound to the Mainthread.

  • @michalt38 from the brief description of your problem/requirements, it looks to me that you'll be doing all the way around.
    The method that "takes a significan amount of time" should not be part of your widget. It must belong to some other threaded class (non GUI related) and when the widget is selected/clicked/etc it must run the other thread for the hard work to be done.

    Let's say you're developing a weather forecasting app, so the GUI has a lot of input widgets for the parameters of your forecast model, and the magic "Predict" push button. So when the user fills the values and clicks on Predict you will call the thread for running the super-duper forecasting algorithm involving zillions of GPUs in the cloud, but nothing will happen in your modest widget.

  • It's a VTK widget and this method renders a 3D object

  • @michalt38 Good question!

  • @michalt38 so your problem is quite a bit different as stated initially :-) it's tied to the VTK toolkit and Qt.
    I guess this answer may shade some light about your issue. You may also want to ask in the VTK mailing list for specific information about VTK, Qt and threading

  • Lifetime Qt Champion

    @michalt38 I don't know anything about VTK, but in Qt you should NEVER call any UI related stuff from other thread than GUI thread! Move long lasting operations to a second thread and in this thread emit a signal when calculation has finished. Connect this signal to a slot in your UI and do what ever needs to be done with the result.

Log in to reply