Browse Source

qml binding autodetect view and viewmodel fixed

pull/2/head
Skycoder42 7 years ago
parent
commit
df3066ff34
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 34
      src/imports/mvvmcore/qqmlmvvmbinding.cpp
  2. 5
      src/imports/mvvmcore/qqmlmvvmbinding.h

34
src/imports/mvvmcore/qqmlmvvmbinding.cpp

@ -16,14 +16,36 @@ QQmlMvvmBinding::QQmlMvvmBinding(QObject *parent) :
this, &QQmlMvvmBinding::resetBinding);
connect(this, &QQmlMvvmBinding::typeChanged,
this, &QQmlMvvmBinding::resetBinding);
connect(this, &QQmlMvvmBinding::viewChanged,
this, &QQmlMvvmBinding::viewWasSet);
connect(this, &QQmlMvvmBinding::viewModelChanged,
this, &QQmlMvvmBinding::viewModelWasSet);
}
void QQmlMvvmBinding::classBegin() {}
void QQmlMvvmBinding::componentComplete()
{
if(!_view)
// auto-detect parent item a view
if(!_viewSet) {
_view = parent();
emit viewChanged(_view);
}
// auto-detect parent items viewmodel
if(_view && !_viewModelSet) {
auto vmo = _view->metaObject();
auto vmPropIdx = vmo->indexOfProperty("viewModel");
if(vmPropIdx != -1) {
auto vm = vmo->property(vmPropIdx).read(_view).value<QObject*>();
if(vm) {
_viewModel = vm;
emit viewModelChanged(_viewModel);
}
}
}
_completed = true;
resetBinding();
}
@ -64,3 +86,13 @@ void QQmlMvvmBinding::resetBinding()
_viewModelChangeSignal.isEmpty() ? nullptr : qUtf8Printable(_viewModelChangeSignal),
_viewChangeSignal.isEmpty() ? nullptr : qUtf8Printable(_viewChangeSignal));
}
void QQmlMvvmBinding::viewWasSet()
{
_viewSet = true;
}
void QQmlMvvmBinding::viewModelWasSet()
{
_viewModelSet = true;
}

5
src/imports/mvvmcore/qqmlmvvmbinding.h

@ -157,9 +157,14 @@ Q_SIGNALS:
private Q_SLOTS:
void resetBinding();
void viewWasSet();
void viewModelWasSet();
private:
Binding _binding;
bool _completed = false;
bool _viewSet = false;
bool _viewModelSet = false;
QObject* _viewModel = nullptr;
QString _viewModelProperty;

Loading…
Cancel
Save