Forcing the Qt GUI to update before entering a separate function

前端 未结 4 1499
小蘑菇
小蘑菇 2020-12-09 02:48

This seems like it should be automatic, but apparently it\'s not. I have the following code:

    ui.my_label->setText(\"Test 1...\");
    ui.my_label->         


        
4条回答
  •  一个人的身影
    2020-12-09 03:23

    Defining a function...

    void YourClass::Update_Ui()
    {
    if(this->isEnabled())
        return;
    
    this->repaint();
    qApp->processEvents();
    }
    

    ...like this and make sure this is disabled (to prevent user actions) while you want to force an update of the ui was the best solution for me.

    Example how to use it inside a function (for example during a stack processing that takes lots of time):

    this->setEnabled(false);
    //Do whatever you want
    Update_Ui();
    //Do some other stuff
    this->setEnabled(true);
    

    This doesn't allow the user to disturb the processing by ui interaction (it is disabled) and updates whenever Update_Ui(); is called in the code and updates the whole ui, not just a chosen label or whatever. Note that this doesn't block signals fired by ui elements.

提交回复
热议问题