Add a Q_PROPERTY for the sourceModel of a proxy model.
Now that Q_PROPERTY with a QObject derived type is more powerful. This property can be used in QML so that wrappers for proxy models do not need to be created, such as in the example at https://codereview.qt-project.org/#change,13007 Change-Id: I6ba676549d2135585d429a28e214fef0b2a6b1f9 Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
parent
e3a9ac4f36
commit
648d5964ee
@ -122,16 +122,19 @@ QAbstractProxyModel::~QAbstractProxyModel()
|
||||
void QAbstractProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
|
||||
{
|
||||
Q_D(QAbstractProxyModel);
|
||||
if (d->model)
|
||||
disconnect(d->model, SIGNAL(destroyed()), this, SLOT(_q_sourceModelDestroyed()));
|
||||
if (sourceModel != d->model) {
|
||||
if (d->model)
|
||||
disconnect(d->model, SIGNAL(destroyed()), this, SLOT(_q_sourceModelDestroyed()));
|
||||
|
||||
if (sourceModel) {
|
||||
d->model = sourceModel;
|
||||
connect(d->model, SIGNAL(destroyed()), this, SLOT(_q_sourceModelDestroyed()));
|
||||
} else {
|
||||
d->model = QAbstractItemModelPrivate::staticEmptyModel();
|
||||
if (sourceModel) {
|
||||
d->model = sourceModel;
|
||||
connect(d->model, SIGNAL(destroyed()), this, SLOT(_q_sourceModelDestroyed()));
|
||||
} else {
|
||||
d->model = QAbstractItemModelPrivate::staticEmptyModel();
|
||||
}
|
||||
d->roleNames = d->model->roleNames();
|
||||
emit sourceModelChanged();
|
||||
}
|
||||
d->roleNames = d->model->roleNames();
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -57,6 +57,7 @@ class QItemSelection;
|
||||
class Q_CORE_EXPORT QAbstractProxyModel : public QAbstractItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QAbstractItemModel* sourceModel READ sourceModel WRITE setSourceModel NOTIFY sourceModelChanged)
|
||||
|
||||
public:
|
||||
explicit QAbstractProxyModel(QObject *parent = 0);
|
||||
@ -94,6 +95,9 @@ public:
|
||||
QStringList mimeTypes() const;
|
||||
Qt::DropActions supportedDropActions() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void sourceModelChanged();
|
||||
|
||||
protected:
|
||||
QAbstractProxyModel(QAbstractProxyModelPrivate &, QObject *parent);
|
||||
|
||||
|
@ -283,15 +283,25 @@ void tst_QAbstractProxyModel::revert()
|
||||
// public void setSourceModel(QAbstractItemModel* sourceModel)
|
||||
void tst_QAbstractProxyModel::setSourceModel()
|
||||
{
|
||||
qRegisterMetaType<QAbstractItemModel*>();
|
||||
|
||||
SubQAbstractProxyModel model;
|
||||
|
||||
QCOMPARE(model.property("sourceModel"), QVariant::fromValue<QAbstractItemModel*>(0));
|
||||
QStandardItemModel *sourceModel = new QStandardItemModel(&model);
|
||||
model.setSourceModel(sourceModel);
|
||||
QCOMPARE(model.sourceModel(), static_cast<QAbstractItemModel*>(sourceModel));
|
||||
|
||||
QCOMPARE(model.property("sourceModel").value<QObject*>(), static_cast<QObject*>(sourceModel));
|
||||
QCOMPARE(model.property("sourceModel").value<QAbstractItemModel*>(), sourceModel);
|
||||
|
||||
QStandardItemModel *sourceModel2 = new QStandardItemModel(&model);
|
||||
model.setSourceModel(sourceModel2);
|
||||
QCOMPARE(model.sourceModel(), static_cast<QAbstractItemModel*>(sourceModel2));
|
||||
|
||||
QCOMPARE(model.property("sourceModel").value<QObject*>(), static_cast<QObject*>(sourceModel2));
|
||||
QCOMPARE(model.property("sourceModel").value<QAbstractItemModel*>(), sourceModel2);
|
||||
|
||||
delete sourceModel2;
|
||||
QCOMPARE(model.sourceModel(), static_cast<QAbstractItemModel*>(0));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user