[英]How to get the ID from a newly inserted row with QSqlQuery and SQLite?
[英]QML Connections not working in newly inserted row?
作为示例,我以Qt文档中的官方示例为例,并添加了几行代码来演示该问题:
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。
当您使用insertRow(0)
您将插入位置0,因此,先前的0元素现在将成为具有连接并接收信号的元素。
因此,在单击click时最后插入的元素将没有连接,因此不会被通知。
为了说明,我将逐步解释它:
在第一个插入物中没有元素,因此没有人接收信号。
在第二次插入中,前一个0元素将是当前元素1,并且具有连接,因此将被通知。
在第三个插入中,前一个0元素将是当前1,而前一个0将是当前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 (+)
(+):表示有连接
更多说明:
需要明确的是,事件循环的规则如下:优先执行顺序任务,如果信号不紧急,则等待信号调用。
让我们更详细地分析您的代码:
前几行是按顺序执行的,因此在第一步结束时,模型中已经有一个新元素,但是视图尚未更新,因为为此,代码必须返回到事件循环,并且必须完成执行步骤3。
在完成第3步之后,便执行了信号任务,因此您要做的就是创建委托和连接,因此现在您将myPropertyChanged
信号赋予优先级,并调用现有连接减去最后一个连接,因为该连接在以下情况不存在:信号发出了。
总而言之,只有在信号发射时存在的插槽才会被调用,在信号发射后立即创建的新连接将一直被调用,直到调用插槽为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.