【问题标题】:Force refresh the interface in Qt Quick在 Qt Quick 中强制刷新界面
【发布时间】:2018-01-01 13:40:19
【问题描述】:

我正在构建一个 Qt Quick QML 应用程序,一个按钮启动一个需要一段时间才能运行的进程。我想让用户知道该程序实际上正在做某事,但如果我这样做:

Button {
    text: "Click me"
    onClicked: {
        text = "Loading..."
        backend.longRunningFunction()
        text = "Click me"
     }
}

当我单击按钮时,GUI 在backend.longRunningFunction() 运行时冻结,然后恢复正常。但是文本永远不会改变;它总是显示“点击我”。

有没有办法强制 UI 在 onClicked 函数中更新,例如 qApp.processEvents()

【问题讨论】:

标签: qt qml qt5 qtquick2


【解决方案1】:

您可以通过调用在 C++ 中强制更新 GUI

QCoreApplication::processEvents();

longRunningFunction 中的第一件事。

您可以通过在同一个类中创建一个 c++ 函数来使其更通用

void Backend::updateUI() 
{
    QCoreApplication::processEvents();
}

并从您的 onClicked 中调用它

onClicked: {
    button1.text = "Loading..."
    backend.updateUI()
    backend.longRunningFunction()
    button1.text = "Click me"
}

QT 推荐的方法是生成 longRunningFunction 的实际长时间运行部分的 QThread,并从该线程发送完成信号以更新 button1.text。

【讨论】:

    【解决方案2】:

    试试这样的:

    Button {
    id: button1
    text: "Click me"
    onClicked: {
        button1.text = "Loading..."
        backend.longRunningFunction()
        button1.text = "Click me"
     }}
    

    【讨论】:

      猜你喜欢
      • 2014-10-22
      • 2021-10-28
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多