5

在使用 QtQuick 控件的 QtQuick 2 中,您可以创建复杂的桌面应用程序。但是在我看来,整个 UI 必须在应用程序启动时立即声明和创建。您还不想使用的任何部分(例如 File->Open 对话框)仍必须创建,但它们是隐藏的,如下所示:

ApplicationWindow {

  FileDialog {
    id: fileOpenDialog
    visible: false
    // ...
  }
  FileDialog {
    id: fileSaveDialog
    visible: false
    // ...
  }
  // And so on for every window in your app and every piece of UI.

现在,这对于简单的应用程序来说可能很好,但对于复杂的应用程序或有很多对话框的应用程序,这肯定是一件疯狂的事情吗?在传统的 QtWidgets 模型中,您可以在需要时动态创建对话框。

我知道有一些解决方法,例如,您可以Loader直接在 javascript 中使用甚至动态创建 QML 对象,但是它们非常丑陋,并且您失去了 QML 语法的所有好处。您也不能真正“卸载”组件。好吧Loader,你可以,但我试过了,我的应用程序崩溃了。

这个问题有优雅的解决方案吗?还是我只需要咬紧牙关,一次为我的应用创建所有潜在的 UI,然后隐藏大部分?

注意:这个页面有关于使用Loaders 来解决这个问题的信息,但是你可以看到它不是一个很好的解决方案。

编辑 1 - 为什么 Loader 不是最理想的?

好的,为了告诉你为什么Loader不那么愉快,考虑这个例子,它开始一些复杂的任务并等待结果。假设 - 与人们通常给出的所有琐碎示例不同 - 任务有许多输入和多个输出。

这是Loader解决方案:

Window {
    Loader {
        id: task
        source: "ComplexTask.qml"
        active: false
    }
    TextField {
        id: input1
    }
    TextField {
        id: output1
    }
    Button {
        text: "Begin complex task"
        onClicked: {
                // Show the task.
                if (task.active === false)
                {
                    task.active = true;
                    // Connect completed signal if it hasn't been already.
                    task.item.taskCompleted.connect(onTaskCompleted)
                }

                view.item.input1 = input1.text;
                // And several more lines of that...
            }
        }
    }

    function onTaskCompleted()
    {
        output1.text = view.item.output1
        // And several more lines...

        // This actually causes a crash in my code:
//      view.active = false;
    }
}

如果我不这样做Loader,我可以有这样的事情:

Window {
    ComplexTask {
        id: task
        taskInput1: input1.text
        componentLoaded: false
        onCompleted: componentLoaded = false
    }

    TextField {
        id: input1
    }
    TextField {
        id: output1
        text: task.taskOutput1
    }

    Button {
        text: "Begin complex task"
        onClicked: task.componentLoaded = true
    }
}

这显然要简单得多。我显然想要的是某种方式ComplexTask来加载并在componentLoaded设置为 true 时激活其所有声明性关系,然后在componentLoaded设置为 false 时断开关系并卸载组件。我很确定目前没有办法在 Qt 中制作这样的东西。

4

3 回答 3

6

从 JS 动态创建 QML 组件就像从 C++ 动态创建小部件一样丑陋(如果不是更少,因为它实际上更灵活)。它没有什么难看的,你可以在单独的文件中实现你的 QML 组件,使用 Creator 在创建它们时提供的所有帮助,并在你需要它们的地方尽可能多地实例化这些组件。从一开始就隐藏所有东西是很丑陋的,它也很重,它不可能像动态组件实例化那样预测可能发生的一切。

这是一个简约的自包含示例,它甚至不使用加载器,因为对话框是本地可用的 QML 文件。

对话框.qml

Rectangle {
    id: dialog
    anchors.fill: parent
    color: "lightblue"

    property var target : null

    Column {
        TextField {
            id: name
            text: "new name"
        }
        Button {
            text: "OK"
            onClicked: {
                if (target) target.text = name.text
                dialog.destroy()
            }
        }
        Button {
            text: "Cancel"
            onClicked: dialog.destroy()
        }
    }
}

main.qml

ApplicationWindow {
    visible: true
    width: 200
    height: 200

    Button {
        id: button
        text: "rename me"
        width: 200
        onClicked: {
            var component = Qt.createComponent("Dialog.qml")
            var obj = component.createObject(overlay)
            obj.target = button
        }
    }

    Item {
        id: overlay
        anchors.fill: parent
    }
}

此外,上面的例子是非常简单的,只是为了说明,考虑使用堆栈视图,无论是您自己的实现还是自 5.1 stock 以来可用的StackView

于 2014-11-06T12:06:06.813 回答
0

这是 ddriver 的答案的一个小替代方案,它不会在Qt.createComponent()您每次创建该组件的实例时调用(这将非常慢):

// Message dialog box component.
Component {
    id: messageBoxFactory
    MessageDialog {
    }
}
// Create and show a new message box.
function showMessage(text, title, modal)
{
    if (typeof modal === 'undefined')
        modal = true;

    // mainWindow is the parent. We can also specify initial property values.
    var messageDialog = messageBoxFactory.createObject(mainWindow, {
                                                           text: text,
                                                           title: title,
                                                           visible: true,
                                                           modality: modal ? Qt.ApplicationModal : Qt.NonModal
                                                       } );

    messageDialog.accepted.connect(messageDialog.destroy);
    messageDialog.rejected.connect(messageDialog.destroy);
}
于 2015-07-14T07:11:47.853 回答
-2

我认为加载和卸载元素不再是实际的,因为每个用户都有超过 2GB 的 RAM。

你认为你的应用程序可以占用超过 512 MB 的内存吗?我对此表示怀疑。

您应该加载 qml 元素并且不要卸载它们,不会发生崩溃,只需存储所有指针并操作 qml 帧。

如果您只是将所有 QML 元素保存在 RAM 中并存储它们的状态,它会运行得更快并且看起来更好。

示例是我以这种方式开发的项目:https ://youtube.com/watch?v=UTMOd2s9Vkk

我已经制作了所有窗口都继承的基础框架。这个框架确实有方法 hide/show 和 resetState。基本窗口确实包含所有子帧,因此通过信号/插槽其他帧显示/隐藏下一个所需的帧。

于 2014-11-06T13:14:48.240 回答