[Qt-interest] Default get/set
André Somers
andre at familiesomers.nl
Thu Jul 28 16:55:57 CEST 2011
Sounds like something you can easily create functions for.
If you create a pair of function like this:
QVariant getUserProperty(QObject* object);
bool setUserProperty(QObject* object, QVariant value);
You can then use QMetaObject to find out what the user property is
(QMetaObject::userProperty())
Check if the returned QMetaProperty is valid (QMetaProperty::isValid()), and
then use either its read() or its write() method.
Seems quite simple, unless I misunderstood what you were really after.
Andre
On Thu, Jul 28, 2011 at 3:54 PM, Jason H <scorp1us at yahoo.com> wrote:
> I've always coded a solution for this, but I'm wondering if others would
> want it, and why it hasn't already been done (generally by now, if its not
> done, its not a good idea)
>
> Anyway, I'd love to have a generic getter/setter on QWidgets that maps to
> the userProperty().
>
> I know there are a variety of approaches, using QDataWidgetMapper (for
> matting to model roles) but many times that is overkill. Maybe it's bad
> design that I name my database fields the same as my widgets, or I'm not
> using Qt correctly, but I'd love to have a form or result set be able to
> share data without a lot of coding.
>
> init:
> QMap<Qstring, QWidget*> widgetMap;
> foreach (QString column, queryResults.columns())
> {
> QWidget *w = findWidget(column);
> if (w) widgetMap[column]=w;
> }
>
> load:
> foreach (QString name, widgetMap.keys())
> {
> widgetMap[name].set(queryResults[name]);
> }
>
> save:
> foreach (QString name, widgetMap.keys())
> {
> updateQuery.bindValue(name, widgetMap[name].get());
> }
>
> It's just be nice to not have to care about the specific member function
> unless you really have to. The most complicated example I can come up with
> is setting a selection list for QListBox.
>
> If a generic get/set is considered bad, what about
> widget.userProperty().get() and widget.userProperty().set(...)
>
> Thoughts?
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110728/2d88dc9e/attachment.html
More information about the Qt-interest-old
mailing list