Added support for binding simple data to combo boxes. (not tested)
This commit is contained in:
@@ -8,10 +8,12 @@
|
||||
#include <QMetaMethod>
|
||||
#include <QDebug>
|
||||
#include <QVariant>
|
||||
#include <QComboBox>
|
||||
|
||||
#include "iform.h"
|
||||
#include "service.h"
|
||||
#include "ivalidator.h"
|
||||
#include "combodata.h"
|
||||
|
||||
|
||||
template <class T>
|
||||
@@ -48,6 +50,10 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void registerBinding(QComboBox *combo, const QList<ComboData> &values) {
|
||||
m_bindCombos[combo] = values;
|
||||
}
|
||||
|
||||
void registerValidator(IValidator *validator) {
|
||||
m_validators.append(validator);
|
||||
}
|
||||
@@ -60,6 +66,7 @@ private:
|
||||
QSharedPointer<T> m_entity;
|
||||
QList<QWidget*> m_bindWidgets;
|
||||
QList<IValidator*> m_validators;
|
||||
QHash<QComboBox*, QList<ComboData> > m_bindCombos;
|
||||
bool m_newRec;
|
||||
|
||||
void bindToUi() {
|
||||
@@ -67,6 +74,24 @@ private:
|
||||
const char* prop = widget->metaObject()->userProperty().name();
|
||||
widget->setProperty(prop, ((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str()));
|
||||
}
|
||||
|
||||
foreach (QComboBox *combo, m_bindCombos.keys()) {
|
||||
int idx = 0;
|
||||
QVariant field = ((QObject*)m_entity.data())->property(combo->objectName().toStdString().c_str());
|
||||
|
||||
combo->clear();
|
||||
for (int i = 0; i < m_bindCombos[combo].size(); i++) {
|
||||
ComboData data = m_bindCombos[combo][i];
|
||||
combo->addItem(data.label(), data.index());
|
||||
|
||||
if (field == data.index()) {
|
||||
idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
combo->setCurrentIndex(idx);
|
||||
}
|
||||
|
||||
bindOtherToUi();
|
||||
}
|
||||
|
||||
@@ -83,6 +108,10 @@ private:
|
||||
((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), widget->property(prop));
|
||||
}
|
||||
|
||||
foreach (QComboBox *combo, m_bindCombos.keys()) {
|
||||
((QObject*)m_entity.data())->setProperty(combo->objectName().toStdString().c_str(), combo->currentData());
|
||||
}
|
||||
|
||||
return bindOtherToData();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user