【问题标题】:Use QQmlListProperty to show and modify QList in Qml使用QQmlListProperty在Qml中显示和修改QList
【发布时间】:2013-10-17 13:13:18
【问题描述】:

再次,我有一个问题(也许是一个问题),我在 qt5 中使用 qt 和 qml 以及使用 qtquick 2.0 制作了一个程序,并且我有一个 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
        }

【问题讨论】:

    标签: c++ qml qt5 qlist


    【解决方案1】:

    我自己得到了答案,首先,我在方法 append_concept 中停止使用属性 Q_INVOCABLE,其次,我在 addConcept 的实现中添加了一行代码。代码如下:

    之前:

    Q_INVOKABLE static void append_concept(QQmlListProperty<Concept> *list, Concept *cpt);
    

    现在:

    static void append_concept(QQmlListProperty<Concept> *list, Concept *cpt);
    

    也许这不会影响,但我不想冒险。

    addConceptremoveConcept的实现中:

     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();
    }
    

    【讨论】:

    • 作为注释,这仅适用于每次都更改公开的列表(例如,此列表通过 http get 请求全部更改)或何时可以再次加载所有列表而不会影响程序流程.
    • append_concept(QQmlListProperty *list, Concept *cpt) 应该抛出错误,不应该是 QQmlListProperty 类型?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多