QML为什么Window.requestUpdate()不起作用?

我有一个Qt快速窗口:

Window{
   id: mainWindow
   ...
}

我想在QML中进行长时间阻止操作之前显示一个消息框:

// Set visible=true of 'messageBox' Rectangle{}
messageBox.showWaitMessage("Please wait")

// I want to show it before blocked the screen
mainWindow.requestUpdate()

// Long blocking operation:
compressFile("filepath")

屏幕上的所有对象都是mainWindow的子对象。


但是它不起作用。阻止操作之前,它仍然不会重绘屏幕。

为什么?

pes2011 回答:QML为什么Window.requestUpdate()不起作用?

这是因为调用这些调用的函数尚未完成,并且排队等待由事件循环执行的requestUpdate()实际上将在compressFile之后发生。 这是设计不良的迹象,最好将compressFile移到另一个线程。如果没有选择线程,则您可能希望像这样间接调用compressFile,这将把该调用放入队列中。然后,GUI事件循环将在执行计时器插槽之前重新绘制窗口小部件。您不需要手动更新GUI。

QTimer::singleShot(0,[=]() { compressFile("filepath") } );

由于QTBUG-26406,这仅适用于Qt5.4及更高版本。 如果您使用的是较低版本的Qt,则可以执行以下操作:

    QTimer *timer = new QTimer(this);
    timer->setSingleShot(true);
    connect(timer,&QTimer::timeout,[=]() {
       compressFile("filepath");
       timer->deleteLater();
    } );
    timer->start(0);
本文链接:https://www.f2er.com/3167165.html

大家都在问