例如,我取 the official example from Qt Documentation并添加了几行来演示问题:
模型.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]
模型.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。
最佳答案
当您使用 insertRow(0)
时,您是在位置 0 插入,因此前一个 0 元素现在将成为具有连接并接收信号的元素。
所以最后插入的元素在点击的时候没有连接所以不会被通知。
为了说明我会一步一步解释:
在第一个插入中没有元素,所以没有人收到信号。
在第二次插入时,之前的0元素将成为当前的1,并且它有连接所以它会被通知。
在第三次插入中,前一个 0 元素将成为当前 1,而前一个元素将成为当前 2,因此 1、2 具有连接并会收到通知。
总而言之,委托(delegate)的创建是在 myPropertyChanged
信号发出之后,所以插入的委托(delegate)不会被通知,其他人会收到通知,并且由于你的插入总是在第一个位置永远不会打印 qml: 0, n
要以图形方式理解,假设有代表,因为他们已经存在,他们将收到通知:
0 0 (+)
0 (+) 1 (+) 1 (+)
1 (+) 2 (+) 2 (+)
2 (+) 3 (+) 3 (+)
... --> clicked --> myPropertyChanged --> ... --> 4 (+)
n (+) n+1 (+) n+1 (+)
(+):表示有连接
更多解释:
要明确的是,事件循环的规则如下:顺序任务优先执行,信号调用如果不紧急则等待。
让我们更详细地分析您的代码:
- 插入行(0);
- m_myProperty = m_animals.count();
- 发出 myPropertyChanged();
前面几行是按顺序执行的,所以在第一步结束时模型中已经有一个新元素,但是 View 还没有更新,因为为此代码必须返回到事件循环,为此它必须完成第 3 步的执行。
刚完成第 3 步,信号任务就被执行了,所以你所做的就是创建委托(delegate)和连接,所以现在你优先考虑 myPropertyChanged
信号并调用现有的连接减去最后一个,因为发出信号时它不存在。
总之只有信号发出信号时存在的槽会被调用,信号发出后立即创建的新连接直到槽调用都不会被调用。
关于c++ - QML 连接在新插入的行中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50563761/