AutoTableModel now can display data from related table.

Usage example of ComboBox binding in Accommodation plugin.
print
Josef Rokos 9 years ago
parent 1b6141923f
commit 7ea06881ef

@ -17,7 +17,8 @@ SOURCES += accommodation.cpp \
accommodationservice.cpp \
tablemodel.cpp \
acform.cpp \
accgrid.cpp
accgrid.cpp \
data/address.cpp
HEADERS += accommodation.h\
accommodation_global.h \
@ -25,7 +26,9 @@ HEADERS += accommodation.h\
accommodationservice.h \
tablemodel.h \
acform.h \
accgrid.h
accgrid.h \
data/address.h \
data/accommodation-data.h
unix {
target.path = /usr/lib
@ -53,7 +56,7 @@ OTHER_FILES += \
FORMS += \
acform.ui
ODB_FILES = accommodation/data/person.h
ODB_FILES = accommodation/data/accommodation-data.h
H_DIR = $$PWD/data/*.h
include(../odb.pri)

@ -1,5 +1,8 @@
#include "acform.h"
#include "ui_acform.h"
#include <QList>
#include "accommodation-odb.hxx"
AcForm::AcForm(QWidget *parent) :
AutoForm<Person>(parent),
@ -15,3 +18,14 @@ AcForm::~AcForm()
{
delete ui;
}
void AcForm::registerCombos()
{
QList<ComboData> cbData;
Service<Address> srv;
foreach (QSharedPointer<Address> adr, srv.all()) {
cbData.append(ComboData(adr));
}
registerBinding(ui->address, cbData);
}

@ -20,6 +20,10 @@ public:
private:
Ui::AcForm *ui;
// AutoForm interface
protected:
virtual void registerCombos();
};
#endif // ACFORM_H

@ -46,6 +46,16 @@
<string>PushButton</string>
</property>
</widget>
<widget class="QComboBox" name="address">
<property name="geometry">
<rect>
<x>110</x>
<y>170</y>
<width>191</width>
<height>22</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections/>

@ -0,0 +1,8 @@
#ifndef ACCOMMODATIONDATA_H
#define ACCOMMODATIONDATA_H
#include "address.h"
#include "person.h"
#endif // ACCOMMODATIONDATA_H

@ -0,0 +1,58 @@
#include "address.h"
Address::Address(QObject *parent) : ComboItem(parent)
{
}
Address::~Address()
{
}
QString Address::city() const
{
return m_city;
}
void Address::setCity(const QString &city)
{
m_city = city;
}
QString Address::street() const
{
return m_street;
}
void Address::setStreet(const QString &street)
{
m_street = street;
}
QString Address::houseNumber() const
{
return m_houseNumber;
}
void Address::setHouseNumber(const QString &houseNumber)
{
m_houseNumber = houseNumber;
}
int Address::id() const
{
return m_id;
}
void Address::setId(int id)
{
m_id = id;
}
bool Address::eq(ComboItem *other)
{
Address *addr = qobject_cast<Address*>(other);
return addr != NULL && m_id == addr->id();
}
QString Address::toString()
{
return m_street + ", " + m_houseNumber + ", " + m_city;
}

@ -0,0 +1,55 @@
#ifndef ADDRESS_H
#define ADDRESS_H
#include <QObject>
#include <QString>
#include <data/comboitem.h>
#include <odb/core.hxx>
#pragma db object
class Address : public ComboItem
{
Q_OBJECT
Q_PROPERTY(QString city READ city WRITE setCity)
Q_PROPERTY(QString street READ street WRITE setStreet)
Q_PROPERTY(QString houseNumber READ houseNumber WRITE setHouseNumber)
public:
explicit Address(QObject *parent = 0);
~Address();
QString city() const;
void setCity(const QString &city);
QString street() const;
void setStreet(const QString &street);
QString houseNumber() const;
void setHouseNumber(const QString &houseNumber);
int id() const;
void setId(int id);
private:
friend class odb::access;
#pragma db id auto
int m_id;
QString m_city;
QString m_street;
QString m_houseNumber;
signals:
public slots:
// ComboItem interface
public:
virtual bool eq(ComboItem *other);
virtual QString toString();
};
#endif // ADDRESS_H

@ -1,3 +1,4 @@
#include <data/comboitem.h>
#include "person.h"
Person::Person()
@ -30,6 +31,19 @@ void Person::setLastName(const QString &value)
{
lastName = value;
}
QSharedPointer<QObject> Person::address() const
{
return m_address;
}
void Person::setAddress(const QSharedPointer<QObject> &address)
{
if (qobject_cast<Address*>(address.data()) != NULL)
{
m_address = qSharedPointerDynamicCast<Address, QObject>(address);
}
}

@ -4,6 +4,8 @@
#include <QObject>
#include <QString>
#include "address.h"
#include <odb/core.hxx>
#pragma db object
@ -13,6 +15,7 @@ class Person : public QObject
Q_PROPERTY(QString firstName READ getFirstName WRITE setFirstName)
Q_PROPERTY(QString lastName READ getLastName WRITE setLastName)
Q_PROPERTY(QSharedPointer<QObject> address READ address WRITE setAddress)
public:
Person();
@ -25,12 +28,16 @@ public:
QString getLastName() const;
void setLastName(const QString &value);
QSharedPointer<QObject> address() const;
void setAddress(const QSharedPointer<QObject> &address);
private:
friend class odb::access;
#pragma db id auto
int id;
QString firstName;
QString lastName;
QSharedPointer<Address> m_address;
};

@ -60,6 +60,7 @@ public:
protected:
virtual void bindOtherToUi() {}
virtual bool bindOtherToData() { return true; }
virtual void registerCombos() {}
private:
QSharedPointer<T> m_entity;
@ -69,6 +70,7 @@ private:
bool m_newRec;
void bindToUi() {
registerCombos();
foreach (QWidget *widget, m_bindWidgets) {
const char* prop = widget->metaObject()->userProperty().name();
widget->setProperty(prop, ((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str()));

@ -10,6 +10,7 @@
#include "core_global.h"
#include "exprevaluator.h"
#include "itablemodel.h"
#include "data/comboitem.h"
template<class T>
class AutoTableModel : public ITableModel
@ -51,7 +52,13 @@ public:
QSharedPointer<T> entity = m_list.at(index.row());
QObject *rawEntity = (QObject*)entity.data();
return rawEntity->property(rawEntity->metaObject()->property(index.column() + 1).name());
QVariant dispData = rawEntity->property(rawEntity->metaObject()->property(index.column() + 1).name());
if (dispData.canConvert<QObject*>() && qobject_cast<ComboItem*>(dispData.value<QObject*>()))
{
return qobject_cast<ComboItem*>(dispData.value<QObject*>())->toString();
}
return dispData;
}
return QVariant::Invalid;

@ -42,6 +42,7 @@ DEFINES += DATABASE_SQLITE
#
ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]
ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]/QtCore
ODB_FLAGS += -I $$PWD/core
ODB_FLAGS += -D __PIC__
win32 {

Loading…
Cancel
Save