3

我正在尝试将数据(在 C++ 中)与可视化(QML 中的列表)严格分开。在模型方面,数据存储在 C++ 类 (Data.cpp) 的实例中,指向该实例的指针由另一个 C++ 类 (Parser.cpp) 排列在两个列表(completeList 和 selectedList)中。这两个列表应该在 QML 中显示,但有以下限制:

  • completeList 中的数据显示在不同的 QML ListViews 中(例如根据名称)
  • 这种分离必须在 QML 中完成,而不是通过 Parses.cpp(仅提供完整列表)
  • 必须可以将列表项从 completeList 添加到 selectedList(例如通过单击它)
  • 如果在一个 ListView 中更改了 Data 的值,则 Data 本身和显示此 Data 的其他视图应相应更改

感谢 ksimons,我找到了完成一半的方法,但我无法弄清楚其余的。这是我到目前为止得到的:

数据.h

class Data : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)

public:
    Data(const QString &n) : _name(n) { }
    QString name() const { return _name; }

    void setName(const QString &n) {
        if (_name == n)
            return;
        _name = n;
        emit nameChanged(n);
    }

signals:
    void nameChanged(const QString &n);

private:
    QString _name;
};

解析器.h

class Parser : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QQmlListProperty<Data> completeList READ completeList CONSTANT)
    Q_PROPERTY(QQmlListProperty<Data> selectedList READ selectedList CONSTANT)

public:
    Parser(QObject *parent = 0) {
         Data* d1 = new Data(QStringLiteral("a1"));
         Data* d2 = new Data(QStringLiteral("a2"));
         Data* d3 = new Data(QStringLiteral("a3"));
         Data* d4 = new Data(QStringLiteral("b1"));
         Data* d5 = new Data(QStringLiteral("b2"));

        _completeList.append(d1);
        _completeList.append(d2);
        _completeList.append(d3);
        _completeList.append(d4);
        _completeList.append(d5);

        _selectedList.append(d1);
        _selectedList.append(d4);
    }

    QQmlListProperty<Data> completeList() {
        return QQmlListProperty<Data>(this, _completeList);
    }
    QQmlListProperty<Data> selectedList() {
        return QQmlListProperty<Data>(this, _selectedList);
    }

private:
    QList<Data*> _completedList;
    QList<Data*> _selectedList;
};

QML部分:

ListView
{
    id: startsWithA
    anchors.fill: parent
    delegate: delegateItem
}
ListView
{
    id: startsWithB
    anchors.fill: parent
    delegate: delegateItem
}
ListView
{
    id: selectedData
    anchors.fill: parent
    delegate: delegateItem
}
Item 
{
     id: delegateItem
     height: 30
     width: parent.width

     Text { text: name }

     MouseArea { //change name of corresponding Data and other views accordingly
     anchors.fill: parent
     onClicked: model.name = "newName";} //does not work

     MouseArea { //add to selectedList in Parser
     anchors.fill: parent
     onClicked: ?????? }
}
Component.onCompleted:
{
    selectedData.model = parser.seletedList;
    var listAll = parser.completeList;
    var listA, listB;
    for(var i=0; i<listAll.length; i++)
    {
        if(listAll[i].name.charAt(0) == 'a')
        {
            //changing values in startsWithA does not affect anything else with that :(
            listA.append(listAll[i]); 
        }
        else if(listAll[i].name.charAt(0) == 'b')
        {
            //changing values in startsWithB does not affect anything else with that :(
            listB.append(listAll[i]); 
        }
        //...
    }
    startsWithA.model = listA;
    startsWithB.model = listB;
    //...
}

我希望这个问题不是具体的,有人可以帮助我。先感谢您。

4

0 回答 0