C++ 如何设置C++;通过qml中的父组件建模?

C++ 如何设置C++;通过qml中的父组件建模?,c++,qt,qml,C++,Qt,Qml,我有: 存储简单数据对象(系数)的C++模型类(Bank): 其中cfg是在main.cpp中注册的对象: BankView.qml是由BankViewHeader.qml(作为复选框)和BankViewDescription.qml(作为包含文本字段列表的组件)组成的委托 BankView.qml: Rectangle { // your any item property alias bankIndex: description.bankIndex // ...

我有:

存储简单数据对象(系数)的C++模型类(Bank): 其中cfg是在main.cpp中注册的对象:

BankView.qml是由BankViewHeader.qml(作为复选框)和BankViewDescription.qml(作为包含文本字段列表的组件)组成的委托

BankView.qml:

Rectangle {  // your any item
    property alias bankIndex: description.bankIndex

    // ...

    BankViewDescription {
        id: description
        // ...
    } 
}
BankViewDescription.qml:

RowLayout {  // your any item
    property alias bankIndex: regs_list_model.rootIndex

    // ...

    ListView {
        model: DelegateModel {
            id: regs_list_model
            model: cfg
            delegate: Row {
                TextField {
                    text: coefficient // use your role
                }
            }
        }
    }
}
行布局{
// ...
列表视图{
模型:委派模型{

模型:模型//< P>,因此考虑编辑部分并用AMFASIS帮助解决方案是: 对于该结构(如图中所示的2d数据存储),我们应该创建一个树模型,然后设置正确的qml结构

在我的例子中,qml结构是:

// model view in main.qml
ListView {
    // ...
    model: DelegateModel {
        id: bank_list_model
        model: cfg // my ConfigModel
        delegate: BankView {
            bankIndex: bank_list_model.modelIndex(index)
        }
    }
}
BankView.qml:

Rectangle {  // your any item
    property alias bankIndex: description.bankIndex

    // ...

    BankViewDescription {
        id: description
        // ...
    } 
}
BankViewDescription.qml:

RowLayout {  // your any item
    property alias bankIndex: regs_list_model.rootIndex

    // ...

    ListView {
        model: DelegateModel {
            id: regs_list_model
            model: cfg
            delegate: Row {
                TextField {
                    text: coefficient // use your role
                }
            }
        }
    }
}
因此,在这里,我们将DelegateModel rootIndex属性链接到带有别名的项

PS:我忘了在编辑部分覆盖ConfigModel上的roleNames方法


PPS:如果您知道一种更简单的方式来描述模型视图连接或应用程序结构,那么欢迎使用。

我认为您应该使用QList(其中QObject实际上是一个银行)要拥有所有银行的列表,这样您就不必在根上下文中为您想要创建的每个银行指定索引属性,以及我如何访问qml中的每个系数?因为qml没有(可能还没有)了解QVoT,我将在<代码>银行类中生成QLead属性,并且实际上可以使用TyPulf而不是用一个成员来构造,除非您计划有更多的成员,然后您可以考虑使用<代码> QSGADGET look,例如:1:(系数)一个复杂的对象模型(不只是int值作为一个字段)(2)(银行)一个C++模型,用于存储多个对象(具有对象容器的QY属性);3(BankModel)用于编辑数据和工作的抽象的ListMead,用于QML视图;4(配置模型)一个用于存储多个(BojMead)的QObjistCLIST模型。AbstracktListModel的.5.在main.cpp中将配置模型对象注册为ConfigModel,并在main.qml的ListView的model字段中设置它。它是否适合应用程序架构?让我们来看看。
class CoefficientItem
{
public:
    CoefficientItem(quint32 value = -1, int row = 0, CoefficientItem* parent = nullptr);
    ~CoefficientItem();

    CoefficientItem *childAt(int i);
    int childrenCount();
    CoefficientItem *parent();
    quint32 value() const;
    int row() const;

    void setValue(const quint32& value);
    void setParent(CoefficientItem* parent_item);

    void appendChild(CoefficientItem* child);

private:
    quint32 _value;
    CoefficientItem* _parent_item;
    QHash<int, CoefficientItem*> _child_items;
    int _row_number;
};
class ConfigModel : public QAbstractItemModel
{
    Q_OBJECT

    enum { CoefficientRole = Qt::UserRole };

public:
    explicit ConfigModel(const QVector<QVector<quint32>>& config, CoefficientItem* root = nullptr, QObject *parent = nullptr);
    ~ConfigModel();

    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role = CoefficientRole) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;

    Qt::ItemFlags flags(const QModelIndex& index) const override;

    void setModelUp(const QVector<QVector<quint32> >& config);

private:
    CoefficientItem* _root_item;
};
ListView {
    anchors.fill: parent
    model: DelegateModel {
        model: cfg
        delegate: BankView {}
    }
}
Rectangle {
    id: bank_view
    width: parent.width
    height: bank_view_column.height
    color: "#946782"

    Column {
        id: bank_view_column
        spacing: 0
        clip: true

        BankViewHeader {
            id: checker
            text: qsTr("Bank " + model.index)
            checked: true
            implicitWidth: bank_view.width
        }

        BankViewDescription {
            id: description
            isOpened: checker.checked
            width: bank_view.width
        }
    }

    Component.onCompleted: checker.checked = false
}
RowLayout {
    // ...
    ListView {
        model: DelegateModel {
            model: model // <-- problem here ???
            delegate: Row {
                TextField {
                    //... use my role here - coefficient (uint32)
                }
            }
            // ...
        }
        // ...
    }
}
// model view in main.qml
ListView {
    // ...
    model: DelegateModel {
        id: bank_list_model
        model: cfg // my ConfigModel
        delegate: BankView {
            bankIndex: bank_list_model.modelIndex(index)
        }
    }
}
Rectangle {  // your any item
    property alias bankIndex: description.bankIndex

    // ...

    BankViewDescription {
        id: description
        // ...
    } 
}
RowLayout {  // your any item
    property alias bankIndex: regs_list_model.rootIndex

    // ...

    ListView {
        model: DelegateModel {
            id: regs_list_model
            model: cfg
            delegate: Row {
                TextField {
                    text: coefficient // use your role
                }
            }
        }
    }
}