在Qt Quick中设置/读取UI值的最佳方式是什么

What is the best way to set/read UI values in Qt Quick

本文关键字:最佳 方式 是什么 UI 读取 Quick Qt 设置      更新时间:2023-10-16

我正在编写一个新的C++应用程序,我决定使用Qt Quick而不是传统的Qt小部件,我很了解QML,但我在读取和设置UI控件值的最佳方法方面遇到了一些麻烦

在我的应用程序中,它被称为DOO,我为每个显示有两个QML文件,第一个定义UI元素,第二个是用于读取/设置UI值的助手类,它有一个保存()函数,用于保存到数据库

例如,让我们取一个名为Son的对象,看看它是如何定义的

SonDisplay.qml

import QtQuick 2.0
import QtQuick.Layouts 1.1
import DOO.Entities.Son 1.0
import DOOTypes 1.0
import "../UIElements"
import "../UIElements/DOOTheme.js" as DOOTheme
import "../DOO"
Display {
    id:sonDisplay
    contentHeight: 350
    contentWidth: 800
    // this is a QObject derived class, that I use as an entity to a table in a MySQL database
    // I use it here to set/read UI from/to it
    property Son son : Son{}
    property int disMode : DOO.Show
    property bool inEditMode : false
    ....

在助手文件SonHelper.qml

    import QtQuick 2.0
import DOO.Commands.Son 1.0
import DOOTypes 1.0
QtObject {
    // read a Son object values into local son
    function readValues(pSon)
    {
        son.sonID = pSon.sonID
        son.name = pSon.name
        son.image = pSon.image
        son.age = pSon.age
        son.entryDate = pSon.entryDate
        son.commingFrom = pSon.commingFrom
        son.disabilityKind.kind = pSon.disabilityKind.kind
        son.caseDescription = pSon.caseDescription
        son.more = pSon.more
    }
    // copy local son values into a Son object
    function copyValues(pSon)
    {
        pSon.sonID = son.sonID
        pSon.name = son.name
        pSon.image = son.image
        pSon.age = son.age
        pSon.entryDate = son.entryDate
        pSon.commingFrom = son.commingFrom
        pSon.disabilityKind.kind = son.disabilityKind.kind
        pSon.caseDescription = son.caseDescription
        pSon.more = son.more
    }
    // read ui values into local son
    function readUIValues()
    {
        son.name = sonName.text
        son.image = sonImage.picture
        son.age = sonAge.text
        son.entryDate = Date.fromLocaleDateString(Qt.locale(), sonEntryDate.text, "dd-MM-yyyy")
        son.commingFrom = sonCommingFrom.text
        son.disabilityKind.kind = sonDisabilityKind.currentIndex
        son.caseDescription = sonCaseDescription.text
        son.more = sonMore.text
    }
    function validateUIValues()
    {
        if (Date.fromLocaleDateString(Qt.locale(), sonEntryDate.text, "dd-MM-yyyy") == "Invalid Date") return false
        //if(!sonSonID.acceptableInput) return false
        //if(!sonAge.acceptableInput) return false
        //if(!sonDisabilityKind.acceptableInput) return false
        return true
    }
    // save or update a son into database
    function save()
    {
        var v_son =  SonFactory.createObject()
        if (!validateUIValues())
        {
            dooNotifier.showMessage("Error","You Have some invalid input")
            return
        }
        readUIValues()
        copyValues(v_son) // copy local son values into v_son
        if(disMode === DOO.CreateNew)
        {
            if(SonCommands.insert(v_son))
            {
                dooNotifier.showMessage("Success","Son added successfully ")
                SonResultsModel.update()
                sonDisplay.hide()
            }
            else
            {
                dooNotifier.showMessage("Failed","Error adding son")
                DOOLogger.log(SonCommands.lasrErrorText())
            }
        }
        else
        {
            if(SonCommands.update(v_son))
            {
                dooNotifier.showMessage("Success","Son updated successfully ")
                SonResultsModel.update()
                sonDisplay.hide()
            }
            else
            {
                dooNotifier.showMessage("Failed","Error updating son")
                DOOLogger.log(SonCommands.lasrErrorText())
            }
        }
        v_son.destroy()
    }
}

正如你所看到的,我使用一个函数将Son对象的值读取到本地Son对象中,第二个函数将本地Son对象的值复制到Son对象中,而第三个函数将UI值读取到局部Son对象中

现在我想知道是否有更好的方法来处理这种情况,而不是readValues(pSon)copyValues(pSon)readUIValues()函数

或者这是一个很好的模式来做

编辑

我想我可以使用QAbstractItemModel派生类来读取/保存到数据库,并将UI值作为JavaScript对象传递给它,或者将UI值读取到本地子对象,并将其传递给C++save()函数,该函数将验证和保存数据,如下所示:

    var uivalues = {
    name : sonName.text ,
    image : sonImage.picture ,
    age : sonAge.text ,
    entryDate : Date.fromLocaleDateString(Qt.locale(), sonEntryDate.text, "dd-MM-yyyy") ,
    commingFrom : sonCommingFrom.text ,
    disabilityKind : sonDisabilityKind.currentIndex ,
    caseDescription : sonCaseDescription.text ,
    more : sonMore.text ,
    }
    SonResultsModel.save(uivalues)

第一个

我认为您应该将更多的逻辑转移到C++代码中。为了获得最佳性能,最好使用尽可能少的js代码。

将您的儿子创建为具有属性的C++类,然后在可能的情况下将这些属性绑定到UI。

第二个:

helper类save()中的最后一个方法可能在特殊类中。当你使用更多像son这样的类时,这可能会很有用,因为这些类的工作是相似的。

编辑:

我觉得现在好多了。请参阅下面的评论。