model.rowCount() 不会绑定到 Item 的属性

K.T*_*lis 3 qt scrollview qml qtquickcontrols2

我有一个ListViewrootModelQml 内部命名的自定义 C++ 模型初始化的。模型继承QAbstractListModel并将 a 定义QVector<customType>为私有成员以填充模型。

在 my 中,ApplicationWindow我创建了一个,Dialog在其中更改模型并调用setList()函数来更新它。这工作正常。

我还想将模型的大小连接到 aScrollViewint属性。此属性将定义childrena 的RowLayout

问题是,当我尝试将此属性绑定到模型的大小时,应用程序崩溃了。

仅供参考,模型的所有修改都遵循 Qt 的规则。rowCount()Q_INVOKABLE。我也尝试过使用onModelChanged处理程序,但这没有用(我在文档中检查了这个信号是在发出时modelReset()发出的,这是setList()通过内部发生的endResetModel()

我相信这是一个简单的过程(已经在我的项目中多次执行了属性绑定)但没有按预期工作。

我引用了我的项目的一些示例代码。

//main.qml
ConfiguredChannels{
    id: configuredList
    anchors{
        left: parent.left
        top: devices.bottom
        right: tabs.left
        bottom: parent.bottom
    }
}

TabArea {
    id: tabs
    y: toolBar.height
    x: parent.width / 8
    anchors {
        top: toolBar.bottom
    }
    width: 3 * parent.width / 4
    height: 3 * parent.height / 4
    countPWM: configuredList.model.rowCount() //This is where I want to bind.
}


//ConfiguredChannels.qml
id: confChanView
header: confChanHeader
model: ChannelModel{
    id: rootModel
    list: channelList
}

//TabArea.qml
Item{
id: tabAreaRoot
property alias channelsPWM: channelsPWM
property int countPWM
ScrollView{
            id: scrollPWM
            anchors.fill: parent
            contentItem: channelsPWM.children
            horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOn
            RowLayout{
                id: channelsPWM
                spacing: 0
                Layout.fillHeight: true
                Layout.fillWidth: true
                Component.onCompleted: {
                    var namesPWM = [];
                    for (var i=0; i<countPWM; i++){
                        namesPWM.push("Channel"+(i+1));
                    }
                    createChannels(countPWM, "PWM", channelsPWM, namesPWM);
                }
            }
}
Run Code Online (Sandbox Code Playgroud)

[编辑 1] 仔细观察后,我意识到使用我当前的实现,即使我正确绑定到模型的大小,我仍然无法按需创建所需数量的RowLayout's children(在我更改 中的模型之后Dialog Configuration.qml)。

那是因为我已经将他们的创作放在了RowLayoutComponent.onCompleted处理程序中。该处理程序的内容将在第一次在Configuration.qml内部初始化后执行main.qml。之后,对 的任何其他更改countPWM都不会产生任何影响,因为该组件已经完成!如果我在这一点上错了,请纠正我

基于此,我遵循了另一个实现。我创建了一个createChannels名为createStrips(countPWM). 这样,要正确更新RowLayout'schildren我必须调用此函数。

\\Configuration.qml
\\more code
currentModel.setList(newList)
tabs.createStrips(tableModel.count) \\tableModel is used to populate the newList that will be set to the model
newList.clear()
\\more code

\\TabArea.qml
function createStrips(countPWM){
    var namesPWM = [];
    for (var i=0; i<countPWM; i++){
        namesPWM.push("Channel"+(i+1));
    }
    createChannels(countPWM, "PWM", channelsPWM, namesPWM);
}


function createChannels(counter, channelType, channelParent, channelMapping){
    if ( channelParent.children.length !== 0){
        console.log("destroying");
        for ( var j = channelParent.children.length; j > 0 ; j--){
            channelParent.children[j-1].destroy();
        }
    }

    for (var i=0;i<counter;i++){
        var component = Qt.createComponent(channelType+".qml");
        if( component.status !== Component.Ready )
        {
            if( component.status === Component.Error )
                console.debug("Error:"+ component.errorString() );
            return; // or maybe throw
        }
        var channels =component.createObject(channelParent, { "id": channelType+(i+1), "channelText.text": channelMapping[i]});
    }
Run Code Online (Sandbox Code Playgroud)

[编辑 2] 尽管 EDIT 1 中的解决方案有效并产生了children适合我的权利,但我ScrollView认为它不够好,我相信最好的实现是通过调用createStrips(countPWM)函数来绑定模型的大小更改。就像是:

\\main.qml
ConfiguredChannels{
id: configuredList
anchors{
    left: parent.left
    top: devices.bottom
    right: tabs.left
    bottom: parent.bottom
}
onModelChanged: tabs.createStrips(model.rowCount) //or an appropriate signal handler defined on C++ side
}
Run Code Online (Sandbox Code Playgroud)

也许更好的是,创建children一个自定义qml信号处理程序,每次更改模型的大小时都会发出该信号处理程序。(我尝试了onModelChanged上述但没有奏效。可能我错过了在这种情况下发出的信号)

[解决方案]

我按照已接受的答案以及此链接的说明进行操作

Q_PROPERTYrowCount以 theNOTIFY rowCountChanged和 signal命名的头文件中的模型定义中添加了 a void rowCountChanged();。此外,在setList(newList)我用来更新模型的函数中,我在其实现的末尾添加了emit rowCountChanged();. 最后,我将此信号与createStrips(count)QML 中的函数连接起来。现在,每次更改模型的大小时,我ScrollView都会自动更新显示为RowLayout子项的条带。

\\ChannelModel.h
...
Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged)
...
signals:
void rowCountChanged();

\\ChannelModel.cpp
void ChannelModel::setList(ChannelList *list)
{
beginResetModel();
...
endRestModel();
emit rowCountChanged();
}

\\main.qml
Connections {
    target: configuredList.model
    onRowCountChanged: tabs.createStrips(configuredList.model.rowCount)
}
Run Code Online (Sandbox Code Playgroud)

eyl*_*esc 6

只有 q-property 允许绑定,在您的情况下Q_INVOKABLE不允许,因此您必须创建它,为此我们使用信号rowsInsertedrowsRemoved如下所示:

*。H

    Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged)
public:
    ...
signals:
    void rowCountChanged();
Run Code Online (Sandbox Code Playgroud)

*.cpp

//constructor
connect(this, &QAbstractListModel::rowsInserted, this, &YourModel::rowCountChanged);
connect(this, &QAbstractListModel::rowsRemoved, this, &YourModel::rowCountChanged);
Run Code Online (Sandbox Code Playgroud)

*.qml

countPWM: configuredList.model.rowCount // without ()
Run Code Online (Sandbox Code Playgroud)

笔记:

我假设当您添加或删除元素时,您正在使用:

beginInsertRows(QModelIndex(), rowCount(), rowCount());
//append data
endInsertRows();
Run Code Online (Sandbox Code Playgroud)

或者:

beginRemoveRows(QModelIndex(), from, to)
// remove
endRemoveRows();
Run Code Online (Sandbox Code Playgroud)