QML连接在新插入的行中不起作用?

时间:2018-05-28 10:00:21

标签: c++ qt qml qabstractitemmodel

作为一个例子,我拿了the official example from Qt Documentation并添加了几行来证明问题:

model.h:

#include <QAbstractListModel>
#include <QStringList>

//![0]
class Animal
{
public:
    Animal(const QString &type, const QString &size);
//![0]

    QString type() const;
    QString size() const;

private:
    QString m_type;
    QString m_size;
//![1]
};

class AnimalModel : public QAbstractListModel
{
    Q_OBJECT

    Q_PROPERTY(int myProperty READ myProperty NOTIFY myPropertyChanged)

signals:
    void myPropertyChanged();

public:
    enum AnimalRoles {
        TypeRole = Qt::UserRole + 1,
        SizeRole
    };

    AnimalModel(QObject *parent = 0);
//![1]

    void addAnimal(const Animal &animal);

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

    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;

    // my code starts
    virtual bool insertRows(int position, int rows,
                            const QModelIndex &index = QModelIndex()) override;
    virtual Qt::ItemFlags flags(const QModelIndex &index) const override {
        return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
    }
    int myProperty() const {
        return m_myProperty;
    }
    int m_myProperty;

public slots:
    void addAnimal();
        // my code ends

protected:
    QHash<int, QByteArray> roleNames() const;
private:
    QList<Animal> m_animals;
//![2]
};
//![2]

model.cpp:

#include "model.h"

Animal::Animal(const QString &type, const QString &size)
    : m_type(type), m_size(size)
{
}

QString Animal::type() const
{
    return m_type;
}

QString Animal::size() const
{
    return m_size;
}

AnimalModel::AnimalModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_myProperty(0)
{
}

void AnimalModel::addAnimal(const Animal &animal)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_animals << animal;
    endInsertRows();
}

int AnimalModel::rowCount(const QModelIndex & parent) const {
    Q_UNUSED(parent);
    return m_animals.count();
}

QVariant AnimalModel::data(const QModelIndex & index, int role) const {
    if (index.row() < 0 || index.row() >= m_animals.count())
        return QVariant();

    const Animal &animal = m_animals[index.row()];
    if (role == TypeRole)
        return animal.type();
    else if (role == SizeRole)
        return animal.size();
    return QVariant();
}

bool AnimalModel::insertRows(int position, int rows, const QModelIndex &index)
{
    beginInsertRows(index, position, position + rows - 1);

    for (int row = 0; row < rows; ++row) {
        m_animals.insert(position, Animal("new type", "new animal"));
    }

    endInsertRows();

    return true;
}

void AnimalModel::addAnimal()
{
    insertRow(0);

    m_myProperty = m_animals.count();
    emit myPropertyChanged();
}

//![0]
QHash<int, QByteArray> AnimalModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[TypeRole] = "type";
    roles[SizeRole] = "size";
    return roles;
}
//![0]

最后是view.qml:

import QtQuick 2.0
import QtQuick.Controls 2.2

//![0]
Column {
    ListView {
        id: list
        width: 200; height: 250

        model: myModel
        delegate: Text {
            text: index + ": Animal: " + type + ", " + size + ", " + list.model.myProperty

            Connections {
                target: myModel
                onMyPropertyChanged: {
                    console.log(index + ", " + list.model.myProperty)
                }
            }
        }
    }
    Button {
        onClicked: {
            myModel.addAnimal()
        }
    }
}

//![0]

非常简单。按下按钮并调用addAnimal()后,我希望看到这样的控制台输出:

qml: 0, 4
qml: 1, 4
qml: 2, 4
qml: 3, 4

但我看到了这一点:

qml: 1, 4
qml: 2, 4
qml: 3, 4

然而,肯定会发出(并接收)信号,因为UI不仅显示具有更新的动物列表大小的新添加的项目,而且还更新了所有其他行。那么为什么新行没有收到onMyPropertyChanged

这是Qt错误吗?我使用Qt 5.9.5。

animal list size == 4

1 个答案:

答案 0 :(得分:1)

当您使用insertRow(0)时,您将插入位置0,因此前一个0元素现在将是具有连接并将接收信号的元素。

因此,当按下单击时,最后插入的元素将没有连接,因此不会通知它。

为了说明我将逐步解释:

  • 在第一个插页中没有元素,因此没有人收到信号。

  • 在第二次插入时,前一个0元素将是当前的1,并且它具有连接,因此将被通知。

  • 在第三次插入中,前一个0元素将是当前的1,前一个元素将是当前的2,因此1,2具有连接并将被通知。

总之,代表的创建是在myPropertyChanged信号发布之后,因此插入的代表将不会被通知,其他人将会通知,并且由于您的插入始终位于第一个位置,因此永远不会打印qml: 0, n

要以图形方式理解假设有代表并且已经存在,他们将收到通知:

                                            0           0  (+)
0 (+)                                       1  (+)      1  (+)
1 (+)                                       2  (+)      2  (+)
2 (+)                                       3  (+)      3  (+)
... --> clicked --> myPropertyChanged -->  ...     -->  4  (+)
n (+)                                      n+1 (+)     n+1 (+)

(+):表示存在连接

更多解释

要明确的是,事件循环的规则如下:顺序任务以优先级执行,如果信号不紧急则对信号进行等待。

让我们更详细地分析您的代码:

  1. 的insertRow(0);
  2. m_myProperty = m_animals.count();
  3. 发出myPropertyChanged();
  4. 前面的行是按顺序执行的,所以在第一步结束时,模型中已经有一个新元素,但视图尚未更新,因为代码必须返回到事件循环,为此必须完成第3步。

    在完成第3步之后,执行信号任务,所以你要做的就是创建委托和连接,所以现在你优先考虑myPropertyChanged信号并调用现有连接减去最后一个因为在发出信号时它不存在。

    总之,只会调用信号发出信号时存在的时隙,在信号发射后立即创建的新连接将不会被调用。

相关问题