我正在编写一个新的C++应用程序,我决定使用QTQuick而不是传统的Quick,我非常了解QML,但在读取和设置UI控件值的最佳方法方面,我遇到了一些困难
在我的应用程序中,它被称为DOO,我为每个显示都有两个QML文件,第一个定义UI元素,第二个是读取/设置UI值的助手类,它有一个保存到数据库中的save()函数
例如,让我们接受一个名为子的对象,并查看它是如何定义的。
在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()
}
}如您所见,我使用一个函数将一个子对象值读入本地子对象,另一个函数将本地子对象的值复制到一个子对象中,另一个函数用于将UI值读取到本地子对象中。
现在我想知道是否有更好的方法来处理这个场景,而不是readValues(pSon)、copyValues(pSon)和readUIValues()函数
或者这是一个很好的模式
编辑
我认为我可以使用QAbstractItemModel派生类读取/保存到数据库,并将UI值作为JavaScript对象传递给它,或者将UI值读入本地子对象,并将其传递给将验证和保存数据的C++ C++()函数,如下所示:
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)发布于 2014-11-03 13:31:56
First
我认为您应该将更多的逻辑转移到C++代码上。为了获得最好的性能,最好使用尽可能少的js代码。
使用属性将您的子类创建为C++类,然后在可能的情况下将该属性绑定到UI。
第二版:
helper类中的最后一个方法--保存()--可能在特殊类中。当您使用更多的类,比如子类时,这是有用的,因为使用该类的工作将是很简单的。
编辑:
我觉得现在好多了。见下文的评论。
https://stackoverflow.com/questions/26715093
复制相似问题