使用 QQmlListProperty 在 Qml 中显示和修改 QList
Use QQmlListProperty to show and modify QList in Qml
再次,我有一个问题(也许是一个问题(,我在Qt5中使用Qt和QML制作了一个程序,并使用QTQUICK 2.0的QML制作了一个程序,我有一个C ++模型qlist,我需要在运行时修改列表,我使用q QQmlListProperty并在qml中显示项目,但是在我添加或删除代码的那一刻,它们没有隐藏和显示是下一个:
class ConceptsList: public QObject{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<Concept> concepts READ concepts NOTIFY conceptsChanged)
Q_CLASSINFO("DefaultProperty", "concepts")
public:
ConceptsList(QObject *parent=0);
QQmlListProperty<Concept> concepts();
Q_INVOKABLE static void append_concept(QQmlListProperty<Concept> *list, Concept *cpt);
Q_INVOKABLE void removeConcept(int index);
Q_INVOKABLE void addConcept(QString m_id,QString description, QString quantity, QString price, QString unit, QString total);
Q_INVOKABLE int countConcepts();
static void clearConcepts(QQmlListProperty<Concept> *property);
static int conceptsSize(QQmlListProperty<Concept> *property);
static Concept *conceptAt(QQmlListProperty<Concept> *property, int index);
signals:
void conceptsChanged();
private:
QList<Concept *> m_concepts;
}
我使用列表视图和委托,我没有问题要查看,但我的问题是我是否可以使用 QQmlListProperty 并修改 Qlist,或者我将更改一个表单以将 qlist 公开给 qml,如果有可能如何从 qml 调用该方法,或者如何在C++中实现,我问是因为存在很少的数量或示例与工作这种形式。在QML中,我的代码是下一个:
ConceptsList{
id:cpts
concepts:[
Concept{
m_id:"7"
m_quantity: "3"
m_price: "1"
m_unit:"1"
m_description:"algo"
m_total:"2"
}
]
}
ListView {
id: listConceptsView
objectName: "list"
anchors.fill: parent
anchors.margins: 5
clip: true
focus: true
highlight: highlightBar
highlightFollowsCurrentItem: false
Component{
id: tableConceptDelegate
Item{
anchors.margins: 4
width: 515
height: 27
clip: true
Row {
spacing: 4
Text {
height: 26; width: 76
text: model.m_id
color: "black"
font.bold: true
horizontalAlignment: Text.AlignHCenter
}
...
...
Text {
height: 26; width: 120
text: model.m_total//amountTotal
color: "black"
font.bold: true
horizontalAlignment: Text.AlignHCenter
}
}
MouseArea {
id: mouse_area1
anchors.fill: parent
onClicked:
{
listConceptsView.currentIndex = index
}
}
}
}
delegate: tableConceptDelegate
model:cptCpt // i define this alias how cptCpt: cpt.concepts
}
我自己得到了答案,首先,我停止在方法中使用属性Q_INVOCABLE append_concept,其次,我在addConcept的实现中添加了一行代码。这是代码:
以前:
Q_INVOKABLE static void append_concept(QQmlListProperty<Concept> *list, Concept *cpt);
现在:
static void append_concept(QQmlListProperty<Concept> *list, Concept *cpt);
也许这不会影响,但我宁愿不冒险。
在addConcept
和removeConcept
的实现中:
void ConceptsList::addConcept(QString m_id, QString quantity, QString price, QString unit, QString description)
{
Concept *cpt=new Concept(m_id, quantity, unit, price, description);
m_concepts.append(cpt);
this->conceptsChanged();
}
void ConceptsList::removeConcept(int index)
{
m_concepts.removeAt(index);
this->conceptsChanged();
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 独立读取-修改-写入顺序
- 当系统的卷被修改时,如何修改WASAPI环回捕获卷
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我可以通过引用修改常量返回
- 对于结构,表达式必须是可修改的ivalue
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 修改创建帐户程序
- 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案
- 如何从子成员函数修改父公共成员变量
- 修改 VS Code 中的默认C++代码段
- 如何检查QList中是否存在值
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 如何使用递归打印修改后的星号三角形图案
- 已修改的LinkedList未在文本文件本身中更新
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 为什么可以修改数组 b?
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 使用 QQmlListProperty 在 Qml 中显示和修改 QList