在Qt Quick中设置/读取UI值的最佳方式是什么
What is the best way to set/read UI values in Qt Quick
我正在编写一个新的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这样的类时,这可能会很有用,因为这些类的工作是相似的。
编辑:
我觉得现在好多了。请参阅下面的评论。
- 在c代码之间共享数据的最佳方式
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 在reactor中存储eventHandlers的最佳方式是什么
- 在AVX通道中混洗的最佳方式
- 从 T 创建 std::future 的最佳方式<T>
- C++:使用 std::unique_ptr 访问重载运算符++的最佳方式?
- 对列表列表中的元素进行分组的最佳方式
- 利用 GPU 的最佳方式
- 使用 QT C++过滤大数据的最佳方式
- 算法设计:用边界数字表示 2D 网格的最佳方式,以C++?
- 在C++中共享键值对的最佳方式
- 为Catch2中的外部文本文件指定路径的最佳方式
- 代表Quarto棋盘游戏棋子的最佳方式
- 等待线程的最佳方式是什么
- 将uint8_t*buffer和size_tbufferlen从C++传递到C中的API函数的最佳方式是什么
- 创建控制台菜单C++的最佳方式
- 只显示片段着色器的最佳方式是什么
- 复制文件的最佳方式是什么,以便我可以在复制过程中轻松取消复制?